ARGOBOTS  36a41b57298ad4e16b5ada8c95c81f00dfed4d3c
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);
26  ABTI_UB_ASSERT(ABTI_waitlist_is_empty(&p_cond->waitlist));
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 */
ABT_COND_NULL
#define ABT_COND_NULL
Definition: abt.h:1111
ABT_ERR_INV_MUTEX
#define ABT_ERR_INV_MUTEX
Error code: invalid mutex.
Definition: abt.h:210
ABT_SYNC_EVENT_TYPE_COND
@ ABT_SYNC_EVENT_TYPE_COND
Definition: abt.h:710
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABT_cond
struct ABT_cond_opaque * ABT_cond
Condition variable handle type.
Definition: abt.h:1001
abti_mutex.h