Use a counting semaphore to signal the usb resource thread that it has work to do.
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
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;