Skip to content

Commit

Permalink
don't deadlock in demo when cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Feb 24, 2020
1 parent e72111a commit 91f59b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/demo/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <version.h>
#include <notcurses/notcurses.h>
Expand Down Expand Up @@ -148,7 +147,8 @@ int demo_render(struct notcurses* nc);
#define DEMO_RENDER(nc) { int demo_render_err = demo_render(nc); if(demo_render_err){ return demo_render_err; }}

// locked by callers to notcurses_render() and notcurses_refresh(), all internal
extern pthread_mutex_t demo_render_lock;
void lock_demo_render(void);
void unlock_demo_render(void);

// if you won't be doing things, and it's a long sleep, consider using
// demo_nanosleep(). it updates the HUD, which looks better to the user.
Expand Down
20 changes: 18 additions & 2 deletions src/demo/hud.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "demo.h"
#include <pthread.h>

// we provide a heads-up display throughout the demo, detailing the demos we're
// about to run, running, and just runned. the user can move this HUD with
// their mouse. it should always be on the top of the z-stack.
struct ncplane* hud = NULL;

pthread_mutex_t demo_render_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t demo_render_lock = PTHREAD_MUTEX_INITIALIZER;

// while the HUD is grabbed by the mouse, these are set to the position where
// the grab started. they are reset once the HUD is released.
Expand Down Expand Up @@ -340,6 +341,10 @@ int hud_completion_notify(const demoresult* result){
return 0;
}

static void unlock_mutex(void* vm){
pthread_mutex_unlock(vm);
}

// inform the HUD of an upcoming demo
int hud_schedule(const char* demoname){
elem* cure;
Expand Down Expand Up @@ -440,9 +445,20 @@ int demo_render(struct notcurses* nc){
return -1;
}
}
int ret = 0;
pthread_cleanup_push(unlock_mutex, &demo_render_lock);
// lock against a possible notcurses_refresh() on Ctrl+L
pthread_mutex_lock(&demo_render_lock);
int ret = notcurses_render(nc);
ret = notcurses_render(nc);
pthread_mutex_unlock(&demo_render_lock);
pthread_cleanup_pop(1);
return ret;
}

void lock_demo_render(void){
pthread_mutex_lock(&demo_render_lock);
}

void unlock_demo_render(void){
pthread_mutex_unlock(&demo_render_lock);
}
4 changes: 2 additions & 2 deletions src/demo/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ ultramegaok_demo(void* vnc){
}
}
if(id == 'L' && ni.ctrl){
pthread_mutex_lock(&demo_render_lock);
lock_demo_render();
notcurses_refresh(nc);
pthread_mutex_unlock(&demo_render_lock);
unlock_demo_render();
continue;
}
// if this was about the menu or HUD, pass to them, and continue
Expand Down

0 comments on commit 91f59b7

Please sign in to comment.