Commit 40821d1f11f77870d9d4c0bd0370cc38e3e7be9d

ckolivas 2013-10-18T10:44:56

Use cgcompletion timeouts for the unreliable shutdown functions on kill_work.

diff --git a/cgminer.c b/cgminer.c
index a9c459d..159d765 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3149,6 +3149,35 @@ static void disable_curses(void)
 }
 #endif
 
+static void kill_timeout(struct thr_info *thr)
+{
+	cg_completion_timeout(&thr_info_cancel, thr, 1000);
+}
+
+static void kill_mining(void)
+{
+	struct thr_info *thr;
+	int i;
+
+	applog(LOG_DEBUG, "Killing off mining threads");
+	/* Kill the mining threads*/
+	for (i = 0; i < mining_threads; i++) {
+		pthread_t *pth = NULL;
+
+		thr = get_thread(i);
+		if (thr && PTH(thr) != 0L)
+			pth = &thr->pth;
+		thr_info_cancel(thr);
+#ifndef WIN32
+		if (pth && *pth)
+			pthread_join(*pth, NULL);
+#else
+		if (pth && pth->p)
+			pthread_join(*pth, NULL);
+#endif
+	}
+}
+
 static void __kill_work(void)
 {
 	struct thr_info *thr;
@@ -3165,19 +3194,19 @@ static void __kill_work(void)
 	if (!opt_scrypt) {
 		applog(LOG_DEBUG, "Killing off HotPlug thread");
 		thr = &control_thr[hotplug_thr_id];
-		thr_info_cancel(thr);
+		kill_timeout(thr);
 	}
 #endif
 
 	applog(LOG_DEBUG, "Killing off watchpool thread");
 	/* Kill the watchpool thread */
 	thr = &control_thr[watchpool_thr_id];
-	thr_info_cancel(thr);
+	kill_timeout(thr);
 
 	applog(LOG_DEBUG, "Killing off watchdog thread");
 	/* Kill the watchdog thread */
 	thr = &control_thr[watchdog_thr_id];
-	thr_info_cancel(thr);
+	kill_timeout(thr);
 
 	applog(LOG_DEBUG, "Shutting down mining threads");
 	for (i = 0; i < mining_threads; i++) {
@@ -3195,43 +3224,27 @@ static void __kill_work(void)
 
 	sleep(1);
 
-	applog(LOG_DEBUG, "Killing off mining threads");
-	/* Kill the mining threads*/
-	for (i = 0; i < mining_threads; i++) {
-		pthread_t *pth = NULL;
-
-		thr = get_thread(i);
-		if (thr && PTH(thr) != 0L)
-			pth = &thr->pth;
-		thr_info_cancel(thr);
-#ifndef WIN32
-		if (pth && *pth)
-			pthread_join(*pth, NULL);
-#else
-		if (pth && pth->p)
-			pthread_join(*pth, NULL);
-#endif
-	}
+	cg_completion_timeout(&kill_mining, NULL, 3000);
 
 	applog(LOG_DEBUG, "Killing off stage thread");
 	/* Stop the others */
 	thr = &control_thr[stage_thr_id];
-	thr_info_cancel(thr);
+	kill_timeout(thr);
 
 	applog(LOG_DEBUG, "Killing off API thread");
 	thr = &control_thr[api_thr_id];
-	thr_info_cancel(thr);
+	kill_timeout(thr);
 
 #ifdef USE_USBUTILS
 	/* Release USB resources in case it's a restart
 	 * and not a QUIT */
 	if (!opt_scrypt) {
 		applog(LOG_DEBUG, "Releasing all USB devices");
-		usb_cleanup();
+		cg_completion_timeout(&usb_cleanup, NULL, 1000);
 
 		applog(LOG_DEBUG, "Killing off usbres thread");
 		thr = &control_thr[usbres_thr_id];
-		thr_info_cancel(thr);
+		kill_timeout(thr);
 	}
 #endif