Lightweight semaphore implementation.  
More...
Lightweight semaphore implementation. 
 | 
| file   | sema.h | 
|   | Semaphore definitions. 
  | 
|   | 
 | 
| enum   | sema_state_t { SEMA_OK = 0
, SEMA_DESTROY
 } | 
|   | A Semaphore states. 
  | 
|   | 
◆ SEMA_CREATE
      
        
          | #define SEMA_CREATE | 
          ( | 
            | 
          value | ) | 
             { (value), SEMA_OK, MUTEX_INIT } | 
        
      
 
Creates semaphore statically. 
- Parameters
 - 
  
    | [in] | value | Initial value for the semaphore (can't be 0). For a 0 initialized semaphore  | 
  
   
- See also
 - SEMA_CREATE_LOCKED
 
- Returns
 - Statically initialized semaphore. 
 
Definition at line 48 of file sema.h.
 
 
◆ SEMA_CREATE_LOCKED
Creates semaphore statically initialized to 0. 
- Returns
 - Statically initialized semaphore. 
 
Definition at line 54 of file sema.h.
 
 
◆ _sema_wait_ztimer()
Wait for a semaphore, blocking or non-blocking. 
For commit purposes you should probably use sema_wait(), sema_wait_timed_ztimer() and sema_try_wait() instead.
- Precondition
 (sema != NULL)
- Parameters
 - 
  
    | [in] | sema | A semaphore.  | 
    | [in] | block | if true, block until semaphore is available.  | 
    | [in] | clock | ztimer clock  | 
    | [in] | timeout | if blocking, ticks of clock until the semaphore times out. 0 waits forever. | 
  
   
- Returns
 - 0 on success 
 
- 
-ETIMEDOUT, if the semaphore times out. 
 
- 
-ECANCELED, if the semaphore was destroyed. 
 
- 
-EAGAIN, if the semaphore is not posted (only if block = 0) 
 
 
 
◆ sema_create()
      
        
          | void sema_create  | 
          ( | 
          sema_t *  | 
          sema,  | 
        
        
           | 
           | 
          unsigned int  | 
          value  | 
        
        
           | 
          ) | 
           |  | 
        
      
 
 
◆ sema_destroy()
      
        
          | void sema_destroy  | 
          ( | 
          sema_t *  | 
          sema | ) | 
           | 
        
      
 
 
◆ sema_get_value()
  
  
      
        
          | static unsigned sema_get_value  | 
          ( | 
          const sema_t *  | 
          sema | ) | 
           | 
         
       
   | 
  
inlinestatic   | 
  
 
Get a semaphore's current value. 
- Parameters
 - 
  
  
 
- Returns
 - the current value of the semaphore 
 
Definition at line 111 of file sema.h.
 
 
◆ sema_post()
      
        
          | int sema_post  | 
          ( | 
          sema_t *  | 
          sema | ) | 
           | 
        
      
 
Signal semaphore. 
- Precondition
 (sema != NULL)
- Parameters
 - 
  
  
 
- Returns
 - 0, on success 
 
- 
-EOVERFLOW, if the semaphore's value would overflow. 
 
 
 
◆ sema_try_wait()
  
  
      
        
          | static int sema_try_wait  | 
          ( | 
          sema_t *  | 
          sema | ) | 
           | 
         
       
   | 
  
inlinestatic   | 
  
 
Test if the semaphore is posted. 
- Precondition
 (sema != NULL)
This is a non-blocking alternative to sema_wait.
- Returns
 - 0 on success 
 
- 
-EAGAIN, if the semaphore is not posted. 
 
- 
-ECANCELED, if the semaphore was destroyed. 
 
Definition at line 188 of file sema.h.
 
 
◆ sema_wait()
  
  
      
        
          | static int sema_wait  | 
          ( | 
          sema_t *  | 
          sema | ) | 
           | 
         
       
   | 
  
inlinestatic   | 
  
 
Wait for a semaphore being posted (without timeout). 
- Precondition
 (sema != NULL)
- Parameters
 - 
  
  
 
- Returns
 - 0 on success 
 
- 
-ECANCELED, if the semaphore was destroyed. 
 
Definition at line 172 of file sema.h.
 
 
◆ sema_wait_timed()
  
  
      
        
          | static int sema_wait_timed  | 
          ( | 
          sema_t *  | 
          sema,  | 
         
        
           | 
           | 
          uint64_t  | 
          timeout  | 
         
        
           | 
          ) | 
           |  | 
         
       
   | 
  
inlinestatic   | 
  
 
Wait for a semaphore being posted with a 64bit timeout. 
- Deprecated:
 - Will be removed after release 2021.07
 
- Precondition
 (sema != NULL)
- Parameters
 - 
  
    | [in] | sema | A semaphore.  | 
    | [in] | timeout | Time in microseconds until the semaphore times out. 0 does not wait. | 
  
   
- Returns
 - 0 on success 
 
- 
-ETIMEDOUT, if the semaphore times out. 
 
- 
-ECANCELED, if the semaphore was destroyed. 
 
- 
-EAGAIN, if the semaphore is not posted (only if timeout = 0) 
 
Definition at line 210 of file sema.h.
 
 
◆ sema_wait_timed_ztimer()
Wait for a semaphore being posted, using ztimer as backend. 
- Precondition
 (sema != NULL)  
- 
(clock != NULL) 
- Parameters
 - 
  
    | [in] | sema | A semaphore.  | 
    | [in] | clock | ztimer clock to use  | 
    | [in] | timeout | Time in microseconds until the semaphore times out. 0 does not wait. | 
  
   
- Returns
 - 0 on success 
 
- 
-ETIMEDOUT, if the semaphore times out. 
 
- 
-ECANCELED, if the semaphore was destroyed. 
 
- 
-EAGAIN, if the semaphore is not posted (only if timeout = 0) 
 
Definition at line 232 of file sema.h.