extract cancel_and_join() helper

pull/2166/head
nick black 3 years ago
parent ff6066ba33
commit 613b2f9d31
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -135,8 +135,7 @@ int ncfdplane_destroy(ncfdplane* n){
n->destroyed = true; // ncfdplane_destroy_inner() is called on thread exit
}else{
void* vret = NULL;
pthread_cancel(n->tid);
ret |= pthread_join(n->tid, &vret);
ret |= cancel_and_join("fdplane", n->tid, &vret);
ret |= ncfdplane_destroy_inner(n);
}
}

@ -66,13 +66,8 @@ int gpm_read(tinfo* ti, ncinput* ni){
}
int gpm_close(tinfo* ti){
if(pthread_cancel(ti->gpmthread)){
logerror("couldn't cancel gpm thread\n"); // daemon might have died
}
void* thrres;
if(pthread_join(ti->gpmthread, &thrres)){
logerror("error joining gpm thread\n");
}
cancel_and_join("gpm", ti->gpmthread, &thrres);
Gpm_Close();
memset(&gpmconn, 0, sizeof(gpmconn));
return 0;

@ -74,6 +74,7 @@ static inline void
free_inputctx(inputctx* i){
if(i){
// we *do not* own any file descriptors or handles; don't close them!
// do not kill the thread here, either.
free(i->inputs);
free(i->csrs);
free(i);
@ -174,6 +175,12 @@ int init_inputlayer(tinfo* ti){
}
int stop_inputlayer(tinfo* ti){
// FIXME get ictx reference from ti, kill thread, free_inputctx()
if(ti){
if(ti->ictx){
// FIXME cancel + join
free_inputctx(ti->ictx);
ti->ictx = NULL;
}
}
return 0;
}

@ -1745,6 +1745,20 @@ tty_check(int fd){
return isatty(fd);
}
// attempt to cancel the specified thread (not an error if we can't; it might
// have already exited), and then join it (an error here is propagated).
static inline int
cancel_and_join(const char* name, pthread_t tid, void** res){
if(pthread_cancel(tid)){
logerror("couldn't cancel %s thread\n", name); // tid might have died
}
if(pthread_join(tid, res)){
logerror("error joining %s thread\n", name);
return -1;
}
return 0;
}
#undef ALLOC
#undef API

@ -9,12 +9,13 @@ extern "C" {
#include "version.h"
#include "builddef.h"
#include "input.h"
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <notcurses/notcurses.h>
#include "input.h"
#include "fbuf.h"
#include "in.h"
struct ncpile;
struct sprixel;
@ -199,6 +200,7 @@ typedef struct tinfo {
// we heap-allocate this one (if we use it), as it's not fully defined on Windows
struct termios *tpreserved;// terminal state upon entry
ncinputlayer input; // input layer
struct inputctx* ictx; // new input layer
// if we get a reply to our initial \e[18t cell geometry query, it will
// replace these values. note that LINES/COLUMNS cannot be used to limit

Loading…
Cancel
Save