StateOS
Loading...
Searching...
No Matches
osrwlock.h
1/******************************************************************************
2
3 @file StateOS: osrwlock.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_RWL_H
33#define __STATEOS_RWL_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
44/* -------------------------------------------------------------------------- */
45
46#define RDR_LIMIT ( 0U-1 )
47
51
52typedef struct __rwl rwl_t;
53
54struct __rwl
55{
56 obj_t obj;
57
58 tsk_t * queue;
59 bool write;
60 unsigned count;
61};
62
63typedef struct __rwl rwl_id [];
64
65/******************************************************************************
66 *
67 * Name : _RWL_INIT
68 *
69 * Description : create and initialize a read/write lock object
70 *
71 * Parameters : none
72 *
73 * Return : read/write lock object
74 *
75 * Note : for internal use
76 *
77 ******************************************************************************/
78
79#define _RWL_INIT() { _OBJ_INIT(), NULL, false, 0 }
80
87#define OS_RWL( rwl ) \
88 rwl_t rwl[] = { _RWL_INIT() }
89
90/** static alias of OS_RWL() */
91#define static_RWL( rwl ) \
92 static rwl_t rwl[] = { _RWL_INIT() }
93
101
102#ifndef __cplusplus
103#define RWL_INIT() \
104 _RWL_INIT()
105#endif
106
114
115#ifndef __cplusplus
116#define RWL_CREATE() \
117 { RWL_INIT () }
118/** alias of RWL_CREATE() */
119#define RWL_NEW \
120 RWL_CREATE
121#endif
122
123#ifdef __cplusplus
124extern "C" {
125#endif
126
135void rwl_init( rwl_t *rwl );
136
146rwl_t *rwl_create( void );
147
149__STATIC_INLINE
150rwl_t *rwl_new( void ) { return rwl_create(); }
151
160void rwl_reset( rwl_t *rwl );
161
163__STATIC_INLINE
164void rwl_kill( rwl_t *rwl ) { rwl_reset(rwl); }
165
174void rwl_destroy( rwl_t *rwl );
175
177__STATIC_INLINE
178void rwl_delete( rwl_t *rwl ) { rwl_destroy(rwl); }
179
192int rwl_takeRead( rwl_t *rwl );
193
195__STATIC_INLINE
196int rwl_tryLockRead( rwl_t *rwl ) { return rwl_takeRead(rwl); }
197
215int rwl_waitReadFor( rwl_t *rwl, cnt_t delay );
216
232int rwl_waitReadUntil( rwl_t *rwl, cnt_t time );
233
246
247__STATIC_INLINE
248int rwl_waitRead( rwl_t *rwl ) { return rwl_waitReadFor(rwl, INFINITE); }
249
251__STATIC_INLINE
252int rwl_lockRead( rwl_t *rwl ) { return rwl_waitRead(rwl); }
253
262void rwl_giveRead( rwl_t *rwl );
263
265__STATIC_INLINE
266void rwl_unlockRead( rwl_t *rwl ) { rwl_giveRead(rwl); }
267
280int rwl_takeWrite( rwl_t *rwl );
281
283__STATIC_INLINE
284int rwl_tryLockWrite( rwl_t *rwl ) { return rwl_takeWrite(rwl); }
285
303int rwl_waitWriteFor( rwl_t *rwl, cnt_t delay );
304
320int rwl_waitWriteUntil( rwl_t *rwl, cnt_t time );
321
334
335__STATIC_INLINE
336int rwl_waitWrite( rwl_t *rwl ) { return rwl_waitWriteFor(rwl, INFINITE); }
337
339__STATIC_INLINE
340int rwl_lockWrite( rwl_t *rwl ) { return rwl_waitWrite(rwl); }
341
350void rwl_giveWrite( rwl_t *rwl );
351
353__STATIC_INLINE
354void rwl_unlockWrite( rwl_t *rwl ) { rwl_giveWrite(rwl); }
355
356#ifdef __cplusplus
357}
358#endif
359
360/* -------------------------------------------------------------------------- */
361
362#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
363namespace stateos {
364
369struct RWLock : public __rwl
370{
372 constexpr
373 RWLock(): __rwl _RWL_INIT() {}
374
375 ~RWLock() { assert(__rwl::write == false && __rwl::count == 0); }
376
377 /// move constructor
378 RWLock( RWLock&& _src ): __rwl(_src)
379 {
380 assert(_src.write == false && _src.count == 0 && _src.obj.queue == nullptr && _src.queue == nullptr);
381 }
382
383 RWLock( const RWLock& ) = delete;
384 RWLock& operator=( RWLock&& ) = delete;
385 RWLock& operator=( const RWLock& ) = delete;
387 void reset () { rwl_reset (this); }
388 void kill () { rwl_kill (this); }
389 void destroy () { rwl_destroy (this); }
390 int takeRead () { return rwl_takeRead (this); }
391 int tryLockRead () { return rwl_tryLockRead (this); }
392 template<typename T>
393 int waitReadFor ( const T& _delay ) { return rwl_waitReadFor (this, Clock::count(_delay)); }
394 template<typename T>
395 int waitReadUntil ( const T& _time ) { return rwl_waitReadUntil (this, Clock::until(_time)); }
396 int waitRead () { return rwl_waitRead (this); }
397 int lockRead () { return rwl_lockRead (this); }
398 void giveRead () { rwl_giveRead (this); }
399 void unlockRead () { rwl_unlockRead (this); }
400 int takeWrite () { return rwl_takeWrite (this); }
401 int tryLockWrite () { return rwl_tryLockWrite (this); }
402 template<typename T>
403 int waitWriteFor ( const T& _delay ) { return rwl_waitWriteFor (this, Clock::count(_delay)); }
404 template<typename T>
405 int waitWriteUntil( const T& _time ) { return rwl_waitWriteUntil(this, Clock::until(_time)); }
406 int waitWrite () { return rwl_waitWrite (this); }
407 int lockWrite () { return rwl_lockWrite (this); }
408 void giveWrite () { rwl_giveWrite (this); }
409 void unlockWrite () { rwl_unlockWrite (this); }
410
411#if __cplusplus >= 201402L
412 using Ptr = std::unique_ptr<RWLock>;
413#else
414 using Ptr = RWLock *;
415#endif
416
424
425 static
426 Ptr Create()
427 {
428 auto rwl = new (std::nothrow) RWLock();
429 if (rwl != nullptr)
430 rwl->__rwl::obj.res = rwl;
431 return Ptr(rwl);
432 }
433
434};
435
443struct ReadLock
444{
446 explicit
447 ReadLock( RWLock& _rwl ): lck_(&_rwl)
448 {
449 int result = lck_->lockRead();
450 assert(result == E_SUCCESS || result == E_STOPPED || result == E_DELETED);
451 if (result != E_SUCCESS)
452 lck_ = nullptr; // nothing to release
453 }
454
455 ~ReadLock()
456 {
457 if (lck_ != nullptr)
458 lck_->unlockRead();
459 }
460
461 /// move constructor
462 ReadLock( ReadLock&& _src ): lck_(_src.lck_)
463 {
464 _src.lck_ = nullptr;
465 }
466
468 bool ownsLock() const { return lck_ != nullptr; }
469
470 explicit
471 operator bool() const { return ownsLock(); }
472
473 ReadLock( const ReadLock& ) = delete;
474 ReadLock& operator=( ReadLock&& ) = delete;
475 ReadLock& operator=( const ReadLock& ) = delete;
476
477 private:
478 RWLock *lck_;
479};
480
488struct WriteLock
489{
491 explicit
492 WriteLock( RWLock& _rwl ): lck_(&_rwl)
493 {
494 int result = lck_->lockWrite();
495 assert(result == E_SUCCESS || result == E_STOPPED || result == E_DELETED);
496 if (result != E_SUCCESS)
497 lck_ = nullptr; // nothing to release
498 }
499
500 ~WriteLock()
501 {
502 if (lck_ != nullptr)
503 lck_->unlockWrite();
504 }
505
506 /// move constructor
507 WriteLock( WriteLock&& _src ): lck_(_src.lck_)
508 {
509 _src.lck_ = nullptr;
510 }
511
513 bool ownsLock() const { return lck_ != nullptr; }
514
515 explicit
516 operator bool() const { return ownsLock(); }
517
518 WriteLock( const WriteLock& ) = delete;
519 WriteLock& operator=( WriteLock&& ) = delete;
520 WriteLock& operator=( const WriteLock& ) = delete;
521
522 private:
523 RWLock *lck_;
524};
525
526} // namespace
527#endif//__cplusplus
528
529/* -------------------------------------------------------------------------- */
530
532
533#endif//__STATEOS_RWL_H
#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