Submit all work asynchronously via a submit_work thread.
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
diff --git a/cpu-miner.c b/cpu-miner.c
index 61f05b8..1369efb 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -645,32 +645,58 @@ out:
return ret;
}
-static bool submit_work(struct thr_info *thr, const struct work *work_in)
+struct submit_data {
+ struct thr_info *thr;
+ struct work work_in;
+ pthread_t pth;
+};
+
+static void *submit_work(void *userdata)
{
+ struct submit_data *sd = (struct submit_data *)userdata;
struct workio_cmd *wc;
/* fill out work request message */
wc = calloc(1, sizeof(*wc));
- if (!wc)
- return false;
+ if (unlikely(!wc))
+ goto out;
- wc->u.work = malloc(sizeof(*work_in));
- if (!wc->u.work)
+ wc->u.work = malloc(sizeof(struct work));
+ if (unlikely(!wc->u.work))
goto err_out;
wc->cmd = WC_SUBMIT_WORK;
- wc->thr = thr;
- memcpy(wc->u.work, work_in, sizeof(*work_in));
+ wc->thr = sd->thr;
+ memcpy(wc->u.work, &sd->work_in, sizeof(struct work));
/* send solution to workio thread */
- if (!tq_push(thr_info[work_thr_id].q, wc))
+ if (unlikely(!tq_push(thr_info[work_thr_id].q, wc)))
goto err_out;
- return true;
+ goto out;
err_out:
workio_cmd_free(wc);
- return false;
+out:
+ free(sd);
+}
+
+static bool submit_work_async(struct thr_info *thr, const struct work *work_in)
+{
+ struct submit_data *sd = malloc(sizeof(struct submit_data));
+ if (unlikely(!sd)) {
+ applog(LOG_ERR, "Failed to malloc sd in submit_work_async");
+ return false;
+ }
+
+ memcpy(&sd->work_in, work_in, sizeof(struct work));
+
+ if (pthread_create(&sd->pth, NULL, submit_work, (void *)sd)) {
+ applog(LOG_ERR, "Failed to create submit_thread");
+ return false;
+ }
+ pthread_detach(sd->pth);
+ return true;
}
bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
@@ -679,7 +705,7 @@ bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
work->data[64+12+1] = (nonce>>8) & 0xff;
work->data[64+12+2] = (nonce>>16) & 0xff;
work->data[64+12+3] = (nonce>>24) & 0xff;
- return submit_work(thr, work);
+ return submit_work_async(thr, work);
}
static inline int cpu_from_thr_id(int thr_id)
@@ -798,7 +824,7 @@ static void *miner_thread(void *userdata)
/* if nonce found, submit work */
if (unlikely(rc)) {
applog(LOG_INFO, "CPU %d found something?", cpu_from_thr_id(thr_id));
- if (!submit_work(mythr, &work))
+ if (!submit_work_async(mythr, &work))
break;
}
}