32#ifndef __STATEOS_SEM_H
33#define __STATEOS_SEM_H
46#define semDirect ( 0U )
47#define semBinary ( 1U )
48#define semCounting ( 0U-1 )
49#define semDefault semCounting
55typedef struct __sem sem_t;
65typedef struct __sem sem_id [];
87#define _SEM_INIT( _init, _limit ) { _OBJ_INIT(), _init < _limit ? _init : _limit, _limit }
99#define _VA_SEM( _limit ) ( (_limit + 0) ? (_limit + 0) : (semDefault) )
113#define OS_SEM( sem, init, ... ) \
114 sem_t sem[] = { _SEM_INIT( init, _VA_SEM(__VA_ARGS__) ) }
117#define static_SEM( sem, init, ... ) \
118 static sem_t sem[] = { _SEM_INIT( init, _VA_SEM(__VA_ARGS__) ) }
136#define SEM_INIT( init, ... ) \
137 _SEM_INIT( init, _VA_SEM(__VA_ARGS__) )
156#define SEM_CREATE( init, ... ) \
157 { SEM_INIT ( init, _VA_SEM(__VA_ARGS__) ) }
181void sem_init( sem_t *sem,
unsigned init,
unsigned limit );
199sem_t *
sem_create(
unsigned init,
unsigned limit );
203sem_t *
sem_new(
unsigned init,
unsigned limit ) {
return sem_create(init, limit); }
386#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
408 Semaphore(
const unsigned _init,
const unsigned _limit =
semDefault ): __sem _SEM_INIT(_init, _limit) {}
410 ~Semaphore() { assert(__sem::obj.queue ==
nullptr); }
415 assert(_src.obj.queue ==
nullptr);
445#if __cplusplus >= 201402L
446 using Ptr = std::unique_ptr<Semaphore>;
505 auto sem =
new (std::nothrow)
Semaphore(_init, _limit);
507 sem->__sem::obj.res = sem;
#define INFINITE
infinite waiting
Definition osbase.h:261
int sem_post(sem_t *sem)
alias of sem_give()
Definition ossemaphore.h:339
int sem_takeAsync(sem_t *sem)
Async alias of sem_take().
int sem_waitFor(sem_t *sem, cnt_t delay)
try to lock the semaphore object, wait for given duration of time if the semaphore object can't be lo...
void sem_destroy(sem_t *sem)
reset the semaphore object, wake up all waiting tasks with 'E_DELETED' event value and free allocated...
int sem_giveISR(sem_t *sem)
ISR alias of sem_give().
Definition ossemaphore.h:343
void sem_init(sem_t *sem, unsigned init, unsigned limit)
initialize a semaphore object
int sem_tryWait(sem_t *sem)
alias of sem_take()
Definition ossemaphore.h:250
void sem_delete(sem_t *sem)
alias of sem_destroy()
Definition ossemaphore.h:230
int sem_takeISR(sem_t *sem)
ISR alias of sem_take().
Definition ossemaphore.h:254
#define semDefault
default semaphore type
Definition ossemaphore.h:49
sem_t * sem_create(unsigned init, unsigned limit)
create and initialize a new semaphore object
void sem_kill(sem_t *sem)
alias of sem_reset()
Definition ossemaphore.h:216
int sem_take(sem_t *sem)
try to lock the semaphore object, don't wait if the semaphore object can't be locked immediately
int sem_releaseISR(sem_t *sem, unsigned num)
ISR alias of sem_release().
Definition ossemaphore.h:367
int sem_waitUntil(sem_t *sem, cnt_t time)
try to lock the semaphore object, wait until given timepoint if the semaphore object can't be locked ...
#define semDirect
direct semaphore (producer can't free the semaphore, it can post the semaphore only to the pending ta...
Definition ossemaphore.h:46
unsigned sem_getValue(sem_t *sem)
return current value of semaphore
#define semBinary
binary semaphore
Definition ossemaphore.h:47
int sem_wait(sem_t *sem)
try to lock the semaphore object, wait indefinitely if the semaphore object can't be locked immediate...
Definition ossemaphore.h:314
sem_t * sem_new(unsigned init, unsigned limit)
alias of sem_create()
Definition ossemaphore.h:202
int sem_release(sem_t *sem, unsigned num)
try to increment the internal semaphore counter by the value of num
int sem_waitAsync(sem_t *sem)
Async alias of sem_wait().
int sem_give(sem_t *sem)
try to unlock the semaphore object, don't wait if the semaphore object can't be unlocked immediately
int sem_giveAsync(sem_t *sem)
Async alias of sem_give().
#define semCounting
counting semaphore
Definition ossemaphore.h:48
void sem_reset(sem_t *sem)
reset the semaphore 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 semaphore object
Definition ossemaphore.h:401
std::unique_ptr< Semaphore > Ptr
result type of Create()
Definition ossemaphore.h:445
int giveISR()
sem_giveISR()
Definition ossemaphore.h:434
int tryWait()
sem_tryWait()
Definition ossemaphore.h:425
int takeISR()
sem_takeISR()
Definition ossemaphore.h:426
int waitUntil(const T &_time)
sem_waitUntil()
Definition ossemaphore.h:430
constexpr Semaphore()
constructor, see the constructor parameters of the class
Definition ossemaphore.h:404
int wait()
sem_wait()
Definition ossemaphore.h:431
static Ptr Create(const unsigned _init, const unsigned _limit=semDefault)
create dynamic object with manageable resources
Definition ossemaphore.h:502
static Semaphore Binary(const unsigned _init=0)
create and initialize static binary semaphore
Definition ossemaphore.h:471
int waitFor(const T &_delay)
sem_waitFor()
Definition ossemaphore.h:428
int post()
sem_post()
Definition ossemaphore.h:433
void reset()
sem_reset()
Definition ossemaphore.h:421
void kill()
sem_kill()
Definition ossemaphore.h:422
int takeAsync()
sem_takeAsync()
Definition ossemaphore.h:439
void destroy()
sem_destroy()
Definition ossemaphore.h:423
static Semaphore Counting(const unsigned _init=0)
create and initialize static counting semaphore
Definition ossemaphore.h:485
int waitAsync()
sem_waitAsync()
Definition ossemaphore.h:440
static Semaphore Direct()
create and initialize static direct semaphore
Definition ossemaphore.h:457
int give()
sem_give()
Definition ossemaphore.h:432
unsigned getValue()
sem_getValue()
Definition ossemaphore.h:437
int giveAsync()
sem_giveAsync()
Definition ossemaphore.h:441
int take()
sem_take()
Definition ossemaphore.h:424
int releaseISR(unsigned _num)
sem_releaseISR()
Definition ossemaphore.h:436
int release(unsigned _num)
sem_release()
Definition ossemaphore.h:435