Browse Source

Spawn a new thread for cmd-idle rather than relying on problematic pthread timedwait

pthreads-win32 uses GetSystemTimeAsFileTime for *_timedwait (UTC) and has no clock_* at all
winpthreads (mingw-w64) uses _ftime for *_timedwait (local time) and GetSystemTimeAsFileTime for clock_*
Luke Dashjr 11 years ago
parent
commit
07aa023afa
1 changed files with 23 additions and 5 deletions
  1. 23 5
      miner.c

+ 23 - 5
miner.c

@@ -8554,10 +8554,26 @@ static void pool_resus(struct pool *pool)
 		applog(LOG_INFO, "Pool %d %s alive", pool->pool_no, pool->rpc_url);
 }
 
+static
+void *cmd_idle_thread(void * const __maybe_unused userp)
+{
+	pthread_detach(pthread_self());
+	RenameThread("cmd-idle");
+	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+	
+	sleep(opt_log_interval);
+	pthread_testcancel();
+	run_cmd(cmd_idle);
+	
+	return NULL;
+}
+
 static struct work *hash_pop(void)
 {
 	struct work *work = NULL, *tmp;
 	int hc;
+	bool did_cmd_idle = false;
+	pthread_t cmd_idle_thr;
 
 retry:
 	mutex_lock(stgd_lock);
@@ -8576,14 +8592,16 @@ retry:
 			no_work = true;
 		}
 		pthread_cond_signal(&gws_cond);
-		const struct timeval tv = { .tv_sec = opt_log_interval, };
-		if (ETIMEDOUT == bfg_cond_timedwait(&getq->cond, stgd_lock, &tv))
+		
+		if (cmd_idle && !did_cmd_idle)
 		{
-			run_cmd(cmd_idle);
-			pthread_cond_signal(&gws_cond);
-			pthread_cond_wait(&getq->cond, stgd_lock);
+			if (likely(!pthread_create(&cmd_idle_thr, NULL, cmd_idle_thread, NULL)))
+				did_cmd_idle = true;
 		}
+		pthread_cond_wait(&getq->cond, stgd_lock);
 	}
+	if (did_cmd_idle)
+		pthread_cancel(cmd_idle_thr);
 	
 	no_work = false;