Abstract out the longpoll start and explicitly restart it on pool change.
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
diff --git a/main.c b/main.c
index 48a902e..ab1fff7 100644
--- a/main.c
+++ b/main.c
@@ -1245,6 +1245,8 @@ static struct pool *priority_pool(int choice)
return ret;
}
+static void restart_longpoll(void);
+
static void switch_pools(struct pool *selected)
{
struct pool *pool, *last_pool;
@@ -1297,8 +1299,10 @@ static void switch_pools(struct pool *selected)
pool = currentpool;
pthread_mutex_unlock(&control_lock);
- if (pool != last_pool)
+ if (pool != last_pool) {
applog(LOG_WARNING, "Switching to %s", pool->rpc_url);
+ restart_longpoll();
+ }
/* Reset the queued amount to allow more to be queued for the new pool */
pthread_mutex_lock(&qd_lock);
@@ -2582,12 +2586,10 @@ static void *longpoll_thread(void *userdata)
goto out;
}
- /* Longpoll sits waiting on next pushed url */
-next_path:
hdr_path = tq_pop(mythr->q, NULL);
if (!hdr_path) {
applog(LOG_WARNING, "No long-poll found on this server");
- goto next_path;
+ goto out;
}
/* full URL */
@@ -2653,13 +2655,6 @@ next_path:
goto out;
}
}
-
- if (pool != current_pool()) {
- applog(LOG_WARNING, "Attempting to change longpoll servers");
- pool = current_pool();
- have_longpoll = false;
- goto next_path;
- }
}
out:
@@ -2671,6 +2666,34 @@ out:
return NULL;
}
+static void stop_longpoll(void)
+{
+ struct thr_info *thr = &thr_info[longpoll_thr_id];
+
+ tq_freeze(thr->q);
+ pthread_cancel(thr->pth);
+ have_longpoll = false;
+}
+
+static void quit(int status, const char *format, ...);
+
+static void start_longpoll(void)
+{
+ struct thr_info *thr = &thr_info[longpoll_thr_id];
+
+ tq_thaw(thr->q);
+ if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr)))
+ quit(1, "longpoll thread create failed");
+ pthread_detach(thr->pth);
+}
+
+static void restart_longpoll(void)
+{
+ stop_longpoll();
+ if (want_longpoll)
+ start_longpoll();
+}
+
static void reinit_cputhread(int thr_id)
{
struct thr_info *thr = &thr_info[thr_id];
@@ -3188,20 +3211,15 @@ int main (int argc, char *argv[])
quit(1, "workio thread create failed");
/* init longpoll thread info */
- if (want_longpoll) {
- longpoll_thr_id = mining_threads + 1;
- thr = &thr_info[longpoll_thr_id];
- thr->id = longpoll_thr_id;
- thr->q = tq_new();
- if (!thr->q)
- quit(1, "Failed to tq_new");
+ longpoll_thr_id = mining_threads + 1;
+ thr = &thr_info[longpoll_thr_id];
+ thr->id = longpoll_thr_id;
+ thr->q = tq_new();
+ if (!thr->q)
+ quit(1, "Failed to tq_new");
- /* start longpoll thread */
- if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr)))
- quit(1, "longpoll thread create failed");
- pthread_detach(thr->pth);
- } else
- longpoll_thr_id = -1;
+ if (want_longpoll)
+ start_longpoll();
if (opt_n_threads ) {
cpus = calloc(num_processors, sizeof(struct cgpu_info));