sys.h

OS abstraction layer.

Defines

SYS_ARCH_TIMEOUT

Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait.

SYS_MBOX_EMPTY

sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.

For now we use the same magic value, but we allow this to change in future.

LWIP_COMPAT_MUTEX

Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores should be used instead.

sys_sem_wait(sem)

Wait for a semaphore - forever/no timeout.

sys_sem_valid_val(sem)

Same as sys_sem_valid() but taking a value, not a pointer.

sys_sem_set_invalid_val(sem)

Same as sys_sem_set_invalid() but taking a value, not a pointer.

sys_mbox_tryfetch(mbox, msg)

For now, we map straight to sys_arch implementation.

sys_mbox_fetch(mbox, msg)
sys_mbox_valid_val(mbox)

Same as sys_mbox_valid() but taking a value, not a pointer.

sys_mbox_set_invalid_val(mbox)

Same as sys_mbox_set_invalid() but taking a value, not a pointer.

LWIP_MARK_TCPIP_THREAD()

Called as first thing in the lwIP TCPIP thread.

Can be used in conjunction with LWIP_ASSERT_CORE_LOCKED to check core locking.

See also

multithreading

SYS_ARCH_DECL_PROTECT(lev)

SYS_LIGHTWEIGHT_PROT define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection for certain critical regions during buffer allocation, deallocation and memory allocation and deallocation.

SYS_ARCH_PROTECT(lev)
SYS_ARCH_UNPROTECT(lev)
SYS_ARCH_INC(var, val)
SYS_ARCH_DEC(var, val)
SYS_ARCH_GET(var, ret)
SYS_ARCH_SET(var, val)
SYS_ARCH_LOCKED(code)

Typedefs

typedef void (*lwip_thread_fn)(void *arg)

Function prototype for thread functions.

Functions

err_t sys_mutex_new(sys_mutex_t *mutex)

Create a new mutex.

Note that mutexes are expected to not be taken recursively by the lwIP code, so both implementation types (recursive or non-recursive) should work. The mutex is allocated to the memory that ‘mutex’ points to (which can be both a pointer or the actual OS structure). If the mutex 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:
  • mutex – pointer to the mutex to create

Returns:

ERR_OK if successful, another err_t otherwise

void sys_mutex_lock(sys_mutex_t *mutex)

Blocks the thread until the mutex can be grabbed.

Parameters:
  • mutex – the mutex to lock

void sys_mutex_unlock(sys_mutex_t *mutex)

Releases the mutex previously locked through ‘sys_mutex_lock()’.

Parameters:
  • mutex – the mutex to unlock

void sys_mutex_free(sys_mutex_t *mutex)

Deallocates a mutex.

Parameters:
  • mutex – the mutex to delete

int sys_mutex_valid(sys_mutex_t *mutex)

Returns 1 if the mutes 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.

void sys_mutex_set_invalid(sys_mutex_t *mutex)

Invalidate a mutex so that sys_mutex_valid() returns 0.

ATTENTION: This does NOT mean that the mutex shall be deallocated: sys_mutex_free() is always called before calling this function! This may also be a define, in which case the function is not prototyped.

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

void sys_sem_signal(sys_sem_t *sem)

Signals a semaphore.

Parameters:
  • sem – the semaphore to signal

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

void sys_sem_free(sys_sem_t *sem)

Deallocates a semaphore.

Parameters:
  • sem – semaphore to delete

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.

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.

void sys_msleep(u32_t ms)

Sleep for specified number of ms.

err_t sys_mbox_new(sys_mbox_t *mbox, int size)

Creates an empty mailbox for maximum “size” elements.

Elements stored in mailboxes are pointers. You have to define macros “_MBOX_SIZE” in your lwipopts.h, or ignore this parameter in your implementation and use a default size. If the mailbox 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:
  • mbox – pointer to the mbox to create

  • size – (minimum) number of messages in this mbox

Returns:

ERR_OK if successful, another err_t otherwise

void sys_mbox_post(sys_mbox_t *mbox, void *msg)

Post a message to an mbox - may not fail -> blocks if full, only to be used from tasks NOT from ISR!

Parameters:
  • mbox – mbox to posts the message

  • msg – message to post (ATTENTION: can be NULL)

err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)

Try to post a message to an mbox - may fail if full.

Can be used from ISR (if the sys arch layer allows this). Returns ERR_MEM if it is full, else, ERR_OK if the “msg” is posted.

Parameters:
  • mbox – mbox to posts the message

  • msg – message to post (ATTENTION: can be NULL)

err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)

Try to post a message to an mbox - may fail if full.

To be be used from ISR. Returns ERR_MEM if it is full, else, ERR_OK if the “msg” is posted.

Parameters:
  • mbox – mbox to posts the message

  • msg – message to post (ATTENTION: can be NULL)

u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)

Blocks the thread until a message arrives in the mailbox, but does not block the thread longer than “timeout” milliseconds (similar to the sys_arch_sem_wait() function).

If “timeout” is 0, the thread should be blocked until a message arrives. The “msg” argument is a result parameter that is set by the function (i.e., by doing “*msg =

ptr”). The “msg” parameter maybe NULL to indicate that the message should be dropped. The return values are the same as for the

sys_arch_sem_wait() function: SYS_ARCH_TIMEOUT if there was a timeout, any other value if a messages is received.

Note that a function with a similar name, sys_mbox_fetch(), is implemented by lwIP.

Parameters:
  • mbox – mbox to get a message from

  • msg – pointer where the message is stored

  • timeout – maximum time (in milliseconds) to wait for a message (0 = wait forever)

Returns:

SYS_ARCH_TIMEOUT on timeout, any other value if a message has been received

u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)

This is similar to sys_arch_mbox_fetch, however if a message is not present in the mailbox, it immediately returns with the code SYS_MBOX_EMPTY.

On success 0 is returned. To allow for efficient implementations, this can be defined as a function-like macro in sys_arch.h instead of a normal function. For example, a naive implementation could be: #define sys_arch_mbox_tryfetch(mbox,msg) sys_arch_mbox_fetch(mbox,msg,1) although this would introduce unnecessary delays.

Parameters:
  • mbox – mbox to get a message from

  • msg – pointer where the message is stored

Returns:

0 (milliseconds) if a message has been received or SYS_MBOX_EMPTY if the mailbox is empty

void sys_mbox_free(sys_mbox_t *mbox)

Deallocates a mailbox.

If there are messages still present in the mailbox when the mailbox is deallocated, it is an indication of a programming error in lwIP and the developer should be notified.

Parameters:
  • mbox – mbox to delete

int sys_mbox_valid(sys_mbox_t *mbox)

Returns 1 if the mailbox 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.

void sys_mbox_set_invalid(sys_mbox_t *mbox)

Invalidate a mailbox so that sys_mbox_valid() returns 0.

ATTENTION: This does NOT mean that the mailbox shall be deallocated: sys_mbox_free() is always called before calling this function! This may also be a define, in which case the function is not prototyped.

sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)

The only thread function: Starts a new thread named “name” with priority “prio” that will begin its execution in the function “thread()”.

The “arg” argument will be passed as an argument to the thread() function. The stack size to used for this thread is the “stacksize” parameter. The id of the new thread is returned. Both the id and the priority are system dependent. ATTENTION: although this function returns a value, it MUST NOT FAIL (ports have to assert this!)

Parameters:
  • name – human-readable name for the thread (used for debugging purposes)

  • thread – thread-function

  • arg – parameter passed to ‘thread’

  • stacksize – stack size in bytes for the new thread (may be ignored by ports)

  • prio – priority of the new thread (may be ignored by ports)