Make main() the getwork scheduler once everything is set up, so that all app exits use the kill_work and quit paths.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
diff --git a/cgminer.c b/cgminer.c
index 43d7dd1..b075866 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -178,9 +178,6 @@ static pthread_cond_t lp_cond;
pthread_mutex_t restart_lock;
pthread_cond_t restart_cond;
-pthread_mutex_t kill_lock;
-pthread_cond_t kill_cond;
-
pthread_cond_t gws_cond;
double total_mhashes_done;
@@ -2806,11 +2803,6 @@ static void __kill_work(void)
applog(LOG_DEBUG, "Killing off API thread");
thr = &thr_info[api_thr_id];
thr_info_cancel(thr);
-
- applog(LOG_DEBUG, "Sending kill broadcast");
- mutex_lock(&kill_lock);
- pthread_cond_signal(&kill_cond);
- mutex_unlock(&kill_lock);
}
/* This should be the common exit path */
@@ -3043,126 +3035,6 @@ static void pool_died(struct pool *pool)
}
}
-static void gen_stratum_work(struct pool *pool, struct work *work);
-
-static void pool_resus(struct pool *pool);
-
-static void *getwork_thread(void __maybe_unused *userdata)
-{
- pthread_detach(pthread_self());
-
- RenameThread("getwork_sched");
-
- while (42) {
- int ts, max_staged = opt_queue;
- struct pool *pool, *cp;
- bool lagging = false;
- struct curl_ent *ce;
- struct work *work;
-
- cp = current_pool();
-
- /* If the primary pool is a getwork pool and cannot roll work,
- * try to stage one extra work per mining thread */
- if (!cp->has_stratum && !cp->has_gbt && !staged_rollable)
- max_staged += mining_threads;
-
- mutex_lock(stgd_lock);
- ts = __total_staged();
-
- if (!cp->has_stratum && !cp->has_gbt && !ts && !opt_fail_only)
- lagging = true;
-
- /* Wait until hash_pop tells us we need to create more work */
- if (ts > max_staged) {
- pthread_cond_wait(&gws_cond, stgd_lock);
- ts = __total_staged();
- }
- mutex_unlock(stgd_lock);
-
- if (ts > max_staged)
- continue;
-
- work = make_work();
-
- if (lagging && !pool_tset(cp, &cp->lagging)) {
- applog(LOG_WARNING, "Pool %d not providing work fast enough", cp->pool_no);
- cp->getfail_occasions++;
- total_go++;
- }
- pool = select_pool(lagging);
-retry:
- if (pool->has_stratum) {
- while (!pool->stratum_active) {
- struct pool *altpool = select_pool(true);
-
- sleep(5);
- if (altpool != pool) {
- pool = altpool;
- goto retry;
- }
- }
- gen_stratum_work(pool, work);
- applog(LOG_DEBUG, "Generated stratum work");
- stage_work(work);
- continue;
- }
-
- if (pool->has_gbt) {
- while (pool->idle) {
- struct pool *altpool = select_pool(true);
-
- sleep(5);
- if (altpool != pool) {
- pool = altpool;
- goto retry;
- }
- }
- gen_gbt_work(pool, work);
- applog(LOG_DEBUG, "Generated GBT work");
- stage_work(work);
- continue;
- }
-
- if (clone_available()) {
- applog(LOG_DEBUG, "Cloned getwork work");
- free_work(work);
- continue;
- }
-
- if (opt_benchmark) {
- get_benchmark_work(work);
- applog(LOG_DEBUG, "Generated benchmark work");
- stage_work(work);
- continue;
- }
-
- work->pool = pool;
- ce = pop_curl_entry(pool);
- /* obtain new work from bitcoin via JSON-RPC */
- if (!get_upstream_work(work, ce->curl)) {
- applog(LOG_DEBUG, "Pool %d json_rpc_call failed on get work, retrying in 5s", pool->pool_no);
- /* Make sure the pool just hasn't stopped serving
- * requests but is up as we'll keep hammering it */
- if (++pool->seq_getfails > mining_threads + opt_queue)
- pool_died(pool);
- sleep(5);
- push_curl_entry(ce, pool);
- pool = select_pool(true);
- goto retry;
- }
- pool_tclear(pool, &pool->lagging);
- if (pool_tclear(pool, &pool->idle))
- pool_resus(pool);
-
- applog(LOG_DEBUG, "Generated getwork work");
- stage_work(work);
- push_curl_entry(ce, pool);
- }
-
- return NULL;
-}
-
static bool stale_work(struct work *work, bool share)
{
struct timeval now;
@@ -4651,6 +4523,8 @@ static bool cnx_needed(struct pool *pool)
}
static void wait_lpcurrent(struct pool *pool);
+static void pool_resus(struct pool *pool);
+static void gen_stratum_work(struct pool *pool, struct work *work);
/* One stratum thread per pool that has stratum waits on the socket checking
* for new messages and for the integrity of the socket connection. We reset
@@ -6368,14 +6242,13 @@ bool add_cgpu(struct cgpu_info*cgpu)
int main(int argc, char *argv[])
{
- struct block *block, *tmpblock;
- struct work *work, *tmpwork;
bool pools_active = false;
struct sigaction handler;
struct thr_info *thr;
- char *s;
+ struct block *block;
unsigned int k;
int i, j;
+ char *s;
/* This dangerous functions tramples random dynamically allocated
* variables so do it before anything at all */
@@ -6416,10 +6289,6 @@ int main(int argc, char *argv[])
if (unlikely(pthread_cond_init(&restart_cond, NULL)))
quit(1, "Failed to pthread_cond_init restart_cond");
- mutex_init(&kill_lock);
- if (unlikely(pthread_cond_init(&kill_cond, NULL)))
- quit(1, "Failed to pthread_cond_init kill_cond");
-
if (unlikely(pthread_cond_init(&gws_cond, NULL)))
quit(1, "Failed to pthread_cond_init gws_cond");
@@ -6879,36 +6748,113 @@ begin_bench:
pthread_detach(thr->pth);
#endif
- thr = &thr_info[gwsched_thr_id];
- thr->id = gwsched_thr_id;
+ /* Once everything is set up, main() becomes the getwork scheduler */
+ while (42) {
+ int ts, max_staged = opt_queue;
+ struct pool *pool, *cp;
+ bool lagging = false;
+ struct curl_ent *ce;
+ struct work *work;
- /* start getwork scheduler thread */
- if (thr_info_create(thr, NULL, getwork_thread, thr))
- quit(1, "getwork_thread create failed");
+ cp = current_pool();
- /* Wait till we receive the conditional telling us to die */
- mutex_lock(&kill_lock);
- pthread_cond_wait(&kill_cond, &kill_lock);
- mutex_unlock(&kill_lock);
+ /* If the primary pool is a getwork pool and cannot roll work,
+ * try to stage one extra work per mining thread */
+ if (!cp->has_stratum && !cp->has_gbt && !staged_rollable)
+ max_staged += mining_threads;
- applog(LOG_INFO, "Given kill message, exiting.");
+ mutex_lock(stgd_lock);
+ ts = __total_staged();
- /* Not really necessary, but let's clean this up too anyway */
- HASH_ITER(hh, staged_work, work, tmpwork) {
- HASH_DEL(staged_work, work);
- free_work(work);
- }
- HASH_ITER(hh, blocks, block, tmpblock) {
- HASH_DEL(blocks, block);
- free(block);
- }
+ if (!cp->has_stratum && !cp->has_gbt && !ts && !opt_fail_only)
+ lagging = true;
-#if defined(unix)
- if (forkpid > 0) {
- kill(forkpid, SIGTERM);
- forkpid = 0;
+ /* Wait until hash_pop tells us we need to create more work */
+ if (ts > max_staged) {
+ pthread_cond_wait(&gws_cond, stgd_lock);
+ ts = __total_staged();
+ }
+ mutex_unlock(stgd_lock);
+
+ if (ts > max_staged)
+ continue;
+
+ work = make_work();
+
+ if (lagging && !pool_tset(cp, &cp->lagging)) {
+ applog(LOG_WARNING, "Pool %d not providing work fast enough", cp->pool_no);
+ cp->getfail_occasions++;
+ total_go++;
+ }
+ pool = select_pool(lagging);
+retry:
+ if (pool->has_stratum) {
+ while (!pool->stratum_active) {
+ struct pool *altpool = select_pool(true);
+
+ sleep(5);
+ if (altpool != pool) {
+ pool = altpool;
+ goto retry;
+ }
+ }
+ gen_stratum_work(pool, work);
+ applog(LOG_DEBUG, "Generated stratum work");
+ stage_work(work);
+ continue;
+ }
+
+ if (pool->has_gbt) {
+ while (pool->idle) {
+ struct pool *altpool = select_pool(true);
+
+ sleep(5);
+ if (altpool != pool) {
+ pool = altpool;
+ goto retry;
+ }
+ }
+ gen_gbt_work(pool, work);
+ applog(LOG_DEBUG, "Generated GBT work");
+ stage_work(work);
+ continue;
+ }
+
+ if (clone_available()) {
+ applog(LOG_DEBUG, "Cloned getwork work");
+ free_work(work);
+ continue;
+ }
+
+ if (opt_benchmark) {
+ get_benchmark_work(work);
+ applog(LOG_DEBUG, "Generated benchmark work");
+ stage_work(work);
+ continue;
+ }
+
+ work->pool = pool;
+ ce = pop_curl_entry(pool);
+ /* obtain new work from bitcoin via JSON-RPC */
+ if (!get_upstream_work(work, ce->curl)) {
+ applog(LOG_DEBUG, "Pool %d json_rpc_call failed on get work, retrying in 5s", pool->pool_no);
+ /* Make sure the pool just hasn't stopped serving
+ * requests but is up as we'll keep hammering it */
+ if (++pool->seq_getfails > mining_threads + opt_queue)
+ pool_died(pool);
+ sleep(5);
+ push_curl_entry(ce, pool);
+ pool = select_pool(true);
+ goto retry;
+ }
+ pool_tclear(pool, &pool->lagging);
+ if (pool_tclear(pool, &pool->idle))
+ pool_resus(pool);
+
+ applog(LOG_DEBUG, "Generated getwork work");
+ stage_work(work);
+ push_curl_entry(ce, pool);
}
-#endif
return 0;
}