Skip to content

Commit

Permalink
Start the worker only at the end of init_all
Browse files Browse the repository at this point in the history
Having multiple threads mess around before everything has been
initialized is fishy to beging with. In particular the worker will
access the cache which will access options which might not yet have been
loaded.
  • Loading branch information
mahkoh committed Sep 26, 2016
1 parent cb77d3d commit b220a51
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion ui_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -2332,8 +2332,8 @@ static void init_all(void)

lib_init();
searchable = tree_searchable;
pl_init();
cmus_init();
pl_init();
browser_init();
filters_init();
help_init();
Expand Down Expand Up @@ -2376,6 +2376,8 @@ static void init_all(void)

cmus_add(lib_add_track, lib_autosave_filename, FILE_TYPE_PL,
JOB_TYPE_LIB, 0, NULL);

worker_start();
}

static void exit_all(void)
Expand Down
25 changes: 20 additions & 5 deletions worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ struct worker_job {
void *data;
};

enum worker_state {
WORKER_PAUSED,
WORKER_RUNNING,
WORKER_STOPPED,
};

static LIST_HEAD(worker_job_head);
static pthread_mutex_t worker_mutex = CMUS_MUTEX_INITIALIZER;
static pthread_cond_t worker_cond = PTHREAD_COND_INITIALIZER;
static pthread_t worker_thread;
static int running = 1;
static enum worker_state state = WORKER_PAUSED;
static int cancel_current = 0;

/*
Expand All @@ -59,10 +65,10 @@ static void *worker_loop(void *arg)

worker_lock();
while (1) {
if (list_empty(&worker_job_head)) {
if (state != WORKER_RUNNING || list_empty(&worker_job_head)) {
int rc;

if (!running)
if (state == WORKER_STOPPED)
break;

rc = pthread_cond_wait(&worker_cond, &worker_mutex);
Expand Down Expand Up @@ -103,13 +109,22 @@ void worker_init(void)
BUG_ON(rc);
}

void worker_exit(void)
static void worker_set_state(enum worker_state s)
{
worker_lock();
running = 0;
state = s;
pthread_cond_signal(&worker_cond);
worker_unlock();
}

void worker_start(void)
{
worker_set_state(WORKER_RUNNING);
}

void worker_exit(void)
{
worker_set_state(WORKER_STOPPED);
pthread_join(worker_thread, NULL);
}

Expand Down
1 change: 1 addition & 0 deletions worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
typedef int (*worker_match_cb)(uint32_t type, void *job_data, void *opaque);

void worker_init(void);
void worker_start(void);
void worker_exit(void);

void worker_add_job(uint32_t type, void (*job_cb)(void *job_data),
Expand Down

0 comments on commit b220a51

Please sign in to comment.