Simplify dramatically the cross-process cgminer locking through use of flock instead of sysv semaphores.
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
diff --git a/usbutils.c b/usbutils.c
index 23045e9..c305f6c 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -664,8 +664,12 @@ static struct driver_count {
static struct usb_busdev {
int bus_number;
int device_address;
+#ifdef WIN32
void *resource1;
void *resource2;
+#else
+ int fd;
+#endif
} *busdev;
static int busdev_count = 0;
@@ -1204,6 +1208,7 @@ void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount
err, amount);
}
+#ifdef WIN32
static void in_use_store_ress(uint8_t bus_number, uint8_t device_address, void *resource1, void *resource2)
{
struct usb_in_use_list *in_use_tmp;
@@ -1275,6 +1280,58 @@ static void in_use_get_ress(uint8_t bus_number, uint8_t device_address, void **r
applog(LOG_ERR, "FAIL: USB get_lock empty (%d:%d)",
(int)bus_number, (int)device_address);
}
+#else
+
+static void in_use_store_fd(uint8_t bus_number, uint8_t device_address, int fd)
+{
+ struct usb_in_use_list *in_use_tmp;
+ bool found = false;
+
+ mutex_lock(&cgusb_lock);
+ in_use_tmp = in_use_head;
+ while (in_use_tmp) {
+ if (in_use_tmp->in_use.bus_number == (int)bus_number &&
+ in_use_tmp->in_use.device_address == (int)device_address) {
+ found = true;
+ in_use_tmp->in_use.fd = fd;
+ break;
+ }
+ in_use_tmp = in_use_tmp->next;
+ }
+ mutex_unlock(&cgusb_lock);
+
+ if (found == false) {
+ applog(LOG_ERR, "FAIL: USB store_fd not found (%d:%d)",
+ (int)bus_number, (int)device_address);
+ }
+}
+
+static int in_use_get_fd(uint8_t bus_number, uint8_t device_address)
+{
+ struct usb_in_use_list *in_use_tmp;
+ bool found = false;
+ int fd = -1;
+
+ mutex_lock(&cgusb_lock);
+ in_use_tmp = in_use_head;
+ while (in_use_tmp) {
+ if (in_use_tmp->in_use.bus_number == (int)bus_number &&
+ in_use_tmp->in_use.device_address == (int)device_address) {
+ found = true;
+ fd = in_use_tmp->in_use.fd;
+ break;
+ }
+ in_use_tmp = in_use_tmp->next;
+ }
+ mutex_unlock(&cgusb_lock);
+
+ if (found == false) {
+ applog(LOG_ERR, "FAIL: USB get_lock not found (%d:%d)",
+ (int)bus_number, (int)device_address);
+ }
+ return fd;
+}
+#endif
static bool _in_use(struct usb_in_use_list *head, uint8_t bus_number,
uint8_t device_address)
@@ -3554,10 +3611,8 @@ void usb_initialise(void)
#ifndef WIN32
#include <errno.h>
#include <unistd.h>
-#include <sys/types.h>
-#include <sys/ipc.h>
-#include <sys/sem.h>
#include <sys/stat.h>
+#include <sys/file.h>
#include <fcntl.h>
#ifndef __APPLE__
@@ -3643,7 +3698,6 @@ static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_
static bool resource_lock(const char *dname, uint8_t bus_number, uint8_t device_address)
{
applog(LOG_DEBUG, "USB res lock %s %d-%d", dname, (int)bus_number, (int)device_address);
-
#ifdef WIN32
struct cgpu_info *cgpu;
LPSECURITY_ATTRIBUTES sec;
@@ -3721,12 +3775,8 @@ fail:
sec = unsec(sec);
return false;
#else
- struct semid_ds seminfo;
- union semun opt;
char name[64];
- key_t *key;
- int *sem;
- int fd, count;
+ int fd;
if (is_in_use_bd(bus_number, device_address))
return false;
@@ -3734,89 +3784,20 @@ fail:
snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd == -1) {
- applog(LOG_ERR,
- "SEM: %s USB open failed '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto _out;
- }
- close(fd);
-
- key = malloc(sizeof(*key));
- if (unlikely(!key))
- quit(1, "SEM: Failed to malloc key");
-
- sem = malloc(sizeof(*sem));
- if (unlikely(!sem))
- quit(1, "SEM: Failed to malloc sem");
-
- *key = ftok(name, 'K');
- *sem = semget(*key, 1, IPC_CREAT | IPC_EXCL | 438);
- if (*sem < 0) {
- if (errno != EEXIST) {
- applog(LOG_ERR,
- "SEM: %s USB failed to get '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto free_out;
- }
-
- *sem = semget(*key, 1, 0);
- if (*sem < 0) {
- applog(LOG_ERR,
- "SEM: %s USB failed to access '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto free_out;
- }
-
- opt.buf = &seminfo;
- count = 0;
- while (++count) {
- // Should NEVER take 100ms
- if (count > 99) {
- applog(LOG_ERR,
- "SEM: %s USB timeout waiting for (%d) '%s'",
- dname, *sem, name);
- goto free_out;
- }
- if (semctl(*sem, 0, IPC_STAT, opt) == -1) {
- applog(LOG_ERR,
- "SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
- dname, *sem, name, count, errno, strerror(errno));
- goto free_out;
- }
- if (opt.buf->sem_otime != 0)
- break;
- cgsleep_ms(1);
- }
+ applog(LOG_ERR, "%s USB open failed '%s' err (%d) %s",
+ dname, name, errno, strerror(errno));
+ return false;
}
-
- struct sembuf sops[] = {
- { 0, 0, IPC_NOWAIT | SEM_UNDO },
- { 0, 1, IPC_NOWAIT | SEM_UNDO }
- };
-
- if (semop(*sem, sops, 2)) {
- if (errno == EAGAIN) {
- if (!hotplug_mode)
- applog(LOG_WARNING,
- "SEM: %s USB failed to get (%d) '%s' - device in use",
- dname, *sem, name);
- } else {
- applog(LOG_DEBUG,
- "SEM: %s USB failed to get (%d) '%s' err (%d) %s",
- dname, *sem, name, errno, strerror(errno));
- }
- goto free_out;
+ if (flock(fd, LOCK_EX | LOCK_NB)) {
+ applog(LOG_INFO, "%s USB failed to get '%s' - device in use",
+ dname, name);
+ close(fd);
+ return false;
}
add_in_use(bus_number, device_address, false);
- in_use_store_ress(bus_number, device_address, (void *)key, (void *)sem);
+ in_use_store_fd(bus_number, device_address, fd);
return true;
-
-free_out:
- free(sem);
- free(key);
-_out:
- return false;
#endif
}
@@ -3853,41 +3834,15 @@ fila:
return;
#else
char name[64];
- key_t *key = NULL;
- int *sem = NULL;
+ int fd;
snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
- in_use_get_ress(bus_number, device_address, (void **)(&key), (void **)(&sem));
-
- if (!key || !sem)
- goto fila;
-
- struct sembuf sops[] = {
- { 0, -1, SEM_UNDO }
- };
-
- // Allow a 10ms timeout
- // exceeding this timeout means it would probably never succeed anyway
- struct timespec timeout = { 0, 10000000 };
-
- if (semtimedop(*sem, sops, 1, &timeout)) {
- applog(LOG_ERR,
- "SEM: %s USB failed to release '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- }
-
- if (semctl(*sem, 0, IPC_RMID)) {
- applog(LOG_WARNING,
- "SEM: %s USB failed to remove SEM '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- }
-
-fila:
-
- free(sem);
- free(key);
+ fd = in_use_get_fd(bus_number, device_address);
+ if (fd < 0)
+ return;
remove_in_use(bus_number, device_address);
+ close(fd);
unlink(name);
return;
#endif