31
31
32
32
#include "opal_config.h"
33
33
34
- #include <errno.h>
35
34
#include <stdio.h>
35
+ #include <string.h>
36
36
37
37
#include "opal/class/opal_object.h"
38
+ #include "opal/constants.h"
38
39
#include "opal/mca/threads/argobots/threads_argobots.h"
39
40
#include "opal/mca/threads/mutex.h"
40
- #include "opal/sys/atomic.h"
41
41
#include "opal/util/output.h"
42
42
43
43
BEGIN_C_DECLS
44
44
45
- struct opal_mutex_t {
46
- opal_object_t super ;
45
+ typedef ABT_mutex_memory opal_thread_internal_mutex_t ;
47
46
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
50
49
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
+ }
77
62
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 );
78
66
#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
+ }
86
71
#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 );
93
73
#endif
74
+ }
94
75
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 )
102
77
{
103
- ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (& m -> m_lock_argobots );
78
+ ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE (p_mutex );
104
79
int ret = ABT_mutex_trylock (mutex );
105
80
if (ABT_ERR_MUTEX_LOCKED == ret ) {
106
81
return 1 ;
107
82
} else if (ABT_SUCCESS != ret ) {
108
83
#if OPAL_ENABLE_DEBUG
109
- opal_output (0 , "opal_mutex_trylock ()" );
84
+ opal_output (0 , "opal_thread_internal_mutex_trylock ()" );
110
85
#endif
111
86
return 1 ;
87
+ } else {
88
+ return 0 ;
112
89
}
113
- return 0 ;
114
90
}
115
91
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 )
117
93
{
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 );
132
95
#if OPAL_ENABLE_DEBUG
133
96
int ret = ABT_mutex_unlock (mutex );
134
97
if (ABT_SUCCESS != ret ) {
135
- opal_output (0 , "opal_mutex_unlock ()" );
98
+ opal_output (0 , "opal_thread_internal_mutex_unlock ()" );
136
99
}
137
100
#else
138
101
ABT_mutex_unlock (mutex );
@@ -141,65 +104,62 @@ static inline void opal_mutex_unlock(opal_mutex_t *m)
141
104
ABT_thread_yield ();
142
105
}
143
106
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
+ }
149
111
150
- #if OPAL_HAVE_ATOMIC_SPINLOCKS
112
+ typedef ABT_cond_memory opal_thread_internal_cond_t ;
151
113
152
- /************************************************************************
153
- * Spin Locks
154
- ************************************************************************/
114
+ #define OPAL_THREAD_INTERNAL_COND_INITIALIZER ABT_COND_INITIALIZER
155
115
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 )
157
117
{
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 ;
159
121
}
160
122
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 )
162
125
{
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
164
134
}
165
135
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 )
167
137
{
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 );
171
142
#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
180
145
}
181
146
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 )
183
148
{
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
185
156
}
186
157
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 )
188
159
{
189
- opal_mutex_unlock ( m );
160
+ /* No destructor is needed. */
190
161
}
191
162
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
-
203
163
END_C_DECLS
204
164
205
165
#endif /* OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H */
0 commit comments