Use a queueing bool set under control_lock to prevent multiple calls to queue_request racing.
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 a3d22b0..a186ff1 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2428,7 +2428,7 @@ static void subtract_queued(int work_units)
mutex_unlock(&qd_lock);
}
-static int discard_stale(void)
+static void discard_stale(void)
{
struct work *work, *tmp;
int stale = 0, nonclone = 0;
@@ -2451,21 +2451,18 @@ static int discard_stale(void)
/* Dec queued outside the loop to not have recursive locks */
subtract_queued(nonclone);
-
- return stale;
}
static bool queue_request(struct thr_info *thr, bool needed);
static void restart_threads(void)
{
- int i, stale;
+ int i;
/* Discard staged work that is now stale */
- stale = discard_stale();
+ discard_stale();
- for (i = 0; i < stale; i++)
- queue_request(NULL, true);
+ queue_request(NULL, true);
for (i = 0; i < mining_threads; i++)
work_restart[i].restart = 1;
@@ -3527,12 +3524,41 @@ static void pool_resus(struct pool *pool)
static time_t requested_tv_sec;
+static bool control_tset(bool *var)
+{
+ bool ret;
+
+ mutex_lock(&control_lock);
+ ret = *var;
+ *var = true;
+ mutex_unlock(&control_lock);
+
+ return ret;
+}
+
+static void control_tclear(bool *var)
+{
+ mutex_lock(&control_lock);
+ *var = false;
+ mutex_unlock(&control_lock);
+}
+
+static bool queueing;
+
static bool queue_request(struct thr_info *thr, bool needed)
{
- int toq, rq = requests_queued(), rs = requests_staged();
struct workio_cmd *wc;
struct timeval now;
time_t scan_post;
+ int toq, rq, rs;
+ bool ret = true;
+
+ /* Prevent multiple requests being executed at once */
+ if (control_tset(&queueing))
+ return ret;
+
+ rq = requests_queued();
+ rs = requests_staged();
/* Grab more work every 2/3 of the scan time to avoid all work expiring
* at the same time */
@@ -3547,7 +3573,7 @@ static bool queue_request(struct thr_info *thr, bool needed)
if ((rq >= mining_threads || rs >= mining_threads) &&
rq > staged_extras + opt_queue &&
now.tv_sec - requested_tv_sec < scan_post)
- return true;
+ goto out;
requested_tv_sec = now.tv_sec;
@@ -3563,14 +3589,12 @@ static bool queue_request(struct thr_info *thr, bool needed)
wc = calloc(1, sizeof(*wc));
if (unlikely(!wc)) {
applog(LOG_ERR, "Failed to calloc wc in queue_request");
- return false;
+ ret = false;
+ break;
}
wc->cmd = WC_GET_WORK;
- if (thr)
- wc->thr = thr;
- else
- wc->thr = NULL;
+ wc->thr = thr;
/* If we're queueing work faster than we can stage it, consider the
* system lagging and allow work to be gathered from another pool if
@@ -3584,12 +3608,16 @@ static bool queue_request(struct thr_info *thr, bool needed)
if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
applog(LOG_ERR, "Failed to tq_push in queue_request");
workio_cmd_free(wc);
- return false;
+ ret = false;
+ break;
}
} while (--toq > 0);
- return true;
+out:
+ control_tclear(&queueing);
+
+ return ret;
}
static struct work *hash_pop(const struct timespec *abstime)