32#ifndef __STATEOS_RWL_H
33#define __STATEOS_RWL_H
46#define RDR_LIMIT ( 0U-1 )
52typedef struct __rwl rwl_t;
63typedef struct __rwl rwl_id [];
79#define _RWL_INIT() { _OBJ_INIT(), NULL, false, 0 }
87#define OS_RWL( rwl ) \
88 rwl_t rwl[] = { _RWL_INIT() }
91#define static_RWL( rwl ) \
92 static rwl_t rwl[] = { _RWL_INIT() }
116#define RWL_CREATE() \
362#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
369struct RWLock :
public __rwl
373 RWLock(): __rwl _RWL_INIT() {}
375 ~RWLock() { assert(__rwl::write ==
false && __rwl::count == 0); }
380 assert(_src.write ==
false && _src.count == 0 && _src.obj.queue ==
nullptr && _src.queue ==
nullptr);
411#if __cplusplus >= 201402L
412 using Ptr = std::unique_ptr<RWLock>;
428 auto rwl =
new (std::nothrow)
RWLock();
430 rwl->__rwl::obj.res = rwl;
449 int result = lck_->lockRead();
468 bool ownsLock()
const {
return lck_ !=
nullptr; }
471 operator bool()
const {
return ownsLock(); }
494 int result = lck_->lockWrite();
513 bool ownsLock()
const {
return lck_ !=
nullptr; }
516 operator bool()
const {
return ownsLock(); }
#define INFINITE
infinite waiting
Definition osbase.h:261
@ E_DELETED
process was released as a result of deleting the supervising object
Definition osbase.h:149
@ E_SUCCESS
process was released as a result of taking the supervising object
Definition osbase.h:146
@ E_STOPPED
process was released as a result of reset the supervising object
Definition osbase.h:148
void rwl_destroy(rwl_t *rwl)
reset the read/write lock object, wake up all waiting tasks with 'E_DELETED' event value and free all...
void rwl_unlockRead(rwl_t *rwl)
alias of rwl_giveRead()
Definition osrwlock.h:265
int rwl_waitReadFor(rwl_t *rwl, cnt_t delay)
try to lock the reader, wait for given duration of time if the reader can't be locked immediately
int rwl_waitWriteUntil(rwl_t *rwl, cnt_t time)
try to lock the writer, wait until given timepoint if the writer can't be locked immediately
void rwl_delete(rwl_t *rwl)
alias of rwl_destroy()
Definition osrwlock.h:177
int rwl_lockRead(rwl_t *rwl)
alias of rwl_waitRead()
Definition osrwlock.h:251
int rwl_tryLockWrite(rwl_t *rwl)
alias of rwl_takeWrite()
Definition osrwlock.h:283
rwl_t * rwl_new(void)
alias of rwl_create()
Definition osrwlock.h:149
void rwl_unlockWrite(rwl_t *rwl)
alias of rwl_giveWrite()
Definition osrwlock.h:353
int rwl_waitRead(rwl_t *rwl)
try to lock the reader, wait indefinitely if the reader can't be locked immediately
Definition osrwlock.h:247
void rwl_giveWrite(rwl_t *rwl)
unlock the writer
int rwl_lockWrite(rwl_t *rwl)
alias of rwl_waitWrite()
Definition osrwlock.h:339
int rwl_waitWriteFor(rwl_t *rwl, cnt_t delay)
try to lock the writer, wait for given duration of time if the writer can't be locked immediately
int rwl_waitWrite(rwl_t *rwl)
try to lock the writer, wait indefinitely if the writer can't be locked immediately
Definition osrwlock.h:335
void rwl_giveRead(rwl_t *rwl)
unlock the reader
rwl_t * rwl_create(void)
create and initialize a new read/write lock object
int rwl_waitReadUntil(rwl_t *rwl, cnt_t time)
try to lock the reader, wait until given timepoint if the reader can't be locked immediately
void rwl_kill(rwl_t *rwl)
alias of rwl_reset()
Definition osrwlock.h:163
void rwl_init(rwl_t *rwl)
initialize a read/write lock object
int rwl_takeRead(rwl_t *rwl)
try to lock the reader, don't wait if the reader can't be locked immediately
int rwl_tryLockRead(rwl_t *rwl)
alias of rwl_takeRead()
Definition osrwlock.h:195
int rwl_takeWrite(rwl_t *rwl)
try to lock the writer, don't wait if the writer can't be locked immediately
void rwl_reset(rwl_t *rwl)
reset the read/write lock object and wake up all waiting tasks with 'E_STOPPED' event value
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:109
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:116
create and initialize a read/write lock object
Definition osrwlock.h:369
int waitWrite()
rwl_waitWrite()
Definition osrwlock.h:405
void giveRead()
rwl_giveRead()
Definition osrwlock.h:397
void unlockRead()
rwl_unlockRead()
Definition osrwlock.h:398
constexpr RWLock()
constructor, see the constructor parameters of the class
Definition osrwlock.h:372
std::unique_ptr< RWLock > Ptr
result type of Create()
Definition osrwlock.h:411
static Ptr Create()
create dynamic object with manageable resources
Definition osrwlock.h:425
void destroy()
rwl_destroy()
Definition osrwlock.h:388
int takeRead()
rwl_takeRead()
Definition osrwlock.h:389
void reset()
rwl_reset()
Definition osrwlock.h:386
void giveWrite()
rwl_giveWrite()
Definition osrwlock.h:407
int lockRead()
rwl_lockRead()
Definition osrwlock.h:396
int tryLockWrite()
rwl_tryLockWrite()
Definition osrwlock.h:400
int waitRead()
rwl_waitRead()
Definition osrwlock.h:395
int waitReadFor(const T &_delay)
rwl_waitReadFor()
Definition osrwlock.h:392
int takeWrite()
rwl_takeWrite()
Definition osrwlock.h:399
int lockWrite()
rwl_lockWrite()
Definition osrwlock.h:406
void kill()
rwl_kill()
Definition osrwlock.h:387
int waitReadUntil(const T &_time)
rwl_waitReadUntil()
Definition osrwlock.h:394
int tryLockRead()
rwl_tryLockRead()
Definition osrwlock.h:390
void unlockWrite()
rwl_unlockWrite()
Definition osrwlock.h:408
int waitWriteUntil(const T &_time)
rwl_waitWriteUntil()
Definition osrwlock.h:404
int waitWriteFor(const T &_delay)
rwl_waitWriteFor()
Definition osrwlock.h:402
create and initialize a read guard object
Definition osrwlock.h:443
ReadLock(RWLock &_rwl)
constructor, see the constructor parameters of the class
Definition osrwlock.h:446
bool ownsLock() const
true: the lock is held by the guard; false: the lock was reset or deleted while waiting (or the guard...
Definition osrwlock.h:467
create and initialize a write guard object
Definition osrwlock.h:488
bool ownsLock() const
true: the lock is held by the guard; false: the lock was reset or deleted while waiting (or the guard...
Definition osrwlock.h:512
WriteLock(RWLock &_rwl)
constructor, see the constructor parameters of the class
Definition osrwlock.h:491