call a separate get_devices() with locking, as required
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
diff --git a/api.c b/api.c
index 7432637..776e735 100644
--- a/api.c
+++ b/api.c
@@ -1194,13 +1194,17 @@ static int pgadevice(int pgaid)
if (devices[i]->drv->drv_id == DRIVER_MODMINER)
count++;
#endif
- if (count == (pgaid + 1)) {
- mutex_unlock(&devices_lock);
- return i;
- }
+ if (count == (pgaid + 1))
+ goto foundit;
}
+
mutex_unlock(&devices_lock);
return -1;
+
+foundit:
+
+ mutex_unlock(&devices_lock);
+ return i;
}
#endif
@@ -1533,14 +1537,9 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
if (dev < 0) // Should never happen
return;
- struct cgpu_info *cgpu;
+ struct cgpu_info *cgpu = get_devices(dev);
double frequency = 0;
- float temp;
-
- mutex_lock(&devices_lock);
- cgpu = devices[dev];
- mutex_unlock(&devices_lock);
- temp = cgpu->temp;
+ float temp = cgpu->temp;
#ifdef USE_ZTEX
if (cgpu->drv->drv_id == DRIVER_ZTEX && cgpu->device_ztex)
@@ -1787,9 +1786,7 @@ static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
return;
}
- mutex_lock(&devices_lock);
- cgpu = devices[dev];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(dev);
applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s%u",
id, dev, cgpu->drv->name, cgpu->device_id);
@@ -1854,9 +1851,7 @@ static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
return;
}
- mutex_lock(&devices_lock);
- cgpu = devices[dev];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(dev);
applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s%u",
id, dev, cgpu->drv->name, cgpu->device_id);
@@ -1900,9 +1895,7 @@ static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
return;
}
- mutex_lock(&devices_lock);
- cgpu = devices[dev];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(dev);
drv = cgpu->drv;
if (!drv->identify_device)
@@ -2830,9 +2823,7 @@ static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
io_open = io_add(io_data, COMSTR JSON_NOTIFY);
for (i = 0; i < total_devices; i++) {
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(i);
notifystatus(io_data, i, cgpu, isjson, group);
}
@@ -2859,9 +2850,7 @@ static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
for (i = 0; i < total_devices; i++) {
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(i);
root = api_add_int(root, "DEVDETAILS", &i, false);
root = api_add_string(root, "Name", cgpu->drv->name, false);
@@ -2971,9 +2960,7 @@ static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
i = 0;
for (j = 0; j < total_devices; j++) {
- mutex_lock(&devices_lock);
- cgpu = devices[j];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(j);
if (cgpu && cgpu->drv) {
if (cgpu->drv->get_api_stats)
@@ -3248,9 +3235,7 @@ static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
return;
}
- mutex_lock(&devices_lock);
- cgpu = devices[dev];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(dev);
drv = cgpu->drv;
char *set = strchr(opt, ',');
diff --git a/cgminer.c b/cgminer.c
index 5981b7c..33d4341 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -390,6 +390,16 @@ static struct cgpu_info *get_thr_cgpu(int thr_id)
return thr->cgpu;
}
+struct cgpu_info *get_devices(int id)
+{
+ struct cgpu_info *cgpu;
+
+ mutex_lock(&devices_lock);
+ cgpu = devices[id];
+ mutex_unlock(&devices_lock);
+ return cgpu;
+}
+
static void sharelog(const char*disposition, const struct work*work)
{
char *target, *hash, *data;
@@ -4043,11 +4053,7 @@ void zero_stats(void)
zero_bestshare();
for (i = 0; i < total_devices; ++i) {
- struct cgpu_info *cgpu;
-
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
+ struct cgpu_info *cgpu = get_devices(i);
mutex_lock(&hash_lock);
cgpu->total_mhashes = 0;
@@ -5967,17 +5973,12 @@ static void *watchdog_thread(void __maybe_unused *userdata)
}
for (i = 0; i < total_devices; ++i) {
- struct cgpu_info *cgpu;
- struct thr_info *thr;
+ struct cgpu_info *cgpu = get_devices(i);
+ struct thr_info *thr = cgpu->thr[0];
enum dev_enable *denable;
char dev_str[8];
int gpu;
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
- thr = cgpu->thr[0];
-
if (cgpu->drv->get_stats)
cgpu->drv->get_stats(cgpu);
@@ -6141,10 +6142,8 @@ void print_summary(void)
applog(LOG_WARNING, "Summary of per device statistics:\n");
for (i = 0; i < total_devices; ++i) {
- struct cgpu_info *cgpu;
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
+ struct cgpu_info *cgpu = get_devices(i);
+
log_print_status(cgpu);
}
diff --git a/driver-ztex.c b/driver-ztex.c
index add381b..27b8c26 100644
--- a/driver-ztex.c
+++ b/driver-ztex.c
@@ -389,10 +389,11 @@ static void ztex_shutdown(struct thr_info *thr)
static void ztex_disable(struct thr_info *thr)
{
+ struct cgpu_info *cgpu;
+
applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
- mutex_lock(&devices_lock);
- devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(thr->cgpu->device_id);
+ cgpu->deven = DEV_DISABLED;
ztex_shutdown(thr);
}
diff --git a/miner.h b/miner.h
index 5b5af43..3c4f89f 100644
--- a/miner.h
+++ b/miner.h
@@ -1110,6 +1110,7 @@ extern void free_work(struct work *work);
extern void __copy_work(struct work *work, struct work *base_work);
extern struct work *copy_work(struct work *base_work);
extern struct thr_info *get_thread(int thr_id);
+extern struct cgpu_info *get_devices(int id);
enum api_data_type {
API_ESCAPE,
diff --git a/usbutils.c b/usbutils.c
index 8d4a095..88edb93 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -624,9 +624,7 @@ static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint
case WAIT_ABANDONED:
// Am I using it already?
for (i = 0; i < total_devices; i++) {
- mutex_lock(&devices_lock);
- cgpu = devices[i];
- mutex_unlock(&devices_lock);
+ cgpu = get_devices(i);
if (cgpu->usbinfo.bus_number == bus_number &&
cgpu->usbinfo.device_address == device_address &&
cgpu->usbinfo.nodev == false) {
@@ -867,9 +865,7 @@ static void release_cgpu(struct cgpu_info *cgpu)
// Any devices sharing the same USB device should be marked also
// Currently only MMQ shares a USB device
for (i = 0; i < total_devices; i++) {
- mutex_lock(&devices_lock);
- lookcgpu = devices[i];
- mutex_unlock(&devices_lock);
+ lookcgpu = get_devices(i);
if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
lookcgpu->usbinfo.nodev = true;
lookcgpu->usbinfo.nodev_count++;