ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
abti_cond.h
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #ifndef ABTI_COND_H_INCLUDED
7 #define ABTI_COND_H_INCLUDED
8 
9 #include "abti_mutex.h"
10 
11 /* Inlined functions for Condition Variable */
12 
13 static inline void ABTI_cond_init(ABTI_cond *p_cond)
14 {
15  ABTD_spinlock_clear(&p_cond->lock);
16  p_cond->p_waiter_mutex = NULL;
17  ABTI_waitlist_init(&p_cond->waitlist);
18 }
19 
20 static inline void ABTI_cond_fini(ABTI_cond *p_cond)
21 {
22  /* The lock needs to be acquired to safely free the condition structure.
23  * However, we do not have to unlock it because the entire structure is
24  * freed here. */
25  ABTD_spinlock_acquire(&p_cond->lock);
27 }
28 
29 static inline ABTI_cond *ABTI_cond_get_ptr(ABT_cond cond)
30 {
31 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
32  ABTI_cond *p_cond;
33  if (cond == ABT_COND_NULL) {
34  p_cond = NULL;
35  } else {
36  p_cond = (ABTI_cond *)cond;
37  }
38  return p_cond;
39 #else
40  return (ABTI_cond *)cond;
41 #endif
42 }
43 
44 static inline ABT_cond ABTI_cond_get_handle(ABTI_cond *p_cond)
45 {
46 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
47  ABT_cond h_cond;
48  if (p_cond == NULL) {
49  h_cond = ABT_COND_NULL;
50  } else {
51  h_cond = (ABT_cond)p_cond;
52  }
53  return h_cond;
54 #else
55  return (ABT_cond)p_cond;
56 #endif
57 }
58 
59 ABTU_ret_err static inline int
60 ABTI_cond_wait(ABTI_local **pp_local, ABTI_cond *p_cond, ABTI_mutex *p_mutex)
61 {
62  ABTD_spinlock_acquire(&p_cond->lock);
63 
64  if (p_cond->p_waiter_mutex == NULL) {
65  p_cond->p_waiter_mutex = p_mutex;
66  } else {
67  if (p_cond->p_waiter_mutex != p_mutex) {
68  ABTD_spinlock_release(&p_cond->lock);
69  return ABT_ERR_INV_MUTEX;
70  }
71  }
72 
73  ABTI_mutex_unlock(*pp_local, p_mutex);
74  ABTI_waitlist_wait_and_unlock(pp_local, &p_cond->waitlist, &p_cond->lock,
75  ABT_SYNC_EVENT_TYPE_COND, (void *)p_cond);
76  /* Lock the mutex again */
77  ABTI_mutex_lock(pp_local, p_mutex);
78  return ABT_SUCCESS;
79 }
80 
81 static inline void ABTI_cond_broadcast(ABTI_local *p_local, ABTI_cond *p_cond)
82 {
83  ABTD_spinlock_acquire(&p_cond->lock);
84  /* Wake up all waiting ULTs */
85  ABTI_waitlist_broadcast(p_local, &p_cond->waitlist);
86  ABTD_spinlock_release(&p_cond->lock);
87 }
88 
89 #endif /* ABTI_COND_H_INCLUDED */
ABTI_mutex_lock
static void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:99
ABTI_waitlist_broadcast
static void ABTI_waitlist_broadcast(ABTI_local *p_local, ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:246
ABT_COND_NULL
#define ABT_COND_NULL
Definition: abt.h:1111
ABTI_cond_fini
static void ABTI_cond_fini(ABTI_cond *p_cond)
Definition: abti_cond.h:20
ABT_ERR_INV_MUTEX
#define ABT_ERR_INV_MUTEX
Error code: invalid mutex.
Definition: abt.h:210
ABTI_mutex_unlock
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:187
ABTI_waitlist_is_empty
static ABT_bool ABTI_waitlist_is_empty(ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:282
ABTI_cond
Definition: abti.h:483
ABTD_spinlock_acquire
static void ABTD_spinlock_acquire(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:28
ABTI_cond::p_waiter_mutex
ABTI_mutex * p_waiter_mutex
Definition: abti.h:485
ABT_SYNC_EVENT_TYPE_COND
@ ABT_SYNC_EVENT_TYPE_COND
Definition: abt.h:710
ABTI_cond_init
static void ABTI_cond_init(ABTI_cond *p_cond)
Definition: abti_cond.h:13
ABTI_cond_get_handle
static ABT_cond ABTI_cond_get_handle(ABTI_cond *p_cond)
Definition: abti_cond.h:44
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTI_cond_wait
static ABTU_ret_err int ABTI_cond_wait(ABTI_local **pp_local, ABTI_cond *p_cond, ABTI_mutex *p_mutex)
Definition: abti_cond.h:60
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
ABTD_spinlock_release
static void ABTD_spinlock_release(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:42
ABTI_cond::lock
ABTD_spinlock lock
Definition: abti.h:484
ABT_cond
struct ABT_cond_opaque * ABT_cond
Condition variable handle type.
Definition: abt.h:1001
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:132
ABTI_cond_broadcast
static void ABTI_cond_broadcast(ABTI_local *p_local, ABTI_cond *p_cond)
Definition: abti_cond.h:81
ABTI_waitlist_init
static void ABTI_waitlist_init(ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:11
ABTI_waitlist_wait_and_unlock
static void ABTI_waitlist_wait_and_unlock(ABTI_local **pp_local, ABTI_waitlist *p_waitlist, ABTD_spinlock *p_lock, ABT_sync_event_type sync_event_type, void *p_sync)
Definition: abti_waitlist.h:21
ABTI_cond_get_ptr
static ABTI_cond * ABTI_cond_get_ptr(ABT_cond cond)
Definition: abti_cond.h:29
ABTI_cond::waitlist
ABTI_waitlist waitlist
Definition: abti.h:486
ABTI_mutex
Definition: abti.h:201
abti_mutex.h