Curl doesn't like multiple instances so go back to one instance.
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 175 176 177 178 179
diff --git a/cpu-miner.c b/cpu-miner.c
index 8fe545b..61f05b8 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -344,12 +344,20 @@ err_out:
return false;
}
-static bool submit_upstream_work(CURL *curl, char *hexstr)
+static bool submit_upstream_work(CURL *curl, const struct work *work)
{
+ char *hexstr = NULL;
json_t *val, *res;
char s[345];
bool rc = false;
+ /* build hex string */
+ hexstr = bin2hex(work->data, sizeof(work->data));
+ if (unlikely(!hexstr)) {
+ applog(LOG_ERR, "submit_upstream_work OOM");
+ goto out;
+ }
+
/* build JSON-RPC request */
sprintf(s,
"{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
@@ -383,6 +391,7 @@ static bool submit_upstream_work(CURL *curl, char *hexstr)
rc = true;
out:
+ free(hexstr);
return rc;
}
@@ -423,23 +432,15 @@ static void workio_cmd_free(struct workio_cmd *wc)
free(wc);
}
-static bool workio_get_work(struct workio_cmd *wc)
+static bool workio_get_work(struct workio_cmd *wc, CURL *curl)
{
struct work *ret_work;
int failures = 0;
- bool ret = false;
- CURL *curl;
ret_work = calloc(1, sizeof(*ret_work));
if (!ret_work) {
applog(LOG_ERR, "Failed to calloc ret_work in workio_get_work");
- return ret;
- }
-
- curl = curl_easy_init();
- if (unlikely(!curl)) {
- applog(LOG_ERR, "CURL initialization failed");
- return ret;
+ return false;
}
/* obtain new work from bitcoin via JSON-RPC */
@@ -447,7 +448,7 @@ static bool workio_get_work(struct workio_cmd *wc)
if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
free(ret_work);
- goto out;
+ return false;
}
/* pause, then restart work-request loop */
@@ -460,33 +461,20 @@ static bool workio_get_work(struct workio_cmd *wc)
if (unlikely(!tq_push(wc->thr->q, ret_work))) {
applog(LOG_ERR, "Failed to tq_push work in workio_get_work");
free(ret_work);
- } else
- ret = true;
+ }
-out:
- curl_easy_cleanup(curl);
- return ret;
+ return true;
}
-static void *submit_thread(void *userdata)
+static bool workio_submit_work(struct workio_cmd *wc, CURL *curl)
{
- char *hexstr = (char *)userdata;
int failures = 0;
- CURL *curl;
-
- curl = curl_easy_init();
- if (unlikely(!curl)) {
- applog(LOG_ERR, "CURL initialization failed");
- exit (1);
- }
/* submit solution to bitcoin via JSON-RPC */
- while (!submit_upstream_work(curl, hexstr)) {
+ while (!submit_upstream_work(curl, wc->u.work)) {
if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
- free(hexstr);
- curl_easy_cleanup(curl);
- exit (1);
+ return false;
}
/* pause, then restart work-request loop */
@@ -495,35 +483,6 @@ static void *submit_thread(void *userdata)
sleep(opt_fail_pause);
}
- free(hexstr);
- curl_easy_cleanup(curl);
- return NULL;
-}
-
-/* Work is submitted asynchronously by creating a thread for each submit
- * thus avoiding the mining threads having to wait till work is submitted
- * before they can continue working. */
-static bool workio_submit_work(struct workio_cmd *wc)
-{
- struct work *work;
- pthread_t thr;
- char *hexstr;
-
- work = wc->u.work;
-
- /* build hex string */
- hexstr = bin2hex(work->data, sizeof(work->data));
- if (unlikely(!hexstr)) {
- applog(LOG_ERR, "workio_submit_work OOM");
- return false;
- }
-
- if (unlikely(pthread_create(&thr, NULL, submit_thread, (void *)hexstr))) {
- applog(LOG_ERR, "Failed to create submit_thread");
- return false;
- }
- pthread_detach(thr);
-
return true;
}
@@ -531,6 +490,13 @@ static void *workio_thread(void *userdata)
{
struct thr_info *mythr = userdata;
bool ok = true;
+ CURL *curl;
+
+ curl = curl_easy_init();
+ if (unlikely(!curl)) {
+ applog(LOG_ERR, "CURL initialization failed");
+ return NULL;
+ }
while (ok) {
struct workio_cmd *wc;
@@ -545,10 +511,10 @@ static void *workio_thread(void *userdata)
/* process workio_cmd */
switch (wc->cmd) {
case WC_GET_WORK:
- ok = workio_get_work(wc);
+ ok = workio_get_work(wc, curl);
break;
case WC_SUBMIT_WORK:
- ok = workio_submit_work(wc);
+ ok = workio_submit_work(wc, curl);
break;
default: /* should never happen */
@@ -560,6 +526,7 @@ static void *workio_thread(void *userdata)
}
tq_freeze(mythr->q);
+ curl_easy_cleanup(curl);
return NULL;
}