usbutils avoid leaving devlock locked when thread cancelled
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
diff --git a/usbutils.c b/usbutils.c
index f036ea4..c15260a 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1341,7 +1341,7 @@ void usb_uninit(struct cgpu_info *cgpu)
/*
* N.B. this is always called inside
- * wr_lock(cgpu->usbinfo.devlock);
+ * DEVLOCK(cgpu);
*/
static void release_cgpu(struct cgpu_info *cgpu)
{
@@ -1449,6 +1449,19 @@ struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devloc
#define USB_INIT_OK 1
#define USB_INIT_IGNORE 2
+/*
+ * WARNING - these assume DEVLOCK(cgpu) is called first and
+ * DEVUNLOCK(cgpu) in called in the same function inside the same brace level
+ * You must call DEVUNLOCK(cgpu) before exiting the function or it will leave
+ * the thread Cancelability unrestored
+ */
+#define DEVLOCK(cgpu) int _pth_state; \
+ pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &_pth_state); \
+ wr_lock(cgpu->usbinfo.devlock);
+
+#define DEVUNLOCK(cgpu) wr_unlock(cgpu->usbinfo.devlock); \
+ pthread_setcanceltype(_pth_state, NULL);
+
static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
{
struct cg_usb_device *cgusb = NULL;
@@ -1461,7 +1474,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
int err, i, j, k;
int bad = USB_INIT_FAIL;
- wr_lock(cgpu->usbinfo.devlock);
+ DEVLOCK(cgpu);
cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
cgpu->usbinfo.device_address = libusb_get_device_address(dev);
@@ -1701,7 +1714,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
cgpu->drv->name = (char *)(found->name);
}
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return USB_INIT_OK;
@@ -1716,7 +1729,7 @@ dame:
cgusb = free_cgusb(cgusb);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return bad;
}
@@ -2166,14 +2179,14 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
unsigned char usbbuf[USB_MAX_READ+4], *ptr;
size_t usbbufread;
- wr_lock(cgpu->usbinfo.devlock);
+ DEVLOCK(cgpu);
if (cgpu->usbinfo.nodev) {
*buf = '\0';
*processed = 0;
USB_REJECT(cgpu, MODE_BULK_READ);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return LIBUSB_ERROR_NO_DEVICE;
}
@@ -2277,7 +2290,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
if (NODEV(err))
release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return err;
}
@@ -2394,7 +2407,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
if (NODEV(err))
release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return err;
}
@@ -2411,7 +2424,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
__maybe_unused bool first = true;
int err, sent, tot;
- wr_lock(cgpu->usbinfo.devlock);
+ DEVLOCK(cgpu);
USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd));
@@ -2420,7 +2433,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_BULK_WRITE);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return LIBUSB_ERROR_NO_DEVICE;
}
@@ -2474,7 +2487,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
if (NODEV(err))
release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return err;
}
@@ -2488,14 +2501,14 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
uint32_t *buf = NULL;
int err, i, bufsiz;
- wr_lock(cgpu->usbinfo.devlock);
+ DEVLOCK(cgpu);
USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd));
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_CTRL_WRITE);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return LIBUSB_ERROR_NO_DEVICE;
}
@@ -2533,7 +2546,7 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
if (NODEV(err))
release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return err;
}
@@ -2546,14 +2559,14 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
#endif
int err;
- wr_lock(cgpu->usbinfo.devlock);
+ DEVLOCK(cgpu);
USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd));
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_CTRL_READ);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return LIBUSB_ERROR_NO_DEVICE;
}
@@ -2579,7 +2592,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
} else if (NODEV(err))
release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
+ DEVUNLOCK(cgpu);
return err;
}