Commit bc5755233c1b1ecd22f9b7237dbd528462382f56

Kano 2013-02-08T02:12:09

call a separate get_devices() with locking, as required

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++;