Skip to content

Commit

Permalink
Add support for creating recursive mutices.
Browse files Browse the repository at this point in the history
Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Mar 27, 2024
1 parent 0dd845d commit 1fbb458
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/H5FDsubfiling/H5FDioc_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ initialize_ioc_threads(void *_sf_context)
t_start = MPI_Wtime();
#endif

if (H5TS_mutex_init(&ioc_data->io_queue.q_mutex) < 0)
if (H5TS_mutex_init(&ioc_data->io_queue.q_mutex, H5TS_MUTEX_TYPE_PLAIN) < 0)
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, (-1), "can't initialize IOC thread queue mutex");

/* Allow experimentation with the number of helper threads */
Expand Down
2 changes: 1 addition & 1 deletion src/H5FDsubfiling/H5subfiling_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ init_subfiling_context(subfiling_context_t *sf_context, const char *base_filenam

sf_context->h5_file_id = file_id;
sf_context->threads_inited = false;
if (H5TS_mutex_init(&sf_context->mutex) < 0)
if (H5TS_mutex_init(&sf_context->mutex, H5TS_MUTEX_TYPE_PLAIN) < 0)
return FAIL;
sf_context->file_ref = 0;
sf_context->sf_fids = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/H5TSatomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ H5TS_atomic_init_int(H5TS_atomic_int_t *obj, int desired)
FUNC_ENTER_NOAPI_NAMECHECK_ONLY

/* Initialize mutex that protects the "atomic" value */
(void) H5TS_mutex_init(&obj->mutex);
(void) H5TS_mutex_init(&obj->mutex, H5TS_MUTEX_TYPE_PLAIN);

/* Set the value */
obj->value = desired;
Expand Down Expand Up @@ -247,7 +247,7 @@ H5TS_atomic_init_uint(H5TS_atomic_uint_t *obj, unsigned desired)
FUNC_ENTER_NOAPI_NAMECHECK_ONLY

/* Initialize mutex that protects the "atomic" value */
(void) H5TS_mutex_init(&obj->mutex);
(void) H5TS_mutex_init(&obj->mutex, H5TS_MUTEX_TYPE_PLAIN);

/* Set the value */
obj->value = desired;
Expand Down
2 changes: 1 addition & 1 deletion src/H5TSexlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ H5TS__ex_lock_init(H5TS_ex_lock_t *lock)

/* Initialize the lock */
memset(lock, 0, sizeof(*lock));
if (H5_UNLIKELY(H5TS_mutex_init(&lock->mutex) < 0))
if (H5_UNLIKELY(H5TS_mutex_init(&lock->mutex, H5TS_MUTEX_TYPE_PLAIN) < 0))
HGOTO_DONE(FAIL);
if (H5_UNLIKELY(H5TS_cond_init(&lock->cond_var) < 0))
HGOTO_DONE(FAIL);
Expand Down
2 changes: 1 addition & 1 deletion src/H5TSint.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ H5TS__tinfo_init(void)
FUNC_ENTER_PACKAGE_NAMECHECK_ONLY

/* Initialize the critical section for modifying the thread info globals */
H5TS_mutex_init(&H5TS_tinfo_mtx_s);
H5TS_mutex_init(&H5TS_tinfo_mtx_s, H5TS_MUTEX_TYPE_PLAIN);

/* Initialize key for thread-specific API contexts */
#ifdef H5_HAVE_WIN_THREADS
Expand Down
28 changes: 23 additions & 5 deletions src/H5TSmutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@
*-------------------------------------------------------------------------
*/
herr_t
H5TS_mutex_init(H5TS_mutex_t *mutex)
H5TS_mutex_init(H5TS_mutex_t *mutex, int type)
{
herr_t ret_value = SUCCEED;

FUNC_ENTER_NOAPI_NAMECHECK_ONLY

if (H5_UNLIKELY(mtx_init(mutex, mtx_plain) != thrd_success))
if (H5_UNLIKELY(mtx_init(mutex, type) != thrd_success))
HGOTO_DONE(FAIL);

done:
Expand Down Expand Up @@ -185,12 +185,14 @@ H5TS_mutex_destroy(H5TS_mutex_t *mutex)
*
* Purpose: Initialize a H5TS_mutex_t (does not allocate it)
*
* Note: All Windows CriticalSections are recursive
*
* Return: Non-negative on success / Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5TS_mutex_init(H5TS_mutex_t *mutex)
H5TS_mutex_init(H5TS_mutex_t *mutex, int H5_ATTR_UNUSED type)
{
FUNC_ENTER_NOAPI_NAMECHECK_ONLY

Expand Down Expand Up @@ -286,16 +288,32 @@ H5TS_mutex_destroy(H5TS_mutex_t *mutex)
*-------------------------------------------------------------------------
*/
herr_t
H5TS_mutex_init(H5TS_mutex_t *mutex)
H5TS_mutex_init(H5TS_mutex_t *mutex, int type)
{
pthread_mutexattr_t _attr;
pthread_mutexattr_t *attr = NULL;
herr_t ret_value = SUCCEED;

FUNC_ENTER_NOAPI_NAMECHECK_ONLY

if (H5_UNLIKELY(pthread_mutex_init(mutex, NULL)))
/* Set up recursive mutex, if requested */
if (H5TS_MUTEX_TYPE_RECURSIVE == type) {
if (H5_UNLIKELY(pthread_mutexattr_init(&_attr)))
HGOTO_DONE(FAIL);
attr = &_attr;

if (H5_UNLIKELY(pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE)))
HGOTO_DONE(FAIL);
}

if (H5_UNLIKELY(pthread_mutex_init(mutex, attr)))
HGOTO_DONE(FAIL);

done:
if (NULL != attr)
if (H5_UNLIKELY(pthread_mutexattr_destroy(attr)))
ret_value = FAIL;

FUNC_LEAVE_NOAPI_NAMECHECK_ONLY(ret_value)
} /* end H5TS_mutex_init() */

Expand Down
2 changes: 1 addition & 1 deletion src/H5TSpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ H5TS_pool_create(H5TS_pool_t **pool, unsigned num_threads)

/* Initialize pool fields to defaults */
memset(new_pool, 0, sizeof(*new_pool));
if (H5_UNLIKELY(H5TS_mutex_init(&new_pool->mutex) < 0))
if (H5_UNLIKELY(H5TS_mutex_init(&new_pool->mutex, H5TS_MUTEX_TYPE_PLAIN) < 0))
HGOTO_DONE(FAIL);
if (H5_UNLIKELY(H5TS_cond_init(&new_pool->cond) < 0))
HGOTO_DONE(FAIL);
Expand Down
14 changes: 13 additions & 1 deletion src/H5TSprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#define H5TS_thread_equal(t1, t2) thrd_equal((t1), (t2))
#define H5TS_THREAD_RETURN_TYPE H5TS_thread_ret_t
#define H5TS_THREAD_CANCEL_DISABLE 0

/* Mutex macros */
#define H5TS_MUTEX_TYPE_PLAIN mtx_plain
#define H5TS_MUTEX_TYPE_RECURSIVE (mtx_plain | mtx_recursive)
#else
#ifdef H5_HAVE_WIN_THREADS
/* Static initialization values */
Expand All @@ -57,6 +61,10 @@
#define H5TS_thread_equal(t1, t2) (GetThreadId(t1) == GetThreadId(t2))
#define H5TS_THREAD_RETURN_TYPE H5TS_thread_ret_t WINAPI
#define H5TS_THREAD_CANCEL_DISABLE 0

/* Mutex macros */
#define H5TS_MUTEX_TYPE_PLAIN 0
#define H5TS_MUTEX_TYPE_RECURSIVE 1
#else
/* Static initialization values */
#define H5TS_ONCE_INITIALIZER PTHREAD_ONCE_INIT
Expand All @@ -66,6 +74,10 @@
#define H5TS_thread_equal(t1, t2) pthread_equal((t1), (t2))
#define H5TS_THREAD_RETURN_TYPE H5TS_thread_ret_t
#define H5TS_THREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE

/* Mutex macros */
#define H5TS_MUTEX_TYPE_PLAIN 0
#define H5TS_MUTEX_TYPE_RECURSIVE 1
#endif
#endif

Expand Down Expand Up @@ -169,7 +181,7 @@ H5_DLL struct H5E_t *H5TS_get_err_stack(void);
H5_DLL herr_t H5TS_once(H5TS_once_t *once, H5TS_once_init_func_t func);

/* Mutex operations */
H5_DLL herr_t H5TS_mutex_init(H5TS_mutex_t *mutex);
H5_DLL herr_t H5TS_mutex_init(H5TS_mutex_t *mutex, int type);
H5_DLL herr_t H5TS_mutex_lock(H5TS_mutex_t *mutex) H5TS_ACQUIRE(*mutex);
H5_DLL herr_t H5TS_mutex_try_lock(H5TS_mutex_t *mutex, bool *acquired) H5TS_TRY_ACQUIRE(SUCCEED, *mutex);
H5_DLL herr_t H5TS_mutex_unlock(H5TS_mutex_t *mutex) H5TS_RELEASE(*mutex);
Expand Down
2 changes: 1 addition & 1 deletion src/H5TSrwlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ H5TS__rw_lock_init(H5TS_rw_lock_t *rw_lock)
/* Initialize the lock */
memset(rw_lock, 0, sizeof(*rw_lock));
HDcompile_assert(H5TS_RW_LOCK_UNUSED == 0);
if (H5_UNLIKELY(H5TS_mutex_init(&rw_lock->mutex) < 0))
if (H5_UNLIKELY(H5TS_mutex_init(&rw_lock->mutex, H5TS_MUTEX_TYPE_PLAIN) < 0))
HGOTO_DONE(FAIL);
if (H5_UNLIKELY(H5TS_cond_init(&rw_lock->writers_cv) < 0))
HGOTO_DONE(FAIL);
Expand Down
2 changes: 1 addition & 1 deletion test/ttsafe_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ tts_error(void)
expected_g[10].maj_num = H5E_LINK;
expected_g[10].min_num = H5E_EXISTS;

status = H5TS_mutex_init(&error_mutex_g);
status = H5TS_mutex_init(&error_mutex_g, H5TS_MUTEX_TYPE_PLAIN);
CHECK_I(status, "H5TS_mutex_init");

def_fapl = H5Pcreate(H5P_FILE_ACCESS);
Expand Down
2 changes: 1 addition & 1 deletion test/ttsafe_thread_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ tts_thread_id(void)
int i;
herr_t result;

result = H5TS_mutex_init(&used_lock);
result = H5TS_mutex_init(&used_lock, H5TS_MUTEX_TYPE_PLAIN);
CHECK_I(result, "H5TS_mutex_lock");
result = H5TS__barrier_init(&barrier, NTHREADS);
CHECK_I(result, "H5TS__barrier_init");
Expand Down
2 changes: 1 addition & 1 deletion test/ttsafe_thread_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ tts_thread_pool(void)
herr_t result;

/* Initialize the counter */
result = H5TS_mutex_init(&counter_g.mutex);
result = H5TS_mutex_init(&counter_g.mutex, H5TS_MUTEX_TYPE_PLAIN);
CHECK_I(result, "H5TS_mutex_init");
counter_g.val = 0;

Expand Down

0 comments on commit 1fbb458

Please sign in to comment.