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_atomic_relaxed_store_uint32(&p_mutex->val, 0);
42 p_mutex->attr.attrs = ABTI_MUTEX_ATTR_NONE;
43 p_mutex->attr.max_handovers = ABTI_global_get_mutex_max_handovers();
44 p_mutex->attr.max_wakeups = ABTI_global_get_mutex_max_wakeups();
45 #ifndef ABT_CONFIG_USE_SIMPLE_MUTEX 46 p_mutex->p_htable = ABTI_thread_htable_create(
gp_ABTI_global->max_xstreams);
47 p_mutex->p_handover = NULL;
48 p_mutex->p_giver = NULL;
52 #ifdef ABT_CONFIG_USE_SIMPLE_MUTEX 53 #define ABTI_mutex_fini(p_mutex) 55 static inline void ABTI_mutex_fini(ABTI_mutex *p_mutex)
57 ABTI_thread_htable_free(p_mutex->p_htable);
61 static inline void ABTI_mutex_spinlock(ABTI_mutex *p_mutex)
65 while (!ABTD_atomic_bool_cas_weak_uint32(&p_mutex->val, 0, 1)) {
66 while (ABTD_atomic_acquire_load_uint32(&p_mutex->val) != 0)
72 static inline void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
74 #ifdef ABT_CONFIG_USE_SIMPLE_MUTEX 75 ABTI_local *p_local = *pp_local;
79 while (!ABTD_atomic_bool_cas_weak_uint32(&p_mutex->val, 0, 1)) {
80 ABTI_thread_yield(pp_local, p_local->p_thread);
83 LOG_EVENT(
"%p: lock - acquired\n", p_mutex);
85 ABTI_mutex_spinlock(p_mutex);
96 if ((c = ABTD_atomic_val_cas_strong_uint32(&p_mutex->val, 0, 1)) != 0) {
98 c = ABTD_atomic_exchange_uint32(&p_mutex->val, 2);
101 ABTI_mutex_wait(pp_local, p_mutex, 2);
106 if (p_mutex->p_handover) {
107 ABTI_thread *p_self = (*pp_local)->p_thread;
108 if (p_self == p_mutex->p_handover) {
109 p_mutex->p_handover = NULL;
110 ABTD_atomic_release_store_uint32(&p_mutex->val, 2);
113 ABTI_thread *p_giver = p_mutex->p_giver;
114 ABTD_atomic_release_store_int(&p_giver->state,
116 ABTI_POOL_PUSH(p_giver->p_pool, p_giver->unit,
117 ABTI_self_get_native_thread_id(
123 c = ABTD_atomic_exchange_uint32(&p_mutex->val, 2);
126 LOG_EVENT(
"%p: lock - acquired\n", p_mutex);
128 ABTI_mutex_spinlock(p_mutex);
140 static inline int ABTI_mutex_trylock(ABTI_mutex *p_mutex)
142 if (!ABTD_atomic_bool_cas_strong_uint32(&p_mutex->val, 0, 1)) {
148 static inline void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
150 #ifdef ABT_CONFIG_USE_SIMPLE_MUTEX 151 ABTD_atomic_mem_barrier();
152 ABTD_atomic_release_store_uint32(&p_mutex->val, 0);
153 LOG_EVENT(
"%p: unlock w/o wake\n", p_mutex);
155 if (ABTD_atomic_fetch_sub_uint32(&p_mutex->val, 1) != 1) {
156 ABTD_atomic_release_store_uint32(&p_mutex->val, 0);
157 LOG_EVENT(
"%p: unlock with wake\n", p_mutex);
158 ABTI_mutex_wake_de(p_local, p_mutex);
160 LOG_EVENT(
"%p: unlock w/o wake\n", p_mutex);
165 static inline ABT_bool ABTI_mutex_equal(ABTI_mutex *p_mutex1,
166 ABTI_mutex *p_mutex2)
#define ABT_ERR_MUTEX_LOCKED
struct ABT_mutex_opaque * ABT_mutex
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
ABTI_global * gp_ABTI_global
#define LOG_EVENT(fmt,...)