ARGOBOTS  1.1
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 }
27 
28 static inline ABTI_cond *ABTI_cond_get_ptr(ABT_cond cond)
29 {
30 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
31  ABTI_cond *p_cond;
32  if (cond == ABT_COND_NULL) {
33  p_cond = NULL;
34  } else {
35  p_cond = (ABTI_cond *)cond;
36  }
37  return p_cond;
38 #else
39  return (ABTI_cond *)cond;
40 #endif
41 }
42 
43 static inline ABT_cond ABTI_cond_get_handle(ABTI_cond *p_cond)
44 {
45 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
46  ABT_cond h_cond;
47  if (p_cond == NULL) {
48  h_cond = ABT_COND_NULL;
49  } else {
50  h_cond = (ABT_cond)p_cond;
51  }
52  return h_cond;
53 #else
54  return (ABT_cond)p_cond;
55 #endif
56 }
57 
58 ABTU_ret_err static inline int
59 ABTI_cond_wait(ABTI_local **pp_local, ABTI_cond *p_cond, ABTI_mutex *p_mutex)
60 {
61  ABTD_spinlock_acquire(&p_cond->lock);
62 
63  if (p_cond->p_waiter_mutex == NULL) {
64  p_cond->p_waiter_mutex = p_mutex;
65  } else {
66  if (p_cond->p_waiter_mutex != p_mutex) {
67  ABTD_spinlock_release(&p_cond->lock);
68  return ABT_ERR_INV_MUTEX;
69  }
70  }
71 
72  ABTI_mutex_unlock(*pp_local, p_mutex);
73  ABTI_waitlist_wait_and_unlock(pp_local, &p_cond->waitlist, &p_cond->lock,
74  ABT_SYNC_EVENT_TYPE_COND, (void *)p_cond);
75  /* Lock the mutex again */
76  ABTI_mutex_lock(pp_local, p_mutex);
77  return ABT_SUCCESS;
78 }
79 
80 static inline void ABTI_cond_broadcast(ABTI_local *p_local, ABTI_cond *p_cond)
81 {
82  ABTD_spinlock_acquire(&p_cond->lock);
83  /* Wake up all waiting ULTs */
84  ABTI_waitlist_broadcast(p_local, &p_cond->waitlist);
85  ABTD_spinlock_release(&p_cond->lock);
86 }
87 
88 #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:98
ABTI_waitlist_broadcast
static void ABTI_waitlist_broadcast(ABTI_local *p_local, ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:247
ABT_COND_NULL
#define ABT_COND_NULL
Definition: abt.h:1068
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:200
ABTI_mutex_unlock
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:181
ABTI_cond
Definition: abti.h:435
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:437
ABT_SYNC_EVENT_TYPE_COND
@ ABT_SYNC_EVENT_TYPE_COND
Definition: abt.h:674
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:43
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:59
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:146
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
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:436
ABT_cond
struct ABT_cond_opaque * ABT_cond
Condition variable handle type.
Definition: abt.h:959
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
ABTI_cond_broadcast
static void ABTI_cond_broadcast(ABTI_local *p_local, ABTI_cond *p_cond)
Definition: abti_cond.h:80
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:28
ABTI_cond::waitlist
ABTI_waitlist waitlist
Definition: abti.h:438
ABTI_mutex
Definition: abti.h:174
abti_mutex.h