Skip to content

Commit

Permalink
dbg: use C11 thread mutex (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored May 12, 2022
1 parent 2f1a510 commit 506b525
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/dbg/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PTHREAD
#include <stdlib.h>
#include <pthread.h>
#endif
#include <time.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_list.h>
#include <re_tmr.h>
#include <re_sys.h>
#include <re_thread.h>


#define DEBUG_MODULE "dbg"
Expand All @@ -32,37 +29,36 @@ static struct {
dbg_print_h *ph; /**< Optional print handler */
void *arg; /**< Handler argument */
FILE *f; /**< Logfile */
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex; /**< Thread locking */
#endif
} dbg = {
0,
DBG_INFO,
DBG_ANSI,
NULL,
NULL,
NULL,
#ifdef HAVE_PTHREAD
PTHREAD_MUTEX_INITIALIZER,
#endif
};

static once_flag flag = ONCE_FLAG_INIT;
static mtx_t mtx;


static void mem_lock_init(void)
{
mtx_init(&mtx, mtx_plain);
}


#ifdef HAVE_PTHREAD
static inline void dbg_lock(void)
{
pthread_mutex_lock(&dbg.mutex);
call_once(&flag, mem_lock_init);
mtx_lock(&mtx);
}


static inline void dbg_unlock(void)
{
pthread_mutex_unlock(&dbg.mutex);
mtx_unlock(&mtx);
}
#else
#define dbg_lock() /**< Stub */
#define dbg_unlock() /**< Stub */
#endif


/**
Expand Down

0 comments on commit 506b525

Please sign in to comment.