Skip to content

Commit 03d50a2

Browse files
committed
opal: add recursive mutex
This new class is the same as the opal_mutex_t class but has a different constructor. This constructor adds the recursive flag to the mutex attributes for the lock. This class can be used where there may be re-enty into the lock from within the same thread. master commit open-mpi/ompi@54998e5 Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 8f8590e commit 03d50a2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

opal/threads/mutex.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,29 @@ OBJ_CLASS_INSTANCE(opal_mutex_t,
7272
opal_object_t,
7373
opal_mutex_construct,
7474
opal_mutex_destruct);
75+
76+
static void opal_recursive_mutex_construct(opal_recursive_mutex_t *m)
77+
{
78+
pthread_mutexattr_t attr;
79+
pthread_mutexattr_init(&attr);
80+
81+
#if OPAL_ENABLE_DEBUG
82+
m->m_lock_debug = 0;
83+
m->m_lock_file = NULL;
84+
m->m_lock_line = 0;
85+
#endif
86+
87+
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
88+
89+
pthread_mutex_init(&m->m_lock_pthread, &attr);
90+
pthread_mutexattr_destroy(&attr);
91+
92+
#if OPAL_HAVE_ATOMIC_SPINLOCKS
93+
opal_atomic_init( &m->m_lock_atomic, OPAL_ATOMIC_UNLOCKED );
94+
#endif
95+
}
96+
97+
OBJ_CLASS_INSTANCE(opal_recursive_mutex_t,
98+
opal_object_t,
99+
opal_recursive_mutex_construct,
100+
opal_mutex_destruct);

opal/threads/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ OPAL_DECLSPEC extern bool opal_uses_threads;
5050
* Opaque mutex object
5151
*/
5252
typedef struct opal_mutex_t opal_mutex_t;
53-
53+
typedef struct opal_mutex_t opal_recursive_mutex_t;
5454

5555
/**
5656
* Try to acquire a mutex.

opal/threads/mutex_unix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct opal_mutex_t {
6161
opal_atomic_lock_t m_lock_atomic;
6262
};
6363
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
64+
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_recursive_mutex_t);
6465

6566
/************************************************************************
6667
*

0 commit comments

Comments
 (0)