forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwlock.c
451 lines (363 loc) · 9.65 KB
/
rwlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "posix_internal.h"
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/posix/pthread.h>
#include <zephyr/sys/bitarray.h>
#define CONCURRENT_READER_LIMIT (CONFIG_MAX_PTHREAD_COUNT + 1)
struct posix_rwlock {
struct k_sem rd_sem;
struct k_sem wr_sem;
struct k_sem reader_active; /* blocks WR till reader has acquired lock */
k_tid_t wr_owner;
};
struct posix_rwlockattr {
bool initialized: 1;
bool pshared: 1;
};
int64_t timespec_to_timeoutms(const struct timespec *abstime);
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, int32_t timeout);
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, int32_t timeout);
LOG_MODULE_REGISTER(pthread_rwlock, CONFIG_PTHREAD_RWLOCK_LOG_LEVEL);
static struct k_spinlock posix_rwlock_spinlock;
static struct posix_rwlock posix_rwlock_pool[CONFIG_MAX_PTHREAD_RWLOCK_COUNT];
SYS_BITARRAY_DEFINE_STATIC(posix_rwlock_bitarray, CONFIG_MAX_PTHREAD_RWLOCK_COUNT);
/*
* We reserve the MSB to mark a pthread_rwlock_t as initialized (from the
* perspective of the application). With a linear space, this means that
* the theoretical pthread_rwlock_t range is [0,2147483647].
*/
BUILD_ASSERT(CONFIG_MAX_PTHREAD_RWLOCK_COUNT < PTHREAD_OBJ_MASK_INIT,
"CONFIG_MAX_PTHREAD_RWLOCK_COUNT is too high");
static inline size_t posix_rwlock_to_offset(struct posix_rwlock *rwl)
{
return rwl - posix_rwlock_pool;
}
static inline size_t to_posix_rwlock_idx(pthread_rwlock_t rwlock)
{
return mark_pthread_obj_uninitialized(rwlock);
}
static struct posix_rwlock *get_posix_rwlock(pthread_rwlock_t rwlock)
{
int actually_initialized;
size_t bit = to_posix_rwlock_idx(rwlock);
/* if the provided rwlock does not claim to be initialized, its invalid */
if (!is_pthread_obj_initialized(rwlock)) {
LOG_DBG("RWlock is uninitialized (%x)", rwlock);
return NULL;
}
/* Mask off the MSB to get the actual bit index */
if (sys_bitarray_test_bit(&posix_rwlock_bitarray, bit, &actually_initialized) < 0) {
LOG_DBG("RWlock is invalid (%x)", rwlock);
return NULL;
}
if (actually_initialized == 0) {
/* The rwlock claims to be initialized but is actually not */
LOG_DBG("RWlock claims to be initialized (%x)", rwlock);
return NULL;
}
return &posix_rwlock_pool[bit];
}
struct posix_rwlock *to_posix_rwlock(pthread_rwlock_t *rwlock)
{
size_t bit;
struct posix_rwlock *rwl;
if (*rwlock != PTHREAD_RWLOCK_INITIALIZER) {
return get_posix_rwlock(*rwlock);
}
/* Try and automatically associate a posix_rwlock */
if (sys_bitarray_alloc(&posix_rwlock_bitarray, 1, &bit) < 0) {
LOG_DBG("Unable to allocate pthread_rwlock_t");
return NULL;
}
/* Record the associated posix_rwlock in rwl and mark as initialized */
*rwlock = mark_pthread_obj_initialized(bit);
/* Initialize the posix_rwlock */
rwl = &posix_rwlock_pool[bit];
return rwl;
}
/**
* @brief Initialize read-write lock object.
*
* See IEEE 1003.1
*/
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
const pthread_rwlockattr_t *attr)
{
struct posix_rwlock *rwl;
ARG_UNUSED(attr);
*rwlock = PTHREAD_RWLOCK_INITIALIZER;
rwl = to_posix_rwlock(rwlock);
if (rwl == NULL) {
return ENOMEM;
}
k_sem_init(&rwl->rd_sem, CONCURRENT_READER_LIMIT, CONCURRENT_READER_LIMIT);
k_sem_init(&rwl->wr_sem, 1, 1);
k_sem_init(&rwl->reader_active, 1, 1);
rwl->wr_owner = NULL;
LOG_DBG("Initialized rwlock %p", rwl);
return 0;
}
/**
* @brief Destroy read-write lock object.
*
* See IEEE 1003.1
*/
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
{
int ret = 0;
int err;
size_t bit;
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
K_SPINLOCK(&posix_rwlock_spinlock) {
if (rwl->wr_owner != NULL) {
ret = EBUSY;
K_SPINLOCK_BREAK;
}
bit = posix_rwlock_to_offset(rwl);
err = sys_bitarray_free(&posix_rwlock_bitarray, 1, bit);
__ASSERT_NO_MSG(err == 0);
}
return ret;
}
/**
* @brief Lock a read-write lock object for reading.
*
* API behaviour is unpredictable if number of concurrent reader
* lock held is greater than CONCURRENT_READER_LIMIT.
*
* See IEEE 1003.1
*/
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
return read_lock_acquire(rwl, SYS_FOREVER_MS);
}
/**
* @brief Lock a read-write lock object for reading within specific time.
*
* API behaviour is unpredictable if number of concurrent reader
* lock held is greater than CONCURRENT_READER_LIMIT.
*
* See IEEE 1003.1
*/
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
const struct timespec *abstime)
{
int32_t timeout;
uint32_t ret = 0U;
struct posix_rwlock *rwl;
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
return EINVAL;
}
timeout = (int32_t) timespec_to_timeoutms(abstime);
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
if (read_lock_acquire(rwl, timeout) != 0U) {
ret = ETIMEDOUT;
}
return ret;
}
/**
* @brief Lock a read-write lock object for reading immediately.
*
* API behaviour is unpredictable if number of concurrent reader
* lock held is greater than CONCURRENT_READER_LIMIT.
*
* See IEEE 1003.1
*/
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
return read_lock_acquire(rwl, 0);
}
/**
* @brief Lock a read-write lock object for writing.
*
* Write lock does not have priority over reader lock,
* threads get lock based on priority.
*
* See IEEE 1003.1
*/
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
return write_lock_acquire(rwl, SYS_FOREVER_MS);
}
/**
* @brief Lock a read-write lock object for writing within specific time.
*
* Write lock does not have priority over reader lock,
* threads get lock based on priority.
*
* See IEEE 1003.1
*/
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
const struct timespec *abstime)
{
int32_t timeout;
uint32_t ret = 0U;
struct posix_rwlock *rwl;
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
return EINVAL;
}
timeout = (int32_t) timespec_to_timeoutms(abstime);
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
if (write_lock_acquire(rwl, timeout) != 0U) {
ret = ETIMEDOUT;
}
return ret;
}
/**
* @brief Lock a read-write lock object for writing immediately.
*
* Write lock does not have priority over reader lock,
* threads get lock based on priority.
*
* See IEEE 1003.1
*/
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
return write_lock_acquire(rwl, 0);
}
/**
*
* @brief Unlock a read-write lock object.
*
* See IEEE 1003.1
*/
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
struct posix_rwlock *rwl;
rwl = get_posix_rwlock(*rwlock);
if (rwl == NULL) {
return EINVAL;
}
if (k_current_get() == rwl->wr_owner) {
/* Write unlock */
rwl->wr_owner = NULL;
k_sem_give(&rwl->reader_active);
k_sem_give(&rwl->wr_sem);
} else {
/* Read unlock */
k_sem_give(&rwl->rd_sem);
if (k_sem_count_get(&rwl->rd_sem) == CONCURRENT_READER_LIMIT) {
/* Last read lock, unlock writer */
k_sem_give(&rwl->reader_active);
}
}
return 0;
}
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, int32_t timeout)
{
uint32_t ret = 0U;
if (k_sem_take(&rwl->wr_sem, SYS_TIMEOUT_MS(timeout)) == 0) {
k_sem_take(&rwl->reader_active, K_NO_WAIT);
k_sem_take(&rwl->rd_sem, K_NO_WAIT);
k_sem_give(&rwl->wr_sem);
} else {
ret = EBUSY;
}
return ret;
}
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, int32_t timeout)
{
uint32_t ret = 0U;
int64_t elapsed_time, st_time = k_uptime_get();
k_timeout_t k_timeout;
k_timeout = SYS_TIMEOUT_MS(timeout);
/* waiting for release of write lock */
if (k_sem_take(&rwl->wr_sem, k_timeout) == 0) {
/* update remaining timeout time for 2nd sem */
if (timeout != SYS_FOREVER_MS) {
elapsed_time = k_uptime_get() - st_time;
timeout = timeout <= elapsed_time ? 0 :
timeout - elapsed_time;
}
k_timeout = SYS_TIMEOUT_MS(timeout);
/* waiting for reader to complete operation */
if (k_sem_take(&rwl->reader_active, k_timeout) == 0) {
rwl->wr_owner = k_current_get();
} else {
k_sem_give(&rwl->wr_sem);
ret = EBUSY;
}
} else {
ret = EBUSY;
}
return ret;
}
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *ZRESTRICT attr,
int *ZRESTRICT pshared)
{
struct posix_rwlockattr *const a = (struct posix_rwlockattr *)attr;
if (a == NULL || !a->initialized) {
return EINVAL;
}
*pshared = a->pshared;
return 0;
}
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
{
struct posix_rwlockattr *const a = (struct posix_rwlockattr *)attr;
if (a == NULL || !a->initialized) {
return EINVAL;
}
if (!(pshared == PTHREAD_PROCESS_PRIVATE || pshared == PTHREAD_PROCESS_SHARED)) {
return EINVAL;
}
a->pshared = pshared;
return 0;
}
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
{
struct posix_rwlockattr *const a = (struct posix_rwlockattr *)attr;
if (a == NULL) {
return EINVAL;
}
*a = (struct posix_rwlockattr){
.initialized = true,
.pshared = PTHREAD_PROCESS_PRIVATE,
};
return 0;
}
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
{
struct posix_rwlockattr *const a = (struct posix_rwlockattr *)attr;
if (a == NULL || !a->initialized) {
return EINVAL;
}
*a = (struct posix_rwlockattr){0};
return 0;
}