Commit cc04d3abc7c05115457d9163052d0272319e9bcf

Con Kolivas 2013-05-30T11:01:29

Use a counting semaphore to signal the usb resource thread that it has work to do.

diff --git a/cgminer.c b/cgminer.c
index 47d6cc3..ea2ff51 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -28,6 +28,10 @@
 #include <assert.h>
 #include <signal.h>
 
+#ifdef USE_USBUTILS
+#include <semaphore.h>
+#endif
+
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -144,6 +148,7 @@ char *opt_avalon_options = NULL;
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
 bool opt_usb_list_all;
+sem_t usb_resource_sem;
 #endif
 
 char *opt_kernel_path;
@@ -7358,6 +7363,8 @@ int main(int argc, char *argv[])
 
 	// before device detection
 	if (!opt_scrypt) {
+		if (!sem_init(&usb_resource_sem, 0, 0))
+			quit(1, "Failed to sem_init usb_resource_sem");
 		usbres_thr_id = 1;
 		thr = &control_thr[usbres_thr_id];
 		if (thr_info_create(thr, NULL, usb_resource_thread, thr))
diff --git a/miner.h b/miner.h
index 5028d39..410df53 100644
--- a/miner.h
+++ b/miner.h
@@ -19,6 +19,10 @@
 # include <netdb.h>
 #endif
 
+#ifdef USE_USBUTILS
+#include <semaphore.h>
+#endif
+
 #ifdef HAVE_OPENCL
 #ifdef __APPLE_CC__
 #include <OpenCL/opencl.h>
@@ -846,6 +850,7 @@ extern char *opt_avalon_options;
 extern char *opt_usb_select;
 extern int opt_usbdump;
 extern bool opt_usb_list_all;
+extern sem_t usb_resource_sem;
 #endif
 #ifdef USE_BITFORCE
 extern bool opt_bfl_noncerange;
diff --git a/usbutils.c b/usbutils.c
index a0d6ba5..3c5092b 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1199,18 +1199,17 @@ static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint
 	res_work->device_address = device_address;
 
 	mutex_lock(&cgusbres_lock);
-
 	res_work->next = res_work_head;
 	res_work_head = res_work;
-
 	mutex_unlock(&cgusbres_lock);
 
-	nmsleep(46);
+	sem_post(&usb_resource_sem);
 
 	// TODO: add a timeout fail - restart the resource thread?
 	while (true) {
-		mutex_lock(&cgusbres_lock);
+		nmsleep(50);
 
+		mutex_lock(&cgusbres_lock);
 		if (res_reply_head) {
 			struct resource_reply *res_reply_prev = NULL;
 			struct resource_reply *res_reply = res_reply_head;
@@ -1235,10 +1234,7 @@ static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint
 				res_reply = res_reply->next;
 			}
 		}
-
 		mutex_unlock(&cgusbres_lock);
-
-		nmsleep(45);
 	}
 }
 
@@ -1262,12 +1258,12 @@ static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, ui
 	res_work->device_address = device_address;
 
 	mutex_lock(&cgusbres_lock);
-
 	res_work->next = res_work_head;
 	res_work_head = res_work;
-
 	mutex_unlock(&cgusbres_lock);
 
+	sem_post(&usb_resource_sem);
+
 	return;
 }
 
@@ -2893,15 +2889,14 @@ void *usb_resource_thread(void __maybe_unused *userdata)
 
 	applog(LOG_DEBUG, "RES: thread starting");
 
-	while (0*1337+1) {
-		mutex_lock(&cgusbres_lock);
+	while (42) {
+		/* Wait to be told we have work to do */
+		sem_wait(&usb_resource_sem);
 
+		mutex_lock(&cgusbres_lock);
 		while (res_work_head)
 			resource_process();
-
 		mutex_unlock(&cgusbres_lock);
-
-		nmsleep(45);
 	}
 
 	return NULL;