Use the reentrant cgsleep functions for usecps in usbutils.
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
diff --git a/usbutils.c b/usbutils.c
index adcd3da..5dab1b6 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2415,23 +2415,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
}
got = 0;
- if (first && usbdev->usecps) {
- if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
- struct timeval now;
- double need;
-
- cgtime(&now);
- need = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps) -
- tdiff(&now, &(usbdev->last_write_tv));
-
- // Simple error condition check/avoidance '< 1.0'
- if (need > 0.0 && need < 1.0) {
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += need;
- cgsleep_ms((unsigned int)(need * 1000.0));
- }
- }
+ if (first && usbdev->usecps && usbdev->last_write_siz) {
+ double need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps);
+
+ cgsleep_ms_r(&usbdev->cgt_last_write, need * 1000.0);
}
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
@@ -2521,23 +2509,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
usbbufread = bufleft;
}
got = 0;
- if (first && usbdev->usecps) {
- if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
- struct timeval now;
- double need;
-
- cgtime(&now);
- need = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps) -
- tdiff(&now, &(usbdev->last_write_tv));
-
- // Simple error condition check/avoidance '< 1.0'
- if (need > 0.0 && need < 1.0) {
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += need;
- cgsleep_ms((unsigned int)(need * 1000.0));
- }
- }
+ if (first && usbdev->usecps && usbdev->last_write_siz) {
+ double need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps);
+
+ cgsleep_ms_r(&usbdev->cgt_last_write, need * 1000.0);
}
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep, ptr,
@@ -2660,24 +2636,12 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
cgtime(&read_start);
while (bufsiz > 0) {
sent = 0;
- if (usbdev->usecps) {
- if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
- struct timeval now;
- double need;
-
- cgtime(&now);
- need = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps) -
- tdiff(&now, &(usbdev->last_write_tv));
-
- // Simple error condition check/avoidance '< 1.0'
- if (need > 0.0 && need < 1.0) {
- cgpu->usbinfo.write_delay_count++;
- cgpu->usbinfo.total_write_delay += need;
- cgsleep_ms((unsigned int)(need * 1000.0));
- }
- }
- cgtime(&(usbdev->last_write_tv));
+ if (usbdev->usecps && usbdev->last_write_siz) {
+ double need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps);
+
+ cgsleep_ms_r(&usbdev->cgt_last_write, need * 1000.0);
+ cgsleep_prepare_r(&usbdev->cgt_last_write);
usbdev->last_write_siz = bufsiz;
}
err = usb_bulk_transfer(usbdev->handle,
@@ -2756,24 +2720,12 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex((unsigned char *)buf, (size_t)siz));
- if (usbdev->usecps) {
- if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
- struct timeval now;
- double need;
+ if (usbdev->usecps && usbdev->last_write_siz) {
+ double need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps);
- cgtime(&now);
- need = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps) -
- tdiff(&now, &(usbdev->last_write_tv));
-
- // Simple error condition check/avoidance '< 1.0'
- if (need > 0.0 && need < 1.0) {
- cgpu->usbinfo.write_delay_count++;
- cgpu->usbinfo.total_write_delay += need;
- cgsleep_ms((unsigned int)(need * 1000.0));
- }
- }
- cgtime(&(usbdev->last_write_tv));
+ cgsleep_ms_r(&usbdev->cgt_last_write, need * 1000.0);
+ cgsleep_prepare_r(&usbdev->cgt_last_write);
usbdev->last_write_siz = siz;
}
STATS_TIMEVAL(&tv_start);
@@ -2834,23 +2786,11 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
*amount = 0;
- if (usbdev->usecps) {
- if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
- struct timeval now;
- double need;
+ if (usbdev->usecps && usbdev->last_write_siz) {
+ double need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps);
- cgtime(&now);
- need = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps) -
- tdiff(&now, &(usbdev->last_write_tv));
-
- // Simple error condition check/avoidance '< 1.0'
- if (need > 0.0 && need < 1.0) {
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += need;
- cgsleep_ms((unsigned int)(need * 1000.0));
- }
- }
+ cgsleep_ms_r(&usbdev->cgt_last_write, need * 1000.0);
}
STATS_TIMEVAL(&tv_start);
cg_rlock(&cgusb_fd_lock);
diff --git a/usbutils.h b/usbutils.h
index 287ef04..4894a9d 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -13,6 +13,8 @@
#include <libusb.h>
+#include "util.h"
+
#define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
#define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
@@ -186,7 +188,7 @@ struct cg_usb_device {
uint32_t bufsiz;
uint32_t bufamt;
uint16_t PrefPacketSize;
- struct timeval last_write_tv;
+ cgtimer_t cgt_last_write;
size_t last_write_siz;
};