ARGOBOTS  1.1
rwlock.c
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 #include "abti.h"
7 
41 {
42 #ifndef ABT_CONFIG_ENABLE_VER_20_API
43  /* Argobots 1.x sets newrwlock to NULL on error. */
44  *newrwlock = ABT_RWLOCK_NULL;
45 #endif
46  ABTI_rwlock *p_newrwlock;
47 
48  int abt_errno = ABTU_malloc(sizeof(ABTI_rwlock), (void **)&p_newrwlock);
49  ABTI_CHECK_ERROR(abt_errno);
50 
51  ABTI_mutex_init(&p_newrwlock->mutex);
52  ABTI_cond_init(&p_newrwlock->cond);
53  p_newrwlock->reader_count = 0;
54  p_newrwlock->write_flag = 0;
55 
56  /* Return value */
57  *newrwlock = ABTI_rwlock_get_handle(p_newrwlock);
58  return ABT_SUCCESS;
59 }
60 
88 {
89  ABT_rwlock h_rwlock = *rwlock;
90  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(h_rwlock);
92 
93  ABTI_cond_fini(&p_rwlock->cond);
94  ABTU_free(p_rwlock);
95 
96  /* Return value */
97  *rwlock = ABT_RWLOCK_NULL;
98  return ABT_SUCCESS;
99 }
100 
139 {
140  ABTI_local *p_local = ABTI_local_get_local();
141  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
142  ABTI_CHECK_NULL_RWLOCK_PTR(p_rwlock);
143 
144 #ifndef ABT_CONFIG_ENABLE_VER_20_API
145  /* Calling this routine on a tasklet is not allowed. */
146  if (ABTI_IS_ERROR_CHECK_ENABLED && p_local) {
147  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream(p_local);
148  ABTI_CHECK_TRUE(p_local_xstream->p_thread->type &
151  }
152 #endif
153 
154  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
155  int abt_errno = ABT_SUCCESS;
156  while (p_rwlock->write_flag && abt_errno == ABT_SUCCESS) {
157  abt_errno = ABTI_cond_wait(&p_local, &p_rwlock->cond, &p_rwlock->mutex);
158  }
159  if (abt_errno == ABT_SUCCESS) {
160  p_rwlock->reader_count++;
161  }
162  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
163  ABTI_CHECK_ERROR(abt_errno);
164  return ABT_SUCCESS;
165 }
166 
200 {
201  ABTI_local *p_local = ABTI_local_get_local();
202  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
203  ABTI_CHECK_NULL_RWLOCK_PTR(p_rwlock);
204 
205 #ifndef ABT_CONFIG_ENABLE_VER_20_API
206  /* Calling this routine on a tasklet is not allowed. */
207  if (ABTI_IS_ERROR_CHECK_ENABLED && p_local) {
208  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream(p_local);
209  ABTI_CHECK_TRUE(p_local_xstream->p_thread->type &
212  }
213 #endif
214 
215  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
216  int abt_errno = ABT_SUCCESS;
217  while ((p_rwlock->write_flag || p_rwlock->reader_count) &&
218  abt_errno == ABT_SUCCESS) {
219  abt_errno = ABTI_cond_wait(&p_local, &p_rwlock->cond, &p_rwlock->mutex);
220  }
221  if (abt_errno == ABT_SUCCESS) {
222  p_rwlock->write_flag = 1;
223  }
224  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
225  ABTI_CHECK_ERROR(abt_errno);
226  return ABT_SUCCESS;
227 }
228 
254 {
255  ABTI_local *p_local = ABTI_local_get_local();
256  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
257  ABTI_CHECK_NULL_RWLOCK_PTR(p_rwlock);
258 
259  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
260  if (p_rwlock->write_flag) {
261  p_rwlock->write_flag = 0;
262  } else {
263  p_rwlock->reader_count--;
264  }
265  ABTI_cond_broadcast(p_local, &p_rwlock->cond);
266  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
267  return ABT_SUCCESS;
268 }
ABTI_mutex_lock
static void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:98
ABTI_cond_fini
static void ABTI_cond_fini(ABTI_cond *p_cond)
Definition: abti_cond.h:20
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
ABTI_rwlock::reader_count
size_t reader_count
Definition: abti.h:444
ABT_rwlock_free
int ABT_rwlock_free(ABT_rwlock *rwlock)
Free a readers-writer lock.
Definition: rwlock.c:87
ABTI_xstream::type
ABTI_xstream_type type
Definition: abti.h:270
ABTI_THREAD_TYPE_YIELDABLE
#define ABTI_THREAD_TYPE_YIELDABLE
Definition: abti.h:86
ABT_rwlock_rdlock
int ABT_rwlock_rdlock(ABT_rwlock rwlock)
Lock a readers-writer lock as a reader.
Definition: rwlock.c:138
ABTI_IS_ERROR_CHECK_ENABLED
#define ABTI_IS_ERROR_CHECK_ENABLED
Definition: abti.h:20
ABTI_xstream
Definition: abti.h:264
ABTI_mutex_unlock
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:181
ABTI_CHECK_NULL_RWLOCK_PTR
#define ABTI_CHECK_NULL_RWLOCK_PTR(p)
Definition: abti_error.h:267
abti.h
ABTI_rwlock::write_flag
int write_flag
Definition: abti.h:445
ABTI_rwlock
Definition: abti.h:441
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:262
ABTI_rwlock::cond
ABTI_cond cond
Definition: abti.h:443
ABTI_local_get_local
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
ABTI_cond_init
static void ABTI_cond_init(ABTI_cond *p_cond)
Definition: abti_cond.h:13
ABTI_rwlock_get_ptr
static ABTI_rwlock * ABTI_rwlock_get_ptr(ABT_rwlock rwlock)
Definition: abti_rwlock.h:14
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
ABT_ERR_RWLOCK
#define ABT_ERR_RWLOCK
Error code: error related to a readers-writer lock.
Definition: abt.h:335
ABT_rwlock_wrlock
int ABT_rwlock_wrlock(ABT_rwlock rwlock)
Lock a readers-writer lock as a writer.
Definition: rwlock.c:199
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:217
ABT_RWLOCK_NULL
#define ABT_RWLOCK_NULL
Definition: abt.h:1069
ABTI_rwlock::mutex
ABTI_mutex mutex
Definition: abti.h:442
ABT_rwlock_unlock
int ABT_rwlock_unlock(ABT_rwlock rwlock)
Unlock a readers-writer lock.
Definition: rwlock.c:253
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_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:130
ABTI_mutex_init
static void ABTI_mutex_init(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:39
ABT_rwlock_create
int ABT_rwlock_create(ABT_rwlock *newrwlock)
Create a new readers-writer lock.
Definition: rwlock.c:40
ABTI_local_get_xstream
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
ABT_rwlock
struct ABT_rwlock_opaque * ABT_rwlock
Readers-writer lock handle type.
Definition: abt.h:966
ABTI_rwlock_get_handle
static ABT_rwlock ABTI_rwlock_get_handle(ABTI_rwlock *p_rwlock)
Definition: abti_rwlock.h:29