Skip to content

Commit

Permalink
After cancelling a thread (unless it's detached, but mining threads a…
Browse files Browse the repository at this point in the history
…ren't), it is always necessary to join the thread so the system will release resources.

This also simplifies kill_mining a bit.
  • Loading branch information
mrbrdo committed Jul 7, 2014
1 parent e33590f commit 0bd7a9f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 18 deletions.
4 changes: 2 additions & 2 deletions driver-opencl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,9 @@ void *reinit_gpu(void *userdata)
thr->rolling = thr->cgpu->rolling = 0;
/* Reports the last time we tried to revive a sick GPU */
cgtime(&thr->sick);
if (!pthread_cancel(thr->pth)) {
if (!pthread_kill(thr->pth, 0)) {
applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
pthread_join(thr->pth, NULL);
cg_completion_timeout(&thr_info_cancel_join, thr, 5000);
thr->cgpu->drv->thread_shutdown(thr);
} else
applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
Expand Down
16 changes: 2 additions & 14 deletions sgminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,7 @@ static void disable_curses(void)

static void kill_timeout(struct thr_info *thr)
{
cg_completion_timeout(&thr_info_cancel, thr, 1000);
cg_completion_timeout(&thr_info_cancel_join, thr, 1000);
}

static void kill_mining(void)
Expand All @@ -3322,21 +3322,9 @@ static void kill_mining(void)
/* Kill the mining threads*/
rd_lock(&mining_thr_lock);
for (i = 0; i < mining_threads; i++) {
pthread_t *pth = (pthread_t *) calloc(1, sizeof(pthread_t));

thr = mining_thr[i];
if (thr && PTH(thr) != 0L)
*pth = thr->pth;
thr_info_cancel(thr);
forcelog(LOG_DEBUG, "Waiting for thread %d to finish...", thr->id);
#ifndef WIN32
if (pth && *pth)
pthread_join(*pth, NULL);
#else
if (pth && pth->p)
pthread_join(*pth, NULL);
#endif
free(pth);
thr_info_cancel_join(thr);
}
rd_unlock(&mining_thr_lock);
}
Expand Down
3 changes: 2 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,14 @@ int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (
return pthread_create(&thr->pth, attr, start, arg);
}

void thr_info_cancel(struct thr_info *thr)
void thr_info_cancel_join(struct thr_info *thr)
{
if (!thr)
return;

if (PTH(thr) != 0L) {
pthread_cancel(thr->pth);
pthread_join(thr->pth, NULL);
PTH(thr) = 0L;
}
cgsem_destroy(&thr->sem);
Expand Down
2 changes: 1 addition & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct pool;
enum dev_reason;
struct cgpu_info;
int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
void thr_info_cancel(struct thr_info *thr);
void thr_info_cancel_join(struct thr_info *thr);
void cgtime(struct timeval *tv);
void subtime(struct timeval *a, struct timeval *b);
void addtime(struct timeval *a, struct timeval *b);
Expand Down

0 comments on commit 0bd7a9f

Please sign in to comment.