32#ifndef __STATEOS_MTX_H
33#define __STATEOS_MTX_H
48#define mtxErrorCheck 1U
49#define mtxRecursive 2U
50#define mtxDefault mtxNormal
51#define mtxTypeMASK ( mtxNormal | mtxErrorCheck | mtxRecursive )
55#define mtxPrioInherit 4U
56#define mtxPrioProtect 8U
58#define mtxPrioMASK ( mtxPrioNone | mtxPrioInherit | mtxPrioProtect )
65#define mtxInconsistent 32U
67#define mtxMASK ( mtxTypeMASK | mtxPrioMASK | mtxRobust | mtxInconsistent )
71#define MTX_LIMIT ( 0U-1 )
88typedef struct __mtx mtx_id [];
109#define _MTX_INIT( _mode, _prio ) { _OBJ_INIT(), NULL, _mode, 0, _prio, NULL }
122#define _VA_MTX( _prio ) ( _prio + 0 )
135#define OS_MTX( mtx, mode, ... ) \
136 mtx_t mtx[] = { _MTX_INIT(mode, _VA_MTX(__VA_ARGS__) ) }
139#define static_MTX( mtx, mode, ... ) \
140 static mtx_t mtx[] = { _MTX_INIT(mode, _VA_MTX(__VA_ARGS__) ) }
157#define MTX_INIT( mode, ... ) \
158 _MTX_INIT( mode, _VA_MTX(__VA_ARGS__) )
176#define MTX_CREATE( mode, ... ) \
177 { MTX_INIT ( mode, _VA_MTX(__VA_ARGS__) ) }
200void mtx_init( mtx_t *mtx,
unsigned mode,
unsigned prio );
217mtx_t *
mtx_create(
unsigned mode,
unsigned prio );
373#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
387struct Mutex :
public __mtx
394 Mutex(
const unsigned _mode,
const unsigned _prio = 0 ): __mtx _MTX_INIT(_mode, _prio) {}
396 ~Mutex() { assert(__mtx::owner ==
nullptr); }
401 assert(_src.owner ==
nullptr && _src.obj.queue ==
nullptr);
423#if __cplusplus >= 201402L
424 using Ptr = std::unique_ptr<Mutex>;
444 Ptr Create(
const unsigned _mode,
const unsigned _prio = 0 )
446 auto mtx =
new (std::nothrow)
Mutex(_mode, _prio);
448 mtx->__mtx::obj.res = mtx;
464 int result = mtx_->lock();
475 int result = mtx_->unlock();
487 bool ownsLock()
const {
return mtx_ !=
nullptr; }
490 operator bool()
const {
return ownsLock(); }
515 UniqueLock(
Mutex& _mtx,
const T& _delay ): mtx_(&_mtx), locked_(false)
524 _src.locked_ =
false;
543 locked_ = _src.locked_;
546 _src.locked_ =
false;
554 if (mtx_ ==
nullptr || locked_)
557 int result = mtx_->tryLock();
565 bool waitFor(
const T& _delay )
567 if (mtx_ ==
nullptr || locked_)
570 int result = mtx_->waitFor(_delay);
580 if (mtx_ ==
nullptr || locked_)
583 int result = mtx_->waitUntil(_time);
592 if (mtx_ ==
nullptr || locked_)
595 int result = mtx_->lock();
604 if (mtx_ ==
nullptr || !locked_)
607 int result = mtx_->unlock();
621 operator bool()
const
629 Mutex* result = mtx_;
644 Mutex *mtx_ =
nullptr;
645 bool locked_ =
false;
#define INFINITE
infinite waiting
Definition osbase.h:261
@ E_TIMEOUT
process was released as a result of the end of the timer countdown
Definition osbase.h:150
@ OWNERDEAD
previous owner has stopped
Definition osbase.h:151
@ 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 mtx_delete(mtx_t *mtx)
alias of mtx_destroy()
Definition osmutex.h:248
int mtx_tryLock(mtx_t *mtx)
alias of mtx_take()
Definition osmutex.h:281
mtx_t * mtx_create(unsigned mode, unsigned prio)
create and initialize a new mutex object
unsigned mtx_prio(mtx_t *mtx)
get priority of given mutex
void mtx_reset(mtx_t *mtx)
reset the mutex object and wake up all waiting tasks with 'E_STOPPED' event value
int mtx_give(mtx_t *mtx)
try to unlock the mutex object (only owner task can unlock mutex object), don't wait if the mutex obj...
int mtx_take(mtx_t *mtx)
try to lock the mutex object, don't wait if the mutex object can't be locked immediately
int mtx_lock(mtx_t *mtx)
alias of mtx_wait()
Definition osmutex.h:346
void mtx_kill(mtx_t *mtx)
alias of mtx_reset()
Definition osmutex.h:234
#define mtxErrorCheck
error checking mutex
Definition osmutex.h:48
int mtx_wait(mtx_t *mtx)
try to lock the mutex object, wait indefinitely if the mutex object can't be locked immediately
Definition osmutex.h:342
mtx_t * mtx_new(unsigned mode, unsigned prio)
alias of mtx_create()
Definition osmutex.h:220
int mtx_waitUntil(mtx_t *mtx, cnt_t time)
try to lock the mutex object, wait until given timepoint if the mutex object can't be locked immediat...
int mtx_waitFor(mtx_t *mtx, cnt_t delay)
try to lock the mutex object, wait for given duration of time if the mutex object can't be locked imm...
#define mtxPrioInherit
priority inheritance mutex
Definition osmutex.h:55
int mtx_unlock(mtx_t *mtx)
alias of mtx_give()
Definition osmutex.h:364
void mtx_destroy(mtx_t *mtx)
reset the mutex object, wake up all waiting tasks with 'E_DELETED' event value and free allocated res...
void mtx_init(mtx_t *mtx, unsigned mode, unsigned prio)
initialize a mutex object
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 lock guard object
Definition osmutex.h:458
LockGuard(Mutex &_mtx)
constructor, see the constructor parameters of the class
Definition osmutex.h:461
bool ownsLock() const
true: the mutex is locked by the guard; false: the mutex was reset or deleted while waiting (or the g...
Definition osmutex.h:486
create and initialize a mutex object
Definition osmutex.h:387
static Ptr Create(const unsigned _mode, const unsigned _prio=0)
create dynamic object with manageable resources
Definition osmutex.h:443
std::unique_ptr< Mutex > Ptr
result type of Create()
Definition osmutex.h:423
int lock()
mtx_lock()
Definition osmutex.h:418
int wait()
mtx_wait()
Definition osmutex.h:417
constexpr Mutex()
constructor, see the constructor parameters of the class
Definition osmutex.h:390
unsigned prio()
mtx_prio()
Definition osmutex.h:410
void kill()
mtx_kill()
Definition osmutex.h:408
int unlock()
mtx_unlock()
Definition osmutex.h:420
int waitFor(const T &_delay)
mtx_waitFor()
Definition osmutex.h:414
int waitUntil(const T &_time)
mtx_waitUntil()
Definition osmutex.h:416
int give()
mtx_give()
Definition osmutex.h:419
void reset()
mtx_reset()
Definition osmutex.h:407
void destroy()
mtx_destroy()
Definition osmutex.h:409
int take()
mtx_take()
Definition osmutex.h:411
int tryLock()
mtx_tryLock()
Definition osmutex.h:412
create and initialize a unique lock object
Definition osmutex.h:504
Mutex * release()
release the mutex without unlocking it, return the mutex
Definition osmutex.h:626
bool waitFor(const T &_delay)
try to lock the mutex, wait for given duration of time; true: the mutex is locked
Definition osmutex.h:564
UniqueLock()=default
default constructor
Mutex * mutex() const
associated mutex
Definition osmutex.h:637
bool tryLock()
try to lock the mutex, don't wait; true: the mutex is locked
Definition osmutex.h:551
bool unlock()
unlock the mutex; true: the mutex was unlocked
Definition osmutex.h:601
bool ownsLock() const
true: the mutex is locked by the object
Definition osmutex.h:613
UniqueLock(Mutex &_mtx)
constructor, see the constructor parameters of the class
Definition osmutex.h:507
bool lock()
lock the mutex, wait indefinitely; true: the mutex is locked
Definition osmutex.h:589
bool waitUntil(const T &_time)
try to lock the mutex, wait until given timepoint; true: the mutex is locked
Definition osmutex.h:577