ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 
25 {
26  int abt_errno;
27  ABTI_rwlock *p_newrwlock;
28 
29  abt_errno = ABTU_malloc(sizeof(ABTI_rwlock), (void **)&p_newrwlock);
30  ABTI_CHECK_ERROR(abt_errno);
31 
32  ABTI_CHECK_TRUE(p_newrwlock != NULL, ABT_ERR_MEM);
33  abt_errno = ABTI_mutex_init(&p_newrwlock->mutex);
34  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
35  ABTU_free(p_newrwlock);
36  ABTI_HANDLE_ERROR(abt_errno);
37  }
38  ABTI_cond_init(&p_newrwlock->cond);
39  p_newrwlock->reader_count = 0;
40  p_newrwlock->write_flag = 0;
41 
42  /* Return value */
43  *newrwlock = ABTI_rwlock_get_handle(p_newrwlock);
44  return ABT_SUCCESS;
45 }
46 
63 {
64  ABT_rwlock h_rwlock = *rwlock;
65  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(h_rwlock);
67 
68  ABTI_mutex_fini(&p_rwlock->mutex);
69  ABTI_cond_fini(&p_rwlock->cond);
70  ABTU_free(p_rwlock);
71 
72  /* Return value */
73  *rwlock = ABT_RWLOCK_NULL;
74  return ABT_SUCCESS;
75 }
76 
93 {
94  ABTI_local *p_local = ABTI_local_get_local();
95  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
97 
98  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
99  int abt_errno = ABT_SUCCESS;
100  while (p_rwlock->write_flag && abt_errno == ABT_SUCCESS) {
101  abt_errno = ABTI_cond_wait(&p_local, &p_rwlock->cond, &p_rwlock->mutex);
102  }
103  if (abt_errno == ABT_SUCCESS) {
104  p_rwlock->reader_count++;
105  }
106  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
107  ABTI_CHECK_ERROR(abt_errno);
108  return ABT_SUCCESS;
109 }
110 
126 {
127  ABTI_local *p_local = ABTI_local_get_local();
128  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
129  ABTI_CHECK_NULL_RWLOCK_PTR(p_rwlock);
130 
131  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
132  int abt_errno = ABT_SUCCESS;
133  while ((p_rwlock->write_flag || p_rwlock->reader_count) &&
134  abt_errno == ABT_SUCCESS) {
135  abt_errno = ABTI_cond_wait(&p_local, &p_rwlock->cond, &p_rwlock->mutex);
136  }
137  if (abt_errno == ABT_SUCCESS) {
138  p_rwlock->write_flag = 1;
139  }
140  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
141  ABTI_CHECK_ERROR(abt_errno);
142  return ABT_SUCCESS;
143 }
144 
159 {
160  ABTI_local *p_local = ABTI_local_get_local();
161  ABTI_rwlock *p_rwlock = ABTI_rwlock_get_ptr(rwlock);
162  ABTI_CHECK_NULL_RWLOCK_PTR(p_rwlock);
163 
164  ABTI_mutex_lock(&p_local, &p_rwlock->mutex);
165  if (p_rwlock->write_flag) {
166  p_rwlock->write_flag = 0;
167  } else {
168  p_rwlock->reader_count--;
169  }
170  ABTI_cond_broadcast(p_local, &p_rwlock->cond);
171  ABTI_mutex_unlock(p_local, &p_rwlock->mutex);
172  return ABT_SUCCESS;
173 }
int write_flag
Definition: abti.h:388
#define ABTI_CHECK_NULL_RWLOCK_PTR(p)
Definition: abti_error.h:265
static void ABTI_cond_fini(ABTI_cond *p_cond)
Definition: abti_cond.h:22
int ABT_rwlock_create(ABT_rwlock *newrwlock) ABT_API_PUBLIC
Create a new rwlock ABT_rwlock_create creates a new rwlock object and returns its handle through newr...
Definition: rwlock.c:24
static ABT_rwlock ABTI_rwlock_get_handle(ABTI_rwlock *p_rwlock)
Definition: abti_rwlock.h:29
struct ABTI_local ABTI_local
Definition: abti.h:101
ABTI_mutex mutex
Definition: abti.h:385
static void ABTI_cond_init(ABTI_cond *p_cond)
Definition: abti_cond.h:13
static void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:75
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:143
static void ABTI_mutex_fini(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:58
int ABT_rwlock_unlock(ABT_rwlock rwlock) ABT_API_PUBLIC
Unlock the rwlock.
Definition: rwlock.c:158
struct ABT_rwlock_opaque * ABT_rwlock
Definition: abt.h:363
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:142
static ABTI_rwlock * ABTI_rwlock_get_ptr(ABT_rwlock rwlock)
Definition: abti_rwlock.h:14
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:137
int ABT_rwlock_free(ABT_rwlock *rwlock) ABT_API_PUBLIC
Free the rwlock object.
Definition: rwlock.c:62
int ABT_rwlock_wrlock(ABT_rwlock rwlock) ABT_API_PUBLIC
Lock the rwlock as a writer.
Definition: rwlock.c:125
static ABTU_ret_err int ABTI_cond_wait(ABTI_local **pp_local, ABTI_cond *p_cond, ABTI_mutex *p_mutex)
Definition: abti_cond.h:61
#define ABT_ERR_MEM
Definition: abt.h:66
#define ABT_SUCCESS
Definition: abt.h:64
static void ABTI_cond_broadcast(ABTI_local *p_local, ABTI_cond *p_cond)
Definition: abti_cond.h:139
static ABTU_ret_err int ABTI_mutex_init(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:39
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:127
#define ABT_RWLOCK_NULL
Definition: abt.h:422
ABTI_cond cond
Definition: abti.h:386
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
#define ABTI_IS_ERROR_CHECK_ENABLED
Definition: abti.h:20
int ABT_rwlock_rdlock(ABT_rwlock rwlock) ABT_API_PUBLIC
Lock the rwlock as a reader.
Definition: rwlock.c:92
static void ABTU_free(void *ptr)
Definition: abtu.h:135
size_t reader_count
Definition: abti.h:387
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:121