clean up input thread in demo #246

pull/251/head
nick black 5 years ago committed by Nick Black
parent bd27e056e7
commit 781a0eda44

@ -397,6 +397,9 @@ int main(int argc, char** argv){
if(hud_destroy()){ if(hud_destroy()){
goto err; goto err;
} }
if(stop_input()){
goto err;
}
if(notcurses_stop(nc)){ if(notcurses_stop(nc)){
return EXIT_FAILURE; return EXIT_FAILURE;
} }

@ -6,6 +6,7 @@ typedef struct nciqueue {
struct nciqueue *next; struct nciqueue *next;
} nciqueue; } nciqueue;
static bool spawned;
static pthread_t tid; static pthread_t tid;
static nciqueue* queue; static nciqueue* queue;
static nciqueue** enqueue = &queue; static nciqueue** enqueue = &queue;
@ -35,15 +36,17 @@ char32_t demo_getc(const struct timespec* ts, ncinput* ni){
} }
pthread_cond_timedwait(&cond, &lock, &abstime); pthread_cond_timedwait(&cond, &lock, &abstime);
} }
char32_t id = queue->ni.id; nciqueue* q = queue;
if(ni){
memcpy(ni, &queue->ni, sizeof(*ni));
}
queue = queue->next; queue = queue->next;
if(queue == NULL){ if(queue == NULL){
enqueue = &queue; enqueue = &queue;
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
char32_t id = q->ni.id;
if(ni){
memcpy(ni, &q->ni, sizeof(*ni));
}
free(q);
return id; return id;
} }
@ -100,7 +103,9 @@ ultramegaok_demo(void* vnc){
// listens for events, handling mouse events directly and making other ones // listens for events, handling mouse events directly and making other ones
// available to demos // available to demos
int input_dispatcher(struct notcurses* nc){ int input_dispatcher(struct notcurses* nc){
spawned = true;
if(pthread_create(&tid, NULL, ultramegaok_demo, nc)){ if(pthread_create(&tid, NULL, ultramegaok_demo, nc)){
spawned = false;
return -1; return -1;
} }
return 0; return 0;
@ -108,7 +113,10 @@ int input_dispatcher(struct notcurses* nc){
int stop_input(void){ int stop_input(void){
int ret = 0; int ret = 0;
ret |= pthread_cancel(tid); if(spawned){
ret |= pthread_join(tid, NULL); ret |= pthread_cancel(tid);
ret |= pthread_join(tid, NULL);
spawned = false;
}
return ret; return ret;
} }

Loading…
Cancel
Save