31 p_newcond = (ABTI_cond *)
ABTU_malloc(
sizeof(ABTI_cond));
32 ABTI_cond_init(p_newcond);
35 *newcond = ABTI_cond_get_handle(p_newcond);
56 ABTI_cond *p_cond = ABTI_cond_get_ptr(h_cond);
57 ABTI_CHECK_NULL_COND_PTR(p_cond);
61 ABTI_cond_fini(p_cond);
95 ABTI_local *p_local = ABTI_local_get_local();
96 ABTI_cond *p_cond = ABTI_cond_get_ptr(cond);
97 ABTI_CHECK_NULL_COND_PTR(p_cond);
98 ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
99 ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
101 abt_errno = ABTI_cond_wait(&p_local, p_cond, p_mutex);
116 secs = ((double)p_ts->tv_sec) + 1.0e-9 * ((double)p_ts->tv_nsec);
120 static inline void remove_unit(ABTI_cond *p_cond, ABTI_unit *p_unit)
122 if (p_unit->p_next == NULL)
125 ABTI_spinlock_acquire(&p_cond->lock);
127 if (p_unit->p_next == NULL) {
128 ABTI_spinlock_release(&p_cond->lock);
133 p_cond->num_waiters--;
134 if (p_cond->num_waiters == 0) {
135 p_cond->p_waiter_mutex = NULL;
136 p_cond->p_head = NULL;
137 p_cond->p_tail = NULL;
139 p_unit->p_prev->p_next = p_unit->p_next;
140 p_unit->p_next->p_prev = p_unit->p_prev;
141 if (p_unit == p_cond->p_head) {
142 p_cond->p_head = p_unit->p_next;
143 }
else if (p_unit == p_cond->p_tail) {
144 p_cond->p_tail = p_unit->p_prev;
148 ABTI_spinlock_release(&p_cond->lock);
150 p_unit->p_prev = NULL;
151 p_unit->p_next = NULL;
177 const struct timespec *abstime)
180 ABTI_local *p_local = ABTI_local_get_local();
181 ABTI_cond *p_cond = ABTI_cond_get_ptr(cond);
182 ABTI_CHECK_NULL_COND_PTR(p_cond);
183 ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
184 ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
189 ABTD_atomic_int32 ext_signal = ABTD_ATOMIC_INT32_STATIC_INITIALIZER(0);
191 p_unit = (ABTI_unit *)
ABTU_calloc(1,
sizeof(ABTI_unit));
193 ABTI_STATIC_ASSERT(
sizeof(ext_signal) <=
sizeof(p_unit->handle.thread));
194 p_unit->handle.thread = (
ABT_thread)&ext_signal;
197 ABTI_spinlock_acquire(&p_cond->lock);
199 if (p_cond->p_waiter_mutex == NULL) {
200 p_cond->p_waiter_mutex = p_mutex;
202 ABT_bool result = ABTI_mutex_equal(p_cond->p_waiter_mutex, p_mutex);
204 ABTI_spinlock_release(&p_cond->lock);
211 if (p_cond->num_waiters == 0) {
212 p_unit->p_prev = p_unit;
213 p_unit->p_next = p_unit;
214 p_cond->p_head = p_unit;
215 p_cond->p_tail = p_unit;
217 p_cond->p_tail->p_next = p_unit;
218 p_cond->p_head->p_prev = p_unit;
219 p_unit->p_prev = p_cond->p_tail;
220 p_unit->p_next = p_cond->p_head;
221 p_cond->p_tail = p_unit;
224 p_cond->num_waiters++;
226 ABTI_spinlock_release(&p_cond->lock);
229 ABTI_mutex_unlock(p_local, p_mutex);
231 while (!ABTD_atomic_acquire_load_int32(&ext_signal)) {
232 double cur_time = ABTI_get_wtime();
233 if (cur_time >= tar_time) {
238 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD 244 ABTI_thread_yield(&p_local, p_local->p_thread);
249 ABTI_mutex_lock(&p_local, p_mutex);
276 ABTI_local *p_local = ABTI_local_get_local();
277 ABTI_cond *p_cond = ABTI_cond_get_ptr(cond);
278 ABTI_CHECK_NULL_COND_PTR(p_cond);
280 ABTI_spinlock_acquire(&p_cond->lock);
282 if (p_cond->num_waiters == 0) {
283 ABTI_spinlock_release(&p_cond->lock);
288 ABTI_unit *p_unit = p_cond->p_head;
290 p_cond->num_waiters--;
291 if (p_cond->num_waiters == 0) {
292 p_cond->p_waiter_mutex = NULL;
293 p_cond->p_head = NULL;
294 p_cond->p_tail = NULL;
296 p_unit->p_prev->p_next = p_unit->p_next;
297 p_unit->p_next->p_prev = p_unit->p_prev;
298 p_cond->p_head = p_unit->p_next;
300 p_unit->p_prev = NULL;
301 p_unit->p_next = NULL;
304 ABTI_thread *p_thread = ABTI_thread_get_ptr(p_unit->handle.thread);
305 ABTI_thread_set_ready(p_local, p_thread);
308 ABTD_atomic_int32 *p_ext_signal =
309 (ABTD_atomic_int32 *)p_unit->handle.thread;
310 ABTD_atomic_release_store_int32(p_ext_signal, 1);
313 ABTI_spinlock_release(&p_cond->lock);
339 ABTI_local *p_local = ABTI_local_get_local();
340 ABTI_cond *p_cond = ABTI_cond_get_ptr(cond);
341 ABTI_CHECK_NULL_COND_PTR(p_cond);
343 ABTI_cond_broadcast(p_local, p_cond);
#define ABT_ERR_INV_MUTEX
int ABT_cond_create(ABT_cond *newcond)
Create a new condition variable.
int ABT_cond_free(ABT_cond *cond)
Free the condition variable.
#define ABT_ERR_COND_TIMEDOUT
static void * ABTU_malloc(size_t size)
int ABT_cond_timedwait(ABT_cond cond, ABT_mutex mutex, const struct timespec *abstime)
Wait on the condition.
int ABT_cond_wait(ABT_cond cond, ABT_mutex mutex)
Wait on the condition.
struct ABT_mutex_opaque * ABT_mutex
struct ABT_thread_opaque * ABT_thread
static void remove_unit(ABTI_cond *p_cond, ABTI_unit *p_unit)
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
int ABT_cond_broadcast(ABT_cond cond)
Broadcast a condition.
static double convert_timespec_to_sec(const struct timespec *p_ts)
static void ABTU_free(void *ptr)
int ABT_cond_signal(ABT_cond cond)
Signal a condition.
static void * ABTU_calloc(size_t num, size_t size)
struct ABT_cond_opaque * ABT_cond