56typedef struct __mtx mtx_id [];
72#define _MTX_INIT() { _OBJ_INIT(), NULL, NULL }
80#define OS_MTX( mtx ) \
81 mtx_t mtx[] = { _MTX_INIT() }
84#define static_MTX( mtx ) \
85 static mtx_t mtx[] = { _MTX_INIT() }
109#define MTX_CREATE() \
231#if defined(__cplusplus)
238struct Mutex :
public __mtx
242 Mutex(): __mtx _MTX_INIT() {}
244 ~Mutex() { assert(__mtx::owner ==
nullptr); }
249 assert(_src.owner ==
nullptr && _src.obj.queue ==
nullptr);
280 int result = mtx_->lock();
291 int result = mtx_->unlock();
303 bool ownsLock()
const {
return mtx_ !=
nullptr; }
306 operator bool()
const {
return ownsLock(); }
331 UniqueLock(
Mutex& _mtx,
const T& _delay ): mtx_(&_mtx), locked_(false)
340 _src.locked_ =
false;
359 locked_ = _src.locked_;
362 _src.locked_ =
false;
370 if (mtx_ ==
nullptr || locked_)
373 int result = mtx_->tryLock();
381 bool waitFor(
const T& _delay )
383 if (mtx_ ==
nullptr || locked_)
386 int result = mtx_->waitFor(_delay);
396 if (mtx_ ==
nullptr || locked_)
399 int result = mtx_->waitUntil(_time);
408 if (mtx_ ==
nullptr || locked_)
411 int result = mtx_->lock();
420 if (mtx_ ==
nullptr || !locked_)
423 int result = mtx_->unlock();
437 operator bool()
const
445 Mutex* result = mtx_;
460 Mutex *mtx_ =
nullptr;
461 bool locked_ =
false;
#define INFINITE
infinite waiting
Definition osbase.h:250
@ E_TIMEOUT
process was released as a result of the end of the timer countdown
Definition osbase.h:153
@ E_SUCCESS
process was released as a result of taking the supervising object
Definition osbase.h:150
@ E_STOPPED
process was released as a result of reset the supervising object
Definition osbase.h:152
int mtx_tryLock(mtx_t *mtx)
alias of mtx_take()
Definition osmutex.h:156
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...
void mtx_init(mtx_t *mtx)
initialize a mutex object
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:206
void mtx_kill(mtx_t *mtx)
alias of mtx_reset()
Definition osmutex.h:139
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:202
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...
int mtx_unlock(mtx_t *mtx)
alias of mtx_give()
Definition osmutex.h:222
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:109
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:102
create and initialize a lock guard object
Definition osmutex.h:274
bool ownsLock() const
true: the mutex is locked by the guard; false: the mutex was reset while waiting (or the guard was mo...
Definition osmutex.h:302
LockGuard(Mutex &_mtx)
constructor, see the constructor parameters of the class
Definition osmutex.h:277
create and initialize a mutex object
Definition osmutex.h:238
int wait()
mtx_wait()
Definition osmutex.h:263
int tryLock()
mtx_tryLock()
Definition osmutex.h:258
int take()
mtx_take()
Definition osmutex.h:257
int lock()
mtx_lock()
Definition osmutex.h:264
int waitFor(const T &_delay)
mtx_waitFor()
Definition osmutex.h:260
int waitUntil(const T &_time)
mtx_waitUntil()
Definition osmutex.h:262
void reset()
mtx_reset()
Definition osmutex.h:255
constexpr Mutex()
constructor, see the constructor parameters of the class
Definition osmutex.h:241
void kill()
mtx_kill()
Definition osmutex.h:256
int give()
mtx_give()
Definition osmutex.h:265
int unlock()
mtx_unlock()
Definition osmutex.h:266
create and initialize a unique lock object
Definition osmutex.h:320
bool waitUntil(const T &_time)
try to lock the mutex, wait until given timepoint; true: the mutex is locked
Definition osmutex.h:393
bool unlock()
unlock the mutex; true: the mutex was unlocked
Definition osmutex.h:417
Mutex * release()
release the mutex without unlocking it, return the mutex
Definition osmutex.h:442
bool lock()
lock the mutex, wait indefinitely; true: the mutex is locked
Definition osmutex.h:405
UniqueLock(Mutex &_mtx)
constructor, see the constructor parameters of the class
Definition osmutex.h:323
UniqueLock()=default
default constructor
bool waitFor(const T &_delay)
try to lock the mutex, wait for given duration of time; true: the mutex is locked
Definition osmutex.h:380
bool tryLock()
try to lock the mutex, don't wait; true: the mutex is locked
Definition osmutex.h:367
Mutex * mutex() const
associated mutex
Definition osmutex.h:453
bool ownsLock() const
true: the mutex is locked by the object
Definition osmutex.h:429