Semaphores
lwIP » Porting (system abstraction layer) » OS abstraction layer

## Functions

err_t | sys_sem_new (sys_sem_t *sem, u8_t count)
void | sys_sem_signal (sys_sem_t *sem)
u32_t | sys_arch_sem_wait (sys_sem_t *sem, u32_t timeout)
void | sys_sem_free (sys_sem_t *sem)
int | sys_sem_valid (sys_sem_t *sem)
void | sys_sem_set_invalid (sys_sem_t *sem)

## Detailed Description

Semaphores can be either counting or binary - lwIP works with both kinds. Semaphores are represented by the type "sys_sem_t" which is typedef'd in the sys_arch.h file. Mailboxes are equivalently represented by the type "sys_mbox_t". Mutexes are represented by the type "sys_mutex_t". lwIP does not place any restrictions on how these types are represented internally.

## Function Documentation

## sys_arch_sem_wait()

u32_t sys_arch_sem_wait | ( | sys_sem_t * | sem,
|  | u32_t | timeout
| ) |  |

Blocks the thread while waiting for the semaphore to be signaled. If the "timeout" argument is non-zero, the thread should only be blocked for the specified time (measured in milliseconds). If the "timeout" argument is zero, the thread should be blocked until the semaphore is signalled.

The return value is SYS_ARCH_TIMEOUT if the semaphore wasn't signaled within the specified time or any other value if it was signaled (with or without waiting). Notice that lwIP implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function.

Parameters

  sem | the semaphore to wait for
  timeout | timeout in milliseconds to wait (0 = wait forever)

Returns
  SYS_ARCH_TIMEOUT on timeout, any other value on success

## sys_sem_free()

void sys_sem_free | ( | sys_sem_t * | sem | ) |

Deallocates a semaphore.

Parameters

  sem | semaphore to delete

## sys_sem_new()

err_t sys_sem_new | ( | sys_sem_t * | sem,
|  | u8_t | count
| ) |  |

Create a new semaphore Creates a new semaphore. The semaphore is allocated to the memory that 'sem' points to (which can be both a pointer or the actual OS structure). The "count" argument specifies the initial state of the semaphore (which is either 0 or 1). If the semaphore has been created, ERR_OK should be returned. Returning any other error will provide a hint what went wrong, but except for assertions, no real error handling is implemented.

Parameters

  sem | pointer to the semaphore to create
  count | initial count of the semaphore

Returns
  ERR_OK if successful, another err_t otherwise

## sys_sem_set_invalid()

void sys_sem_set_invalid | ( | sys_sem_t * | sem | ) |

Invalidate a semaphore so that sys_sem_valid() returns 0. ATTENTION: This does NOT mean that the semaphore shall be deallocated: sys_sem_free() is always called before calling this function! This may also be a define, in which case the function is not prototyped.

## sys_sem_signal()

void sys_sem_signal | ( | sys_sem_t * | sem | ) |

Signals a semaphore

Parameters

  sem | the semaphore to signal

## sys_sem_valid()

int sys_sem_valid | ( | sys_sem_t * | sem | ) |

Returns 1 if the semaphore is valid, 0 if it is not valid. When using pointers, a simple way is to check the pointer for != NULL. When directly using OS structures, implementing this may be more complex. This may also be a define, in which case the function is not prototyped.