6 #ifndef ABTI_MUTEX_H_INCLUDED
7 #define ABTI_MUTEX_H_INCLUDED
9 static inline ABTI_mutex *ABTI_mutex_get_ptr(
ABT_mutex mutex)
11 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
16 p_mutex = (ABTI_mutex *)mutex;
20 return (ABTI_mutex *)mutex;
24 static inline ABT_mutex ABTI_mutex_get_handle(ABTI_mutex *p_mutex)
26 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
28 if (p_mutex == NULL) {
39 static inline void ABTI_mutex_init(ABTI_mutex *p_mutex)
41 ABTD_spinlock_clear(&p_mutex->lock);
42 #ifndef ABT_CONFIG_USE_SIMPLE_MUTEX
43 ABTD_spinlock_clear(&p_mutex->waiter_lock);
44 ABTI_waitlist_init(&p_mutex->waitlist);
46 p_mutex->attrs = ABTI_MUTEX_ATTR_NONE;
47 p_mutex->nesting_cnt = 0;
48 p_mutex->owner_id = 0;
51 static inline void ABTI_mutex_fini(ABTI_mutex *p_mutex)
53 #ifndef ABT_CONFIG_USE_SIMPLE_MUTEX
54 ABTD_spinlock_acquire(&p_mutex->waiter_lock);
58 static inline void ABTI_mutex_lock_no_recursion(ABTI_local **pp_local,
61 #ifndef ABT_CONFIG_USE_SIMPLE_MUTEX
62 while (ABTD_spinlock_try_acquire(&p_mutex->lock)) {
64 ABTD_spinlock_acquire(&p_mutex->waiter_lock);
66 if (!ABTD_spinlock_try_acquire(&p_mutex->lock)) {
68 ABTD_spinlock_release(&p_mutex->waiter_lock);
72 ABTI_waitlist_wait_and_unlock(pp_local, &p_mutex->waitlist,
73 &p_mutex->waiter_lock,
80 ABTI_ythread *p_ythread = NULL;
81 ABTI_xstream *p_local_xstream = ABTI_local_get_xstream_or_null(*pp_local);
82 if (!ABTI_IS_EXT_THREAD_ENABLED || p_local_xstream)
83 p_ythread = ABTI_thread_get_ythread_or_null(p_local_xstream->p_thread);
86 while (ABTD_spinlock_try_acquire(&p_mutex->lock)) {
87 ABTI_ythread_yield(&p_local_xstream, p_ythread,
88 ABTI_YTHREAD_YIELD_KIND_YIELD_LOOP,
90 *pp_local = ABTI_xstream_get_local(p_local_xstream);
94 ABTD_spinlock_acquire(&p_mutex->lock);
99 static inline void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
101 if (p_mutex->attrs & ABTI_MUTEX_ATTR_RECURSIVE) {
103 ABTI_thread_id self_id = ABTI_self_get_thread_id(*pp_local);
104 if (self_id != p_mutex->owner_id) {
105 ABTI_mutex_lock_no_recursion(pp_local, p_mutex);
106 ABTI_ASSERT(p_mutex->nesting_cnt == 0);
107 p_mutex->owner_id = self_id;
110 p_mutex->nesting_cnt++;
113 ABTI_mutex_lock_no_recursion(pp_local, p_mutex);
117 static inline ABT_bool ABTI_mutex_is_locked(ABTI_mutex *p_mutex)
119 return ABTD_spinlock_is_locked(&p_mutex->lock);
122 static inline int ABTI_mutex_trylock_no_recursion(ABTI_mutex *p_mutex)
128 static inline int ABTI_mutex_trylock(ABTI_local *p_local, ABTI_mutex *p_mutex)
130 if (p_mutex->attrs & ABTI_MUTEX_ATTR_RECURSIVE) {
132 ABTI_thread_id self_id = ABTI_self_get_thread_id(p_local);
133 if (self_id != p_mutex->owner_id) {
134 int abt_errno = ABTI_mutex_trylock_no_recursion(p_mutex);
136 ABTI_ASSERT(p_mutex->nesting_cnt == 0);
137 p_mutex->owner_id = self_id;
142 p_mutex->nesting_cnt++;
146 return ABTI_mutex_trylock_no_recursion(p_mutex);
150 static inline void ABTI_mutex_spinlock_no_recursion(ABTI_mutex *p_mutex)
152 ABTD_spinlock_acquire(&p_mutex->lock);
155 static inline void ABTI_mutex_spinlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
157 if (p_mutex->attrs & ABTI_MUTEX_ATTR_RECURSIVE) {
159 ABTI_thread_id self_id = ABTI_self_get_thread_id(p_local);
160 if (self_id != p_mutex->owner_id) {
161 ABTI_mutex_spinlock_no_recursion(p_mutex);
162 ABTI_ASSERT(p_mutex->nesting_cnt == 0);
163 p_mutex->owner_id = self_id;
166 p_mutex->nesting_cnt++;
169 ABTI_mutex_spinlock_no_recursion(p_mutex);
173 static inline void ABTI_mutex_unlock_no_recursion(ABTI_local *p_local,
176 #ifndef ABT_CONFIG_USE_SIMPLE_MUTEX
177 ABTD_spinlock_acquire(&p_mutex->waiter_lock);
178 ABTD_spinlock_release(&p_mutex->lock);
180 ABTI_waitlist_broadcast(p_local, &p_mutex->waitlist);
181 ABTD_spinlock_release(&p_mutex->waiter_lock);
183 ABTD_spinlock_release(&p_mutex->lock);
187 static inline void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
189 if (p_mutex->attrs & ABTI_MUTEX_ATTR_RECURSIVE) {
191 if (p_mutex->nesting_cnt == 0) {
192 p_mutex->owner_id = 0;
193 ABTI_mutex_unlock_no_recursion(p_local, p_mutex);
195 p_mutex->nesting_cnt--;
199 ABTI_mutex_unlock_no_recursion(p_local, p_mutex);