StateOS
Loading...
Searching...
No Matches
Semaphore

semaphores (binary, limited, counting) More...

Macros

#define semDirect   ( 0U )
 direct semaphore (producer can't free the semaphore, it can post the semaphore only to the pending task)
#define semBinary   ( 1U )
 binary semaphore
#define semCounting   ( 0U-1 )
 counting semaphore
#define semDefault   semCounting
 default semaphore type
#define OS_SEM(sem, init, ...)
 define and initialize a semaphore object
#define static_SEM(sem, init, ...)
 static alias of OS_SEM()
#define SEM_INIT(init, ...)
 create and initialize a semaphore object
#define SEM_CREATE(init, ...)
 create and initialize a semaphore object
#define SEM_NEW   SEM_CREATE
 alias of SEM_CREATE()

Functions

void sem_init (sem_t *sem, unsigned init, unsigned limit)
 initialize a semaphore object
sem_t * sem_create (unsigned init, unsigned limit)
 create and initialize a new semaphore object
sem_t * sem_new (unsigned init, unsigned limit)
 alias of sem_create()
void sem_reset (sem_t *sem)
 reset the semaphore object and wake up all waiting tasks with 'E_STOPPED' event value
void sem_kill (sem_t *sem)
 alias of sem_reset()
void sem_destroy (sem_t *sem)
 reset the semaphore object, wake up all waiting tasks with 'E_DELETED' event value and free allocated resource
void sem_delete (sem_t *sem)
 alias of sem_destroy()
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_tryWait (sem_t *sem)
 alias of sem_take()
int sem_takeISR (sem_t *sem)
 ISR alias of sem_take().
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 locked immediately
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 immediately
int sem_wait (sem_t *sem)
 try to lock the semaphore object, wait indefinitely if the semaphore object can't be locked immediately
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_post (sem_t *sem)
 alias of sem_give()
int sem_giveISR (sem_t *sem)
 ISR alias of sem_give().
int sem_giveAsync (sem_t *sem)
 Async alias of sem_give().
int sem_release (sem_t *sem, unsigned num)
 try to increment the internal semaphore counter by the value of num
int sem_releaseISR (sem_t *sem, unsigned num)
 ISR alias of sem_release().
unsigned sem_getValue (sem_t *sem)
 return current value of semaphore

Detailed Description

semaphores (binary, limited, counting)

Macro Definition Documentation

◆ OS_SEM

#define OS_SEM ( sem,
init,
... )
Value:
sem_t sem[] = { _SEM_INIT( init, _VA_SEM(__VA_ARGS__) ) }

define and initialize a semaphore object

Parameters
semname of a pointer to semaphore object
initinitial value of semaphore counter
...limit: (optional) maximum value of semaphore counter
  • semDirect: direct semaphore
  • semBinary: binary semaphore
  • semCounting: counting semaphore (default)
  • otherwise: limited semaphore

◆ static_SEM

#define static_SEM ( sem,
init,
... )
Value:
static sem_t sem[] = { _SEM_INIT( init, _VA_SEM(__VA_ARGS__) ) }

static alias of OS_SEM()

◆ SEM_INIT

#define SEM_INIT ( init,
... )
Value:
_SEM_INIT( init, _VA_SEM(__VA_ARGS__) )

create and initialize a semaphore object

Parameters
initinitial value of semaphore counter
...limit: (optional) maximum value of semaphore counter
  • semDirect: direct semaphore
  • semBinary: binary semaphore
  • semCounting: counting semaphore (default)
  • otherwise: limited semaphore
Returns
semaphore object
Note
use only in 'C' code

◆ SEM_CREATE

#define SEM_CREATE ( init,
... )
Value:
{ SEM_INIT ( init, _VA_SEM(__VA_ARGS__) ) }
#define SEM_INIT(init,...)
create and initialize a semaphore object
Definition ossemaphore.h:135

create and initialize a semaphore object

Parameters
initinitial value of semaphore counter
...limit: (optional) maximum value of semaphore counter
  • semDirect: direct semaphore
  • semBinary: binary semaphore
  • semCounting: counting semaphore (default)
  • otherwise: limited semaphore
Returns
semaphore object as array (id)
Note
use only in 'C' code

Function Documentation

◆ sem_init()

void sem_init ( sem_t * sem,
unsigned init,
unsigned limit )

initialize a semaphore object

Parameters
sempointer to semaphore object
initinitial value of semaphore counter
limitmaximum value of semaphore counter
  • semDirect: direct semaphore
  • semBinary: binary semaphore
  • semCounting: counting semaphore
  • otherwise: limited semaphore
Note
use only in thread mode

◆ sem_create()

sem_t * sem_create ( unsigned init,
unsigned limit )

create and initialize a new semaphore object

Parameters
initinitial value of semaphore counter
limitmaximum value of semaphore counter
  • semDirect: direct semaphore
  • semBinary: binary semaphore
  • semCounting: counting semaphore
  • otherwise: limited semaphore
Returns
pointer to semaphore object
Return values
NULLobject not created (not enough free memory)
Note
use only in thread mode

◆ sem_reset()

void sem_reset ( sem_t * sem)

reset the semaphore object and wake up all waiting tasks with 'E_STOPPED' event value

Parameters
sempointer to semaphore object
Note
use only in thread mode

◆ sem_destroy()

void sem_destroy ( sem_t * sem)

reset the semaphore object, wake up all waiting tasks with 'E_DELETED' event value and free allocated resource

Parameters
sempointer to semaphore object
Note
use only in thread mode

◆ sem_take()

int sem_take ( sem_t * sem)

try to lock the semaphore object, don't wait if the semaphore object can't be locked immediately

Parameters
sempointer to semaphore object
Return values
E_SUCCESSsemaphore object was successfully locked
E_TIMEOUTsemaphore object can't be locked immediately, try again
Note
can be used in both thread and handler mode
use ISR alias in blockable interrupt handlers
use Async alias for communication with unmasked interrupt handlers

◆ sem_waitFor()

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 locked immediately

Parameters
sempointer to semaphore object
delayduration of time (maximum number of ticks to wait for lock the semaphore object)
  • IMMEDIATE: don't wait if the semaphore object can't be locked immediately
  • INFINITE: wait indefinitely until the semaphore object has been locked
Return values
E_SUCCESSsemaphore object was successfully locked
E_STOPPEDsemaphore object was reseted before the specified timeout expired
E_DELETEDsemaphore object was deleted before the specified timeout expired
E_TIMEOUTsemaphore object was not locked before the specified timeout expired
Note
use only in thread mode

◆ sem_waitUntil()

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 immediately

Parameters
sempointer to semaphore object
timetimepoint value
Return values
E_SUCCESSsemaphore object was successfully locked
E_STOPPEDsemaphore object was reseted before the specified timeout expired
E_DELETEDsemaphore object was deleted before the specified timeout expired
E_TIMEOUTsemaphore object was not locked before the specified timeout expired
Note
use only in thread mode

◆ sem_wait()

int sem_wait ( sem_t * sem)
inline

try to lock the semaphore object, wait indefinitely if the semaphore object can't be locked immediately

Parameters
sempointer to semaphore object
Return values
E_SUCCESSsemaphore object was successfully locked
E_STOPPEDsemaphore object was reseted (unavailable for async version)
E_DELETEDsemaphore object was deleted (unavailable for async version)
Note
use only in thread mode
use Async alias for communication with unmasked interrupt handlers
the Async alias is intended for tasks of equal priority: it waits in a loop with tsk_yield, lower priority tasks do not run meanwhile

◆ sem_give()

int sem_give ( sem_t * sem)

try to unlock the semaphore object, don't wait if the semaphore object can't be unlocked immediately

Parameters
sempointer to semaphore object
Return values
E_SUCCESSsemaphore object was successfully unlocked
E_TIMEOUTsemaphore object can't be unlocked immediately, try again
Note
can be used in both thread and handler mode
use ISR alias in blockable interrupt handlers
use Async alias for communication with unmasked interrupt handlers

◆ sem_release()

int sem_release ( sem_t * sem,
unsigned num )

try to increment the internal semaphore counter by the value of num

Parameters
sempointer to semaphore object
numupdate value of semaphore counter
Return values
E_SUCCESSsemaphore counter was successfully updated
E_TIMEOUTsemaphore counter was updated to the limit value
Note
can be used in both thread and handler mode
use ISR alias in blockable interrupt handlers

◆ sem_getValue()

unsigned sem_getValue ( sem_t * sem)

return current value of semaphore

Parameters
sempointer to semaphore object
Returns
current value of semaphore