zoo: add necessary locking #835

pull/844/head
nick black 4 years ago committed by Nick Black
parent bea35fb91f
commit c8f623a426

@ -68,15 +68,19 @@ multiselector_demo(struct ncplane* n, int dimx, int y, pthread_mutex_t* lock){
}; };
channels_set_fg_alpha(&mopts.bgchannels, CELL_ALPHA_BLEND); channels_set_fg_alpha(&mopts.bgchannels, CELL_ALPHA_BLEND);
channels_set_bg_alpha(&mopts.bgchannels, CELL_ALPHA_BLEND); channels_set_bg_alpha(&mopts.bgchannels, CELL_ALPHA_BLEND);
pthread_mutex_lock(lock);
struct ncmultiselector* mselector = ncmultiselector_create(n, y, 0, &mopts); struct ncmultiselector* mselector = ncmultiselector_create(n, y, 0, &mopts);
pthread_mutex_unlock(lock);
if(mselector == NULL){ if(mselector == NULL){
return NULL; return NULL;
} }
struct ncplane* mplane = ncmultiselector_plane(mselector);
struct timespec swoopdelay; struct timespec swoopdelay;
timespec_div(&demodelay, dimx / 3, &swoopdelay); timespec_div(&demodelay, dimx / 3, &swoopdelay);
pthread_mutex_lock(lock);
struct ncplane* mplane = ncmultiselector_plane(mselector);
int length = ncplane_dim_x(mplane); int length = ncplane_dim_x(mplane);
ncplane_move_yx(mplane, y, -length); ncplane_move_yx(mplane, y, -length);
pthread_mutex_unlock(lock);
ncinput ni; ncinput ni;
for(int i = -length + 1 ; i < dimx - (length + 1) ; ++i){ for(int i = -length + 1 ; i < dimx - (length + 1) ; ++i){
if(locked_demo_render(nc, lock)){ if(locked_demo_render(nc, lock)){
@ -154,7 +158,9 @@ selector_demo(struct ncplane* n, int dimx, int y, pthread_mutex_t* lock){
}; };
channels_set_fg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND); channels_set_fg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND);
channels_set_bg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND); channels_set_bg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND);
pthread_mutex_lock(lock);
struct ncselector* selector = ncselector_create(n, y, dimx, &sopts); struct ncselector* selector = ncselector_create(n, y, dimx, &sopts);
pthread_mutex_unlock(lock);
if(selector == NULL){ if(selector == NULL){
return NULL; return NULL;
} }
@ -163,17 +169,18 @@ selector_demo(struct ncplane* n, int dimx, int y, pthread_mutex_t* lock){
timespec_div(&demodelay, dimx / 3, &swoopdelay); timespec_div(&demodelay, dimx / 3, &swoopdelay);
ncinput ni; ncinput ni;
for(int i = dimx - 1 ; i > 1 ; --i){ for(int i = dimx - 1 ; i > 1 ; --i){
if(locked_demo_render(nc, lock)){ pthread_mutex_lock(lock);
ncselector_destroy(selector, NULL); demo_render(nc);
return NULL; ncplane_move_yx(splane, y, i);
} pthread_mutex_unlock(lock);
ncplane_move_yx(splane, y, i);
char32_t wc = demo_getc(nc, &swoopdelay, &ni); char32_t wc = demo_getc(nc, &swoopdelay, &ni);
if(wc == (char32_t)-1){ if(wc == (char32_t)-1){
ncselector_destroy(selector, NULL); ncselector_destroy(selector, NULL);
return NULL; return NULL;
}else if(wc){ }else if(wc){
ncselector_offer_input(selector, &ni); pthread_mutex_lock(lock);
ncselector_offer_input(selector, &ni);
pthread_mutex_unlock(lock);
} }
} }
if(locked_demo_render(nc, lock)){ if(locked_demo_render(nc, lock)){
@ -192,7 +199,9 @@ selector_demo(struct ncplane* n, int dimx, int y, pthread_mutex_t* lock){
ncselector_destroy(selector, NULL); ncselector_destroy(selector, NULL);
return NULL; return NULL;
}else if(wc){ }else if(wc){
ncselector_offer_input(selector, &ni); pthread_mutex_lock(lock);
ncselector_offer_input(selector, &ni);
pthread_mutex_unlock(lock);
} }
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
cur = timespec_to_ns(&ts); cur = timespec_to_ns(&ts);
@ -214,12 +223,22 @@ reader_thread(void* vmarsh){
pthread_mutex_t* lock = marsh->lock; pthread_mutex_t* lock = marsh->lock;
free(marsh); free(marsh);
int x, y; int x, y;
pthread_mutex_lock(lock);
struct ncplane* rplane = ncreader_plane(reader); struct ncplane* rplane = ncreader_plane(reader);
pthread_mutex_unlock(lock);
struct timespec rowdelay;
ncplane_yx(rplane, &y, &x); ncplane_yx(rplane, &y, &x);
// FIXME move it up, add text int targrow = y / 2;
if(locked_demo_render(nc, lock)){ timespec_div(&demodelay, y - targrow, &rowdelay);
// FIXME while(y > targrow){
// FIXME add text
pthread_mutex_lock(lock);
demo_render(nc);
ncplane_move_yx(rplane, --y, x);
pthread_mutex_unlock(lock);
clock_nanosleep(CLOCK_MONOTONIC, 0, &rowdelay, NULL);
} }
// FIXME want to communicate exceptional exit vs successful run...
return NULL; return NULL;
} }
@ -242,13 +261,11 @@ reader_demo(struct notcurses* nc, pthread_t* tid, pthread_mutex_t* lock){
.physrows = READER_ROWS, .physrows = READER_ROWS,
}; };
const int x = ncplane_align(std, NCALIGN_CENTER, nopts.physcols); const int x = ncplane_align(std, NCALIGN_CENTER, nopts.physcols);
fprintf(stderr, "PUT READER AT %d/%d\n", dimy, x);
if((marsh->reader = ncreader_create(std, dimy, x, &nopts)) == NULL){ if((marsh->reader = ncreader_create(std, dimy, x, &nopts)) == NULL){
free(marsh); free(marsh);
return NULL; return NULL;
} }
struct ncreader* reader = marsh->reader; struct ncreader* reader = marsh->reader;
fprintf(stderr, "PUT READER AT %p\n", reader);
if(pthread_create(tid, NULL, reader_thread, marsh)){ if(pthread_create(tid, NULL, reader_thread, marsh)){
ncreader_destroy(marsh->reader, NULL); ncreader_destroy(marsh->reader, NULL);
free(marsh); free(marsh);
@ -288,21 +305,20 @@ int zoo_demo(struct notcurses* nc){
if(selector == NULL || mselector == NULL){ if(selector == NULL || mselector == NULL){
goto err; goto err;
} }
ncselector_destroy(selector, NULL); pthread_mutex_lock(&lock);
ncmultiselector_destroy(mselector); ncselector_destroy(selector, NULL);
locked_demo_render(nc, &lock); ncmultiselector_destroy(mselector);
demo_render(nc);
pthread_mutex_unlock(&lock);
int ret = 0; int ret = 0;
fprintf(stderr, "RET: %d\n", ret);
ret |= zap_reader(readertid, reader); ret |= zap_reader(readertid, reader);
fprintf(stderr, "RET: %d\n", ret);
ret |= pthread_mutex_destroy(&lock); ret |= pthread_mutex_destroy(&lock);
fprintf(stderr, "RET: %d\n", ret);
return ret; return ret;
err: err:
zap_reader(readertid, reader);
ncselector_destroy(selector, NULL); ncselector_destroy(selector, NULL);
ncmultiselector_destroy(mselector); ncmultiselector_destroy(mselector);
zap_reader(readertid, reader);
pthread_mutex_destroy(&lock); pthread_mutex_destroy(&lock);
return -1; return -1;
} }

Loading…
Cancel
Save