3131
3232#include "opal_config.h"
3333
34- #include <errno.h>
3534#include <stdio.h>
35+ #include <string.h>
3636
3737#include "opal/class/opal_object.h"
38+ #include "opal/constants.h"
3839#include "opal/mca/threads/argobots/threads_argobots.h"
3940#include "opal/mca/threads/mutex.h"
40- #include "opal/sys/atomic.h"
4141#include "opal/util/output.h"
4242
4343BEGIN_C_DECLS
4444
45- struct opal_mutex_t {
46- opal_object_t super ;
45+ typedef ABT_mutex_memory opal_thread_internal_mutex_t ;
4746
48- ABT_mutex_memory m_lock_argobots ;
49- int m_recursive ;
47+ #define OPAL_THREAD_INTERNAL_MUTEX_INITIALIZER ABT_MUTEX_INITIALIZER
48+ #define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER ABT_RECURSIVE_MUTEX_INITIALIZER
5049
51- #if OPAL_ENABLE_DEBUG
52- int m_lock_debug ;
53- const char * m_lock_file ;
54- int m_lock_line ;
55- #endif
56-
57- opal_atomic_lock_t m_lock_atomic ;
58- };
59-
60- OPAL_DECLSPEC OBJ_CLASS_DECLARATION (opal_mutex_t );
61- OPAL_DECLSPEC OBJ_CLASS_DECLARATION (opal_recursive_mutex_t );
62-
63- #if OPAL_ENABLE_DEBUG
64- # define OPAL_MUTEX_STATIC_INIT \
65- { \
66- .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), .m_lock_argobots = ABT_MUTEX_INITIALIZER, \
67- .m_recursive = 0, .m_lock_debug = 0, .m_lock_file = NULL, .m_lock_line = 0, \
68- .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
69- }
70- #else
71- # define OPAL_MUTEX_STATIC_INIT \
72- { \
73- .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), .m_lock_argobots = ABT_MUTEX_INITIALIZER, \
74- .m_recursive = 0, .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
75- }
76- #endif
50+ static inline int opal_thread_internal_mutex_init (opal_thread_internal_mutex_t * p_mutex ,
51+ bool recursive )
52+ {
53+ if (recursive ) {
54+ const ABT_mutex_memory init_mutex = ABT_RECURSIVE_MUTEX_INITIALIZER ;
55+ memcpy (p_mutex , & init_mutex , sizeof (ABT_mutex_memory ));
56+ } else {
57+ const ABT_mutex_memory init_mutex = ABT_MUTEX_INITIALIZER ;
58+ memcpy (p_mutex , & init_mutex , sizeof (ABT_mutex_memory ));
59+ }
60+ return OPAL_SUCCESS ;
61+ }
7762
63+ static inline void opal_thread_internal_mutex_lock (opal_thread_internal_mutex_t * p_mutex )
64+ {
65+ ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (p_mutex );
7866#if OPAL_ENABLE_DEBUG
79- # define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
80- { \
81- .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
82- .m_lock_argobots = ABT_RECURSIVE_MUTEX_INITIALIZER, .m_recursive = 1, \
83- .m_lock_debug = 0, .m_lock_file = NULL, .m_lock_line = 0, \
84- .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
85- }
67+ int ret = ABT_mutex_lock (mutex );
68+ if (ABT_SUCCESS != ret ) {
69+ opal_output (0 , "opal_thread_internal_mutex_lock()" );
70+ }
8671#else
87- # define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
88- { \
89- .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
90- .m_lock_argobots = ABT_RECURSIVE_MUTEX_INITIALIZER, .m_recursive = 1, \
91- .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
92- }
72+ ABT_mutex_lock (mutex );
9373#endif
74+ }
9475
95- /************************************************************************
96- *
97- * mutex operations (non-atomic versions)
98- *
99- ************************************************************************/
100-
101- static inline int opal_mutex_trylock (opal_mutex_t * m )
76+ static inline int opal_thread_internal_mutex_trylock (opal_thread_internal_mutex_t * p_mutex )
10277{
103- ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (& m -> m_lock_argobots );
78+ ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (p_mutex );
10479 int ret = ABT_mutex_trylock (mutex );
10580 if (ABT_ERR_MUTEX_LOCKED == ret ) {
10681 return 1 ;
10782 } else if (ABT_SUCCESS != ret ) {
10883#if OPAL_ENABLE_DEBUG
109- opal_output (0 , "opal_mutex_trylock ()" );
84+ opal_output (0 , "opal_thread_internal_mutex_trylock ()" );
11085#endif
11186 return 1 ;
87+ } else {
88+ return 0 ;
11289 }
113- return 0 ;
11490}
11591
116- static inline void opal_mutex_lock ( opal_mutex_t * m )
92+ static inline void opal_thread_internal_mutex_unlock ( opal_thread_internal_mutex_t * p_mutex )
11793{
118- ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (& m -> m_lock_argobots );
119- #if OPAL_ENABLE_DEBUG
120- int ret = ABT_mutex_lock (mutex );
121- if (ABT_SUCCESS != ret ) {
122- opal_output (0 , "opal_mutex_lock()" );
123- }
124- #else
125- ABT_mutex_lock (mutex );
126- #endif
127- }
128-
129- static inline void opal_mutex_unlock (opal_mutex_t * m )
130- {
131- ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (& m -> m_lock_argobots );
94+ ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (p_mutex );
13295#if OPAL_ENABLE_DEBUG
13396 int ret = ABT_mutex_unlock (mutex );
13497 if (ABT_SUCCESS != ret ) {
135- opal_output (0 , "opal_mutex_unlock ()" );
98+ opal_output (0 , "opal_thread_internal_mutex_unlock ()" );
13699 }
137100#else
138101 ABT_mutex_unlock (mutex );
@@ -141,65 +104,62 @@ static inline void opal_mutex_unlock(opal_mutex_t *m)
141104 ABT_thread_yield ();
142105}
143106
144- /************************************************************************
145- *
146- * mutex operations (atomic versions)
147- *
148- ************************************************************************/
107+ static inline void opal_thread_internal_mutex_destroy (opal_thread_internal_mutex_t * p_mutex )
108+ {
109+ /* No specific operation is needed to destroy opal_thread_internal_mutex_t. */
110+ }
149111
150- #if OPAL_HAVE_ATOMIC_SPINLOCKS
112+ typedef ABT_cond_memory opal_thread_internal_cond_t ;
151113
152- /************************************************************************
153- * Spin Locks
154- ************************************************************************/
114+ #define OPAL_THREAD_INTERNAL_COND_INITIALIZER ABT_COND_INITIALIZER
155115
156- static inline int opal_mutex_atomic_trylock ( opal_mutex_t * m )
116+ static inline int opal_thread_internal_cond_init ( opal_thread_internal_cond_t * p_cond )
157117{
158- return opal_atomic_trylock (& m -> m_lock_atomic );
118+ const ABT_cond_memory init_cond = ABT_COND_INITIALIZER ;
119+ memcpy (p_cond , & init_cond , sizeof (ABT_cond_memory ));
120+ return OPAL_SUCCESS ;
159121}
160122
161- static inline void opal_mutex_atomic_lock (opal_mutex_t * m )
123+ static inline void opal_thread_internal_cond_wait (opal_thread_internal_cond_t * p_cond ,
124+ opal_thread_internal_mutex_t * p_mutex )
162125{
163- opal_atomic_lock (& m -> m_lock_atomic );
126+ ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (p_mutex );
127+ ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE (p_cond );
128+ #if OPAL_ENABLE_DEBUG
129+ int ret = ABT_cond_wait (cond , mutex );
130+ assert (ABT_SUCCESS == ret );
131+ #else
132+ ABT_cond_wait (cond , mutex );
133+ #endif
164134}
165135
166- static inline void opal_mutex_atomic_unlock ( opal_mutex_t * m )
136+ static inline void opal_thread_internal_cond_broadcast ( opal_thread_internal_cond_t * p_cond )
167137{
168- opal_atomic_unlock (& m -> m_lock_atomic );
169- }
170-
138+ ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE (p_cond );
139+ #if OPAL_ENABLE_DEBUG
140+ int ret = ABT_cond_broadcast (cond );
141+ assert (ABT_SUCCESS == ret );
171142#else
172-
173- /************************************************************************
174- * Standard locking
175- ************************************************************************/
176-
177- static inline int opal_mutex_atomic_trylock (opal_mutex_t * m )
178- {
179- return opal_mutex_trylock (m );
143+ ABT_cond_broadcast (cond );
144+ #endif
180145}
181146
182- static inline void opal_mutex_atomic_lock ( opal_mutex_t * m )
147+ static inline void opal_thread_internal_cond_signal ( opal_thread_internal_cond_t * p_cond )
183148{
184- opal_mutex_lock (m );
149+ ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE (p_cond );
150+ #if OPAL_ENABLE_DEBUG
151+ int ret = ABT_cond_signal (cond );
152+ assert (ABT_SUCCESS == ret );
153+ #else
154+ ABT_cond_signal (cond );
155+ #endif
185156}
186157
187- static inline void opal_mutex_atomic_unlock ( opal_mutex_t * m )
158+ static inline void opal_thread_internal_cond_destroy ( opal_thread_internal_cond_t * p_cond )
188159{
189- opal_mutex_unlock ( m );
160+ /* No destructor is needed. */
190161}
191162
192- #endif
193-
194- typedef ABT_cond_memory opal_cond_t ;
195- #define OPAL_CONDITION_STATIC_INIT ABT_COND_INITIALIZER
196-
197- int opal_cond_init (opal_cond_t * cond );
198- int opal_cond_wait (opal_cond_t * cond , opal_mutex_t * lock );
199- int opal_cond_broadcast (opal_cond_t * cond );
200- int opal_cond_signal (opal_cond_t * cond );
201- int opal_cond_destroy (opal_cond_t * cond );
202-
203163END_C_DECLS
204164
205165#endif /* OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H */
0 commit comments