From d1075e525f45d783ac38d64cc5929393946072c1 Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Sat, 15 Sep 2018 22:12:06 +0200 Subject: Mutex for the system message queue The main thread only reads and removes events from the queue. All other threads only insert events in the queue. Hence, only insertion and removal need protection. Signed-off-by: Lukas Fleischer --- src/queue.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/queue.c b/src/queue.c index 939b502..dad7056 100644 --- a/src/queue.c +++ b/src/queue.c @@ -4,6 +4,7 @@ * A queue for calcurse system messages. */ llist_t sysqueue; +static pthread_mutex_t que_mutex = PTHREAD_MUTEX_INITIALIZER; void que_init(void) { @@ -30,7 +31,9 @@ struct event *que_ins(char *mesg, time_t time, int id) ev->day = time; ev->id = id; ev->note = NULL; + pthread_mutex_lock(&que_mutex); LLIST_ADD(&sysqueue, ev); + pthread_mutex_unlock(&que_mutex); return ev; } @@ -52,11 +55,14 @@ void que_rem(void) if (!sysqueue.head) return; - else - ev = sysqueue.head->data; + ev = sysqueue.head->data; + + pthread_mutex_lock(&que_mutex); + LLIST_REMOVE(&sysqueue, sysqueue.head); + pthread_mutex_unlock(&que_mutex); + mem_free(ev->mesg); mem_free(ev); - LLIST_REMOVE(&sysqueue, sysqueue.head); } /* -- cgit v1.2.3-70-g09d2