This source file includes following definitions.
- pmix_mutex_trylock
- pmix_mutex_lock
- pmix_mutex_unlock
- pmix_mutex_atomic_trylock
- pmix_mutex_atomic_lock
- pmix_mutex_atomic_unlock
- pmix_mutex_atomic_trylock
- pmix_mutex_atomic_lock
- pmix_mutex_atomic_unlock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #ifndef PMIX_MUTEX_UNIX_H
26 #define PMIX_MUTEX_UNIX_H 1
27
28
29
30
31
32
33
34
35
36
37
38
39 #include "pmix_config.h"
40
41 #include <pthread.h>
42 #include <errno.h>
43 #include <stdio.h>
44
45 #include "src/class/pmix_object.h"
46 #include "src/atomics/sys/atomic.h"
47
48 BEGIN_C_DECLS
49
50 struct pmix_mutex_t {
51 pmix_object_t super;
52
53 pthread_mutex_t m_lock_pthread;
54
55 #if PMIX_ENABLE_DEBUG
56 int m_lock_debug;
57 const char *m_lock_file;
58 int m_lock_line;
59 #endif
60
61 pmix_atomic_lock_t m_lock_atomic;
62 };
63 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_mutex_t);
64 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_recursive_mutex_t);
65
66 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
67 #define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
68 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
69 #define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
70 #endif
71
72 #if PMIX_ENABLE_DEBUG
73 #define PMIX_MUTEX_STATIC_INIT \
74 { \
75 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t), \
76 .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \
77 .m_lock_debug = 0, \
78 .m_lock_file = NULL, \
79 .m_lock_line = 0, \
80 .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED}, \
81 }
82 #else
83 #define PMIX_MUTEX_STATIC_INIT \
84 { \
85 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t), \
86 .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \
87 .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED}, \
88 }
89 #endif
90
91 #if defined(PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
92
93 #if PMIX_ENABLE_DEBUG
94 #define PMIX_RECURSIVE_MUTEX_STATIC_INIT \
95 { \
96 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t), \
97 .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \
98 .m_lock_debug = 0, \
99 .m_lock_file = NULL, \
100 .m_lock_line = 0, \
101 .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED}, \
102 }
103 #else
104 #define PMIX_RECURSIVE_MUTEX_STATIC_INIT \
105 { \
106 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t), \
107 .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \
108 .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED}, \
109 }
110 #endif
111
112 #endif
113
114
115
116
117
118
119
120 static inline int pmix_mutex_trylock(pmix_mutex_t *m)
121 {
122 #if PMIX_ENABLE_DEBUG
123 int ret = pthread_mutex_trylock(&m->m_lock_pthread);
124 if (ret == EDEADLK) {
125 errno = ret;
126 perror("pmix_mutex_trylock()");
127 abort();
128 }
129 return ret;
130 #else
131 return pthread_mutex_trylock(&m->m_lock_pthread);
132 #endif
133 }
134
135 static inline void pmix_mutex_lock(pmix_mutex_t *m)
136 {
137 #if PMIX_ENABLE_DEBUG
138 int ret = pthread_mutex_lock(&m->m_lock_pthread);
139 if (ret == EDEADLK) {
140 errno = ret;
141 perror("pmix_mutex_lock()");
142 abort();
143 }
144 #else
145 pthread_mutex_lock(&m->m_lock_pthread);
146 #endif
147 }
148
149 static inline void pmix_mutex_unlock(pmix_mutex_t *m)
150 {
151 #if PMIX_ENABLE_DEBUG
152 int ret = pthread_mutex_unlock(&m->m_lock_pthread);
153 if (ret == EPERM) {
154 errno = ret;
155 perror("pmix_mutex_unlock");
156 abort();
157 }
158 #else
159 pthread_mutex_unlock(&m->m_lock_pthread);
160 #endif
161 }
162
163
164
165
166
167
168
169 #if PMIX_HAVE_ATOMIC_SPINLOCKS
170
171
172
173
174
175 static inline int pmix_mutex_atomic_trylock(pmix_mutex_t *m)
176 {
177 return pmix_atomic_trylock(&m->m_lock_atomic);
178 }
179
180 static inline void pmix_mutex_atomic_lock(pmix_mutex_t *m)
181 {
182 pmix_atomic_lock(&m->m_lock_atomic);
183 }
184
185 static inline void pmix_mutex_atomic_unlock(pmix_mutex_t *m)
186 {
187 pmix_atomic_unlock(&m->m_lock_atomic);
188 }
189
190 #else
191
192
193
194
195
196 static inline int pmix_mutex_atomic_trylock(pmix_mutex_t *m)
197 {
198 return pmix_mutex_trylock(m);
199 }
200
201 static inline void pmix_mutex_atomic_lock(pmix_mutex_t *m)
202 {
203 pmix_mutex_lock(m);
204 }
205
206 static inline void pmix_mutex_atomic_unlock(pmix_mutex_t *m)
207 {
208 pmix_mutex_unlock(m);
209 }
210
211 #endif
212
213 END_C_DECLS
214
215 #endif