From 39713cc1fba008481093955f535fe26eaeae4906 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 6 Mar 2016 02:01:38 +0100 Subject: [PATCH] make xbt_mutex reentrant (the standard way) and kill xbt_rmutex --- include/xbt/xbt_os_thread.h | 8 ----- src/xbt/log.c | 24 ++++++-------- src/xbt/xbt_os_thread.c | 64 +++---------------------------------- 3 files changed, 15 insertions(+), 81 deletions(-) diff --git a/include/xbt/xbt_os_thread.h b/include/xbt/xbt_os_thread.h index 3dfac2abe5..c9033639d1 100644 --- a/include/xbt/xbt_os_thread.h +++ b/include/xbt/xbt_os_thread.h @@ -74,14 +74,6 @@ XBT_PUBLIC(void) xbt_os_mutex_acquire(xbt_os_mutex_t mutex); XBT_PUBLIC(void) xbt_os_mutex_release(xbt_os_mutex_t mutex); XBT_PUBLIC(void) xbt_os_mutex_destroy(xbt_os_mutex_t mutex); -/** \brief Thread reentrant mutex data type (opaque structure) */ -typedef struct xbt_os_rmutex_ *xbt_os_rmutex_t; - -XBT_PUBLIC(xbt_os_rmutex_t) xbt_os_rmutex_init(void); -XBT_PUBLIC(void) xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex); -XBT_PUBLIC(void) xbt_os_rmutex_release(xbt_os_rmutex_t rmutex); -XBT_PUBLIC(void) xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex); - /** \brief Thread condition data type (opaque structure) */ typedef struct xbt_os_cond_ *xbt_os_cond_t; diff --git a/src/xbt/log.c b/src/xbt/log.c index 296e594885..2019f4c769 100644 --- a/src/xbt/log.c +++ b/src/xbt/log.c @@ -24,7 +24,7 @@ #include "xbt/xbt_os_thread.h" int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */ -static xbt_os_rmutex_t log_cat_init_mutex = NULL; +static xbt_os_mutex_t log_cat_init_mutex = NULL; /** \addtogroup XBT_log * @@ -552,7 +552,7 @@ void xbt_log_preinit(void) xbt_log_default_layout = xbt_log_layout_simple_new(NULL); _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender; _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout; - log_cat_init_mutex = xbt_os_rmutex_init(); + log_cat_init_mutex = xbt_os_mutex_init(); } static void xbt_log_connect_categories(void) @@ -790,7 +790,7 @@ static void log_cat_exit(xbt_log_category_t cat) void xbt_log_postexit(void) { XBT_VERB("Exiting log"); - xbt_os_rmutex_destroy(log_cat_init_mutex); + xbt_os_mutex_destroy(log_cat_init_mutex); xbt_dynar_free(&xbt_log_settings); log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT)); } @@ -885,8 +885,7 @@ static void _xbt_log_cat_apply_set(xbt_log_category_t category, if (setting->fmt) { xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt)); - XBT_DEBUG("Apply settings for category '%s': set format to %s", - category->name, setting->fmt); + XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt); } if (setting->additivity != -1) { @@ -916,14 +915,12 @@ int _xbt_log_cat_init(xbt_log_category_t category, { #define _xbt_log_cat_init(a, b) (0) - if (log_cat_init_mutex != NULL) { - xbt_os_rmutex_acquire(log_cat_init_mutex); - } + if (log_cat_init_mutex != NULL) + xbt_os_mutex_acquire(log_cat_init_mutex); if (category->initialized) { - if (log_cat_init_mutex != NULL) { - xbt_os_rmutex_release(log_cat_init_mutex); - } + if (log_cat_init_mutex != NULL) + xbt_os_mutex_release(log_cat_init_mutex); return priority >= category->threshold; } @@ -1000,9 +997,8 @@ int _xbt_log_cat_init(xbt_log_category_t category, } category->initialized = 1; - if (log_cat_init_mutex != NULL) { - xbt_os_rmutex_release(log_cat_init_mutex); - } + if (log_cat_init_mutex != NULL) + xbt_os_mutex_release(log_cat_init_mutex); return priority >= category->threshold; #undef _xbt_log_cat_init diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index 7449725ce2..ac9df60d23 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -310,7 +310,6 @@ void xbt_os_thread_cancel(xbt_os_thread_t t) /****** mutex related functions ******/ typedef struct xbt_os_mutex_ { - /* KEEP IT IN SYNC WITH xbt_thread.c */ pthread_mutex_t m; } s_xbt_os_mutex_t; @@ -319,8 +318,12 @@ typedef struct xbt_os_mutex_ { xbt_os_mutex_t xbt_os_mutex_init(void) { + pthread_mutexattr_t Attr; + pthread_mutexattr_init(&Attr); + pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE); + xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1); - int errcode = pthread_mutex_init(&(res->m), NULL); + int errcode = pthread_mutex_init(&(res->m), &Attr); xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode)); return res; @@ -350,7 +353,6 @@ void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) /***** condition related functions *****/ typedef struct xbt_os_cond_ { - /* KEEP IT IN SYNC WITH xbt_thread.c */ pthread_cond_t c; } s_xbt_os_cond_t; @@ -536,59 +538,3 @@ void *xbt_os_thread_get_extra_data(void) xbt_os_thread_t thread = xbt_os_thread_self(); return thread ? thread->extra_data : NULL; } - -/***** reentrant mutexes *****/ -typedef struct xbt_os_rmutex_ { - xbt_os_mutex_t mutex; - xbt_os_thread_t owner; - int count; -} s_xbt_os_rmutex_t; - -xbt_os_rmutex_t xbt_os_rmutex_init(void) -{ - xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1); - rmutex->mutex = xbt_os_mutex_init(); - rmutex->owner = NULL; - rmutex->count = 0; - return rmutex; -} - -void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex) -{ - xbt_os_thread_t self = xbt_os_thread_self(); - - if (self == NULL) { - /* the thread module is not initialized yet */ - rmutex->owner = NULL; - return; - } - - if (self != rmutex->owner) { - xbt_os_mutex_acquire(rmutex->mutex); - rmutex->owner = self; - rmutex->count = 1; - } else { - rmutex->count++; - } -} - -void xbt_os_rmutex_release(xbt_os_rmutex_t rmutex) -{ - if (rmutex->owner == NULL) { - /* the thread module was not initialized */ - return; - } - - xbt_assert(rmutex->owner == xbt_os_thread_self()); - - if (--rmutex->count == 0) { - rmutex->owner = NULL; - xbt_os_mutex_release(rmutex->mutex); - } -} - -void xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex) -{ - xbt_os_mutex_destroy(rmutex->mutex); - xbt_free(rmutex); -} -- 2.20.1