Skip to content

Commit daef600

Browse files
committed
Replace calls to opal atomic by Qthreads synchronization
1 parent 75628d7 commit daef600

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

opal/mca/threads/qthreads/threads_qthreads_mutex.h

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* Copyright (c) 2020 Cisco Systems, Inc. All rights reserved.
2222
* Copyright (c) 2021 Argonne National Laboratory. All rights reserved.
23+
* Copyright (c) 2022 Sandia National Laboratories. All rights reserved.
2324
* $COPYRIGHT$
2425
*
2526
* Additional copyrights may follow
@@ -42,46 +43,68 @@
4243

4344
BEGIN_C_DECLS
4445

45-
typedef opal_atomic_lock_t opal_thread_internal_mutex_t;
46+
typedef aligned_t qtheads_lock_t;
47+
typedef qtheads_lock_t opal_thread_internal_mutex_t;
4648

4749
#define OPAL_THREAD_INTERNAL_MUTEX_INITIALIZER OPAL_ATOMIC_LOCK_INIT
4850
#define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER OPAL_ATOMIC_LOCK_INIT
4951

5052
static inline int opal_thread_internal_mutex_init(opal_thread_internal_mutex_t *p_mutex,
5153
bool recursive)
5254
{
53-
opal_atomic_lock_init(p_mutex, 0);
55+
opal_threads_ensure_init_qthreads();
56+
#if OPAL_ENABLE_DEBUG
57+
int ret = qthread_fill(p_mutex, recursive); //set here that this is recursive or not
58+
if (QTHREAD_SUCCESS != ret) {
59+
opal_output(0, "opal_thread_internal_mutex_init()");
60+
}
61+
#else
62+
qthread_fill(p_mutex, recursive); //set here that this is recursive or not
63+
#endif
64+
5465
return OPAL_SUCCESS;
5566
}
5667

5768
static inline void opal_thread_internal_mutex_lock(opal_thread_internal_mutex_t *p_mutex)
5869
{
5970
opal_threads_ensure_init_qthreads();
60-
61-
int ret = opal_atomic_trylock(p_mutex);
62-
while (0 != ret) {
63-
qthread_yield();
64-
ret = opal_atomic_trylock(p_mutex);
71+
#if OPAL_ENABLE_DEBUG
72+
int ret = qthread_lock(p_mutex);
73+
if (QTHREAD_SUCCESS != ret) {
74+
opal_output(0, "opal_thread_internal_mutex_init()");
6575
}
76+
#else
77+
qthread_lock(p_mutex);
78+
#endif
6679
}
6780

6881
static inline int opal_thread_internal_mutex_trylock(opal_thread_internal_mutex_t *p_mutex)
6982
{
7083
opal_threads_ensure_init_qthreads();
71-
72-
int ret = opal_atomic_trylock(p_mutex);
73-
if (0 != ret) {
74-
/* Yield to avoid a deadlock. */
75-
qthread_yield();
84+
int ret = qthread_trylock(p_mutex);
85+
if (QTHREAD_OPFAIL == ret) {
86+
return 1;
87+
} else if (QTHREAD_SUCCESS != ret) {
88+
#if OPAL_ENABLE_DEBUG
89+
opal_output(0, "opal_thread_internal_mutex_trylock()");
90+
#endif
91+
return 1;
92+
} else {
93+
return 0;
7694
}
77-
return ret;
7895
}
7996

8097
static inline void opal_thread_internal_mutex_unlock(opal_thread_internal_mutex_t *p_mutex)
8198
{
8299
opal_threads_ensure_init_qthreads();
83-
84-
opal_atomic_unlock(p_mutex);
100+
#if OPAL_ENABLE_DEBUG
101+
int ret = qthread_unlock(p_mutex);
102+
if (QTHREAD_SUCCESS != ret) {
103+
opal_output(0, "opal_thread_internal_mutex_unlock()");
104+
}
105+
#else
106+
qthread_unlock(p_mutex);
107+
#endif
85108
/* For fairness of locking. */
86109
qthread_yield();
87110
}
@@ -97,7 +120,7 @@ typedef struct opal_thread_cond_waiter_t {
97120
} opal_thread_cond_waiter_t;
98121

99122
typedef struct {
100-
opal_atomic_lock_t m_lock;
123+
qtheads_lock_t m_lock;
101124
opal_thread_cond_waiter_t *m_waiter_head;
102125
opal_thread_cond_waiter_t *m_waiter_tail;
103126
} opal_thread_internal_cond_t;
@@ -109,7 +132,7 @@ typedef struct {
109132

110133
static inline int opal_thread_internal_cond_init(opal_thread_internal_cond_t *p_cond)
111134
{
112-
opal_atomic_lock_init(&p_cond->m_lock, 0);
135+
qthread_lock(&p_cond->m_lock);
113136
p_cond->m_waiter_head = NULL;
114137
p_cond->m_waiter_tail = NULL;
115138
return OPAL_SUCCESS;
@@ -121,24 +144,23 @@ static inline void opal_thread_internal_cond_wait(opal_thread_internal_cond_t *p
121144
opal_threads_ensure_init_qthreads();
122145
/* This thread is taking "lock", so only this thread can access this
123146
* condition variable. */
124-
opal_atomic_lock(&p_cond->m_lock);
147+
qthread_lock(&p_cond->m_lock);
125148
opal_thread_cond_waiter_t waiter = {0, NULL};
126149
if (NULL == p_cond->m_waiter_head) {
127150
p_cond->m_waiter_tail = &waiter;
128151
} else {
129152
p_cond->m_waiter_head->m_prev = &waiter;
130153
}
131154
p_cond->m_waiter_head = &waiter;
132-
opal_atomic_unlock(&p_cond->m_lock);
133-
134-
while (1) {
135-
opal_thread_internal_mutex_unlock(p_mutex);
155+
qthread_unlock(&p_cond->m_lock);
156+
while (1) {
157+
opal_thread_internal_mutex_lock(p_mutex);
136158
qthread_yield();
137159
opal_thread_internal_mutex_lock(p_mutex);
138160
/* Check if someone woke me up. */
139-
opal_atomic_lock(&p_cond->m_lock);
161+
qthread_lock(&p_cond->m_lock);
140162
int signaled = waiter.m_signaled;
141-
opal_atomic_unlock(&p_cond->m_lock);
163+
qthread_unlock(&p_cond->m_lock);
142164
if (1 == signaled) {
143165
break;
144166
}
@@ -148,7 +170,7 @@ static inline void opal_thread_internal_cond_wait(opal_thread_internal_cond_t *p
148170

149171
static inline void opal_thread_internal_cond_broadcast(opal_thread_internal_cond_t *p_cond)
150172
{
151-
opal_atomic_lock(&p_cond->m_lock);
173+
qthread_lock(&p_cond->m_lock);
152174
while (NULL != p_cond->m_waiter_tail) {
153175
opal_thread_cond_waiter_t *p_cur_tail = p_cond->m_waiter_tail;
154176
p_cond->m_waiter_tail = p_cur_tail->m_prev;
@@ -157,12 +179,12 @@ static inline void opal_thread_internal_cond_broadcast(opal_thread_internal_cond
157179
}
158180
/* No waiters. */
159181
p_cond->m_waiter_head = NULL;
160-
opal_atomic_unlock(&p_cond->m_lock);
182+
qthread_unlock(&p_cond->m_lock);
161183
}
162184

163185
static inline void opal_thread_internal_cond_signal(opal_thread_internal_cond_t *p_cond)
164186
{
165-
opal_atomic_lock(&p_cond->m_lock);
187+
qthread_lock(&p_cond->m_lock);
166188
if (NULL != p_cond->m_waiter_tail) {
167189
opal_thread_cond_waiter_t *p_cur_tail = p_cond->m_waiter_tail;
168190
p_cond->m_waiter_tail = p_cur_tail->m_prev;
@@ -172,7 +194,7 @@ static inline void opal_thread_internal_cond_signal(opal_thread_internal_cond_t
172194
p_cond->m_waiter_head = NULL;
173195
}
174196
}
175-
opal_atomic_unlock(&p_cond->m_lock);
197+
qthread_unlock(&p_cond->m_lock);
176198
}
177199

178200
static inline void opal_thread_internal_cond_destroy(opal_thread_internal_cond_t *p_cond)

0 commit comments

Comments
 (0)