ARGOBOTS
abti_rwlock.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_RWLOCK_H_INCLUDED
7 #define ABTI_RWLOCK_H_INCLUDED
8 
9 #include "abti_mutex.h"
10 #include "abti_cond.h"
11 
12 /* Inlined functions for RWLock */
13 
14 static inline ABTI_rwlock *ABTI_rwlock_get_ptr(ABT_rwlock rwlock)
15 {
16 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
17  ABTI_rwlock *p_rwlock;
18  if (rwlock == ABT_RWLOCK_NULL) {
19  p_rwlock = NULL;
20  } else {
21  p_rwlock = (ABTI_rwlock *)rwlock;
22  }
23  return p_rwlock;
24 #else
25  return (ABTI_rwlock *)rwlock;
26 #endif
27 }
28 
29 static inline ABT_rwlock ABTI_rwlock_get_handle(ABTI_rwlock *p_rwlock)
30 {
31 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
32  ABT_rwlock h_rwlock;
33  if (p_rwlock == NULL) {
34  h_rwlock = ABT_RWLOCK_NULL;
35  } else {
36  h_rwlock = (ABT_rwlock)p_rwlock;
37  }
38  return h_rwlock;
39 #else
40  return (ABT_rwlock)p_rwlock;
41 #endif
42 }
43 
44 static inline void ABTI_rwlock_init(ABTI_rwlock *p_rwlock)
45 {
46  ABTI_mutex_init(&p_rwlock->mutex);
47  ABTI_cond_init(&p_rwlock->cond);
48  p_rwlock->reader_count = 0;
49  p_rwlock->write_flag = 0;
50 }
51 
52 static inline void ABTI_rwlock_fini(ABTI_rwlock *p_rwlock)
53 {
54  ABTI_mutex_fini(&p_rwlock->mutex);
55  ABTI_cond_fini(&p_rwlock->cond);
56 }
57 
58 static inline int ABTI_rwlock_rdlock(ABTI_local **pp_local,
59  ABTI_rwlock *p_rwlock)
60 {
61  int abt_errno = ABT_SUCCESS;
62 
63  ABTI_mutex_lock(pp_local, &p_rwlock->mutex);
64 
65  while (p_rwlock->write_flag && abt_errno == ABT_SUCCESS) {
66  abt_errno = ABTI_cond_wait(pp_local, &p_rwlock->cond, &p_rwlock->mutex);
67  }
68 
69  if (abt_errno == ABT_SUCCESS) {
70  p_rwlock->reader_count++;
71  }
72 
73  ABTI_mutex_unlock(*pp_local, &p_rwlock->mutex);
74  return abt_errno;
75 }
76 
77 static inline int ABTI_rwlock_wrlock(ABTI_local **pp_local,
78  ABTI_rwlock *p_rwlock)
79 {
80  int abt_errno = ABT_SUCCESS;
81  ABTI_mutex_lock(pp_local, &p_rwlock->mutex);
82 
83  while ((p_rwlock->write_flag || p_rwlock->reader_count) &&
84  abt_errno == ABT_SUCCESS) {
85  abt_errno = ABTI_cond_wait(pp_local, &p_rwlock->cond, &p_rwlock->mutex);
86  }
87 
88  if (abt_errno == ABT_SUCCESS) {
89  p_rwlock->write_flag = 1;
90  }
91 
92  ABTI_mutex_unlock(*pp_local, &p_rwlock->mutex);
93  return abt_errno;
94 }
95 
96 static inline void ABTI_rwlock_unlock(ABTI_local **pp_local,
97  ABTI_rwlock *p_rwlock)
98 {
99  ABTI_mutex_lock(pp_local, &p_rwlock->mutex);
100 
101  if (p_rwlock->write_flag) {
102  p_rwlock->write_flag = 0;
103  } else {
104  p_rwlock->reader_count--;
105  }
106 
107  /* TODO: elision */
108  ABTI_local *p_local = *pp_local;
109  ABTI_cond_broadcast(p_local, &p_rwlock->cond);
110 
111  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
112 }
113 
114 #endif /* ABTI_RWLOCK_H_INCLUDED */
struct ABT_rwlock_opaque * ABT_rwlock
Definition: abt.h:299
#define ABT_SUCCESS
Definition: abt.h:64
#define ABT_RWLOCK_NULL
Definition: abt.h:351