Standardise the way all non-mining threads are destroyed to make sure we can safely cancel them, freeing ram and NULLifying pointers.
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
diff --git a/main.c b/main.c
index c6abeaf..b840bce 100644
--- a/main.c
+++ b/main.c
@@ -85,7 +85,6 @@ static inline void affine_to_cpu(int id, int cpu)
enum workio_commands {
WC_GET_WORK,
WC_SUBMIT_WORK,
- WC_DIE,
};
struct workio_cmd {
@@ -2252,62 +2251,41 @@ static void print_summary(void);
void kill_work(void)
{
- struct workio_cmd *wc;
struct thr_info *thr;
unsigned int i;
disable_curses();
applog(LOG_INFO, "Received kill message");
+ if (opt_debug)
+ applog(LOG_DEBUG, "Killing off watchdog thread");
/* Kill the watchdog thread */
thr = &thr_info[watchdog_thr_id];
- if (thr && thr->pth)
- pthread_cancel(*thr->pth);
+ thr_info_cancel(thr);
+ if (opt_debug)
+ applog(LOG_DEBUG, "Killing off mining threads");
/* Stop the mining threads*/
for (i = 0; i < mining_threads; i++) {
thr = &thr_info[i];
- if (!thr)
- continue;
- if (!thr->pth)
- continue;
- if (thr->q)
- tq_freeze(thr->q);
- /* No need to check if this succeeds or not */
- pthread_cancel(*thr->pth);
+ thr_info_cancel(thr);
}
+ if (opt_debug)
+ applog(LOG_DEBUG, "Killing off stage thread");
/* Stop the others */
thr = &thr_info[stage_thr_id];
- if (thr && thr->pth)
- pthread_cancel(*thr->pth);
- thr = &thr_info[longpoll_thr_id];
- if (thr && thr->pth)
- pthread_cancel(*thr->pth);
-
- wc = calloc(1, sizeof(*wc));
- if (unlikely(!wc)) {
- applog(LOG_ERR, "Failed to calloc wc in kill_work");
- /* We're just trying to die anyway, so forget graceful */
- exit (1);
- }
-
- wc->cmd = WC_DIE;
- wc->thr = 0;
+ thr_info_cancel(thr);
if (opt_debug)
- applog(LOG_DEBUG, "Pushing die request to work thread");
+ applog(LOG_DEBUG, "Killing off longpoll thread");
+ thr = &thr_info[longpoll_thr_id];
+ thr_info_cancel(thr);
+ if (opt_debug)
+ applog(LOG_DEBUG, "Killing off work thread");
thr = &thr_info[work_thr_id];
- if (thr) {
- if (unlikely(!tq_push(thr->q, wc))) {
- applog(LOG_ERR, "Failed to tq_push work in kill_work");
- exit (1);
- }
-
- if (thr->pth)
- pthread_cancel(*thr->pth);
- }
+ thr_info_cancel(thr);
}
void quit(int status, const char *format, ...);
@@ -3315,7 +3293,6 @@ static void *workio_thread(void *userdata)
case WC_SUBMIT_WORK:
ok = workio_submit_work(wc);
break;
- case WC_DIE:
default:
ok = false;
break;
@@ -4472,9 +4449,7 @@ static void stop_longpoll(void)
{
struct thr_info *thr = &thr_info[longpoll_thr_id];
- tq_freeze(thr->q);
- if (thr->pth)
- pthread_cancel(*thr->pth);
+ thr_info_cancel(thr);
have_longpoll = false;
}
@@ -4482,7 +4457,7 @@ static void start_longpoll(void)
{
struct thr_info *thr = &thr_info[longpoll_thr_id];
- tq_thaw(thr->q);
+ tq_thaw(thr->q);
if (unlikely(thr_info_create(thr, NULL, longpoll_thread, thr)))
quit(1, "longpoll thread create failed");
if (opt_debug)
@@ -4566,10 +4541,15 @@ select_cgpu:
continue;
thr = &thr_info[thr_id];
+ if (!thr) {
+ applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
+ continue;
+ }
+
thr->rolling = thr->cgpu->rolling = 0;
/* Reports the last time we tried to revive a sick GPU */
gettimeofday(&thr->sick, NULL);
- if (!pthread_cancel(*thr->pth)) {
+ if (thr->pth && !pthread_cancel(*thr->pth)) {
applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
} else
applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
diff --git a/miner.h b/miner.h
index ce2a6b6..75c53ce 100644
--- a/miner.h
+++ b/miner.h
@@ -236,7 +236,8 @@ struct thr_info {
double rolling;
};
-extern inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
+extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
+extern void thr_info_cancel(struct thr_info *thr);
static inline uint32_t swab32(uint32_t v)
{
diff --git a/util.c b/util.c
index 5344586..aca9e81 100644
--- a/util.c
+++ b/util.c
@@ -653,18 +653,37 @@ out:
return rval;
}
-inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg)
+int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg)
{
- int ret = 0;
-
+ int ret = -1;
+
thr->pth = malloc(sizeof(pthread_t));
+ if (unlikely(!thr->pth)) {
+ applog(LOG_ERR, "Failed to malloc in thr_info_create");
+ return ret;
+ }
+
ret = pthread_create(thr->pth, attr, start, arg);
-
if (unlikely(ret)) {
+ applog(LOG_ERR, "Failed to pthread_create in thr_info_create");
free(thr->pth);
- thr->pth = 0;
+ thr->pth = NULL;
}
-
+
return ret;
}
+void thr_info_cancel(struct thr_info *thr)
+{
+ if (!thr)
+ return;
+
+ if (thr->q)
+ tq_freeze(thr->q);
+ if (thr->pth) {
+ if (pthread_cancel(*thr->pth))
+ pthread_join(*thr->pth, NULL);
+ free(thr->pth);
+ thr->pth = NULL;
+ }
+}