We now are guaranteed to have one fresh work item after a block change and we should only discard staged requests.
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
diff --git a/main.c b/main.c
index ed463a6..2c9bf16 100644
--- a/main.c
+++ b/main.c
@@ -1218,45 +1218,37 @@ static bool queue_request(void)
return true;
}
-static bool discard_request(void)
+static void discard_staged(void)
{
struct work *work_heap;
/* Just in case we fell in a hole and missed a queue filling */
- if (unlikely(!requests_queued())) {
- applog(LOG_WARNING, "Tried to discard_request with nil queued");
- return true;
- }
+ if (unlikely(!requests_staged()))
+ return;
work_heap = tq_pop(getq, NULL);
- if (unlikely(!work_heap)) {
- applog(LOG_ERR, "Failed to tq_pop in discard_request");
- return false;
- }
+ if (unlikely(!work_heap))
+ return;
+
free(work_heap);
dec_queued();
discarded_work++;
- return true;
}
static void flush_requests(bool longpoll)
{
- int i, extra;
+ int i, stale;
- extra = requests_queued();
- /* When flushing from longpoll, we don't know the new work yet. When
- * not flushing from longpoll, the first work item is valid so do not
- * discard it */
+ /* We should have one fresh work item staged from the block change. */
+ stale = requests_staged() - 1;
if (longpoll)
memcpy(current_block, blank, 36);
- else
- extra--;
/* Temporarily increase the staged count so that get_work thinks there
* is work available instead of making threads reuse existing work */
inc_staged(mining_threads, true);
- for (i = 0; i < extra; i++) {
+ for (i = 0; i < stale; i++) {
/* Queue a whole batch of new requests */
if (unlikely(!queue_request())) {
applog(LOG_ERR, "Failed to queue requests in flush_requests");
@@ -1265,11 +1257,7 @@ static void flush_requests(bool longpoll)
}
/* Pop off the old requests. Cancelling the requests would be better
* but is tricky */
- if (unlikely(!discard_request())) {
- applog(LOG_ERR, "Failed to discard requests in flush_requests");
- kill_work();
- break;
- }
+ discard_staged();
}
}