1 sys_arch interface for lwIP 0.5
5 The operating system emulation layer provides a common interface
6 between the lwIP code and the underlying operating system kernel. The
7 general idea is that porting lwIP to new architectures requires only
8 small changes to a few header files and a new sys_arch
9 implementation. It is also possible to do a sys_arch implementation
10 that does not rely on any underlying operating system.
12 The sys_arch provides semaphores and mailboxes to lwIP. For the full
13 lwIP functionality, multiple threads support can be implemented in the
14 sys_arch, but this is not required for the basic lwIP
15 functionality. Previous versions of lwIP required the sys_arch to
16 implement timer scheduling as well but as of lwIP 0.5 this is
17 implemented in a higher layer.
19 Semaphores can be either counting or binary - lwIP works with both
20 kinds. Mailboxes are used for message passing and can be implemented
21 either as a queue which allows multiple messages to be posted to a
22 mailbox, or as a rendez-vous point where only one message can be
23 posted at a time. lwIP works with both kinds, but the former type will
24 be more efficient. A message in a mailbox is just a pointer, nothing
27 Semaphores are represented by the type "sys_sem_t" which is typedef'd
28 in the sys_arch.h file. Mailboxes are equivalently represented by the
29 type "sys_mbox_t". lwIP does not place any restrictions on how
30 sys_sem_t or sys_mbox_t are represented internally.
32 The following functions must be implemented by the sys_arch:
36 Is called to initialize the sys_arch layer.
38 - sys_sem_t sys_sem_new(u8_t count)
40 Creates and returns a new semaphore. The "count" argument specifies
41 the initial state of the semaphore.
43 - void sys_sem_free(sys_sem_t sem)
45 Deallocates a semaphore.
47 - void sys_sem_signal(sys_sem_t sem)
51 - u16_t sys_arch_sem_wait(sys_sem_t sem, u16_t timeout)
53 Blocks the thread while waiting for the semaphore to be
54 signaled. If the "timeout" argument is non-zero, the thread should
55 only be blocked for the specified time (measured in
58 If the timeout argument is non-zero, the return value is the amount
59 of time spent waiting for the semaphore to be signaled. If the
60 semaphore wasn't signaled within the specified time, the return
61 value is zero. If the thread didn't have to wait for the semaphore
62 (i.e., it was already signaled), care must be taken to ensure that
63 the function does not return a zero value since this is used to
64 indicate that a timeout occured. A suitable way to implement this is
65 to check if the time spent waiting is zero and if so, the value 1 is
68 Notice that lwIP implements a function with a similar name,
69 sys_sem_wait(), that uses the sys_arch_sem_wait() function.
71 - sys_mbox_t sys_mbox_new(void)
73 Creates an empty mailbox.
75 - void sys_mbox_free(sys_mbox_t mbox)
77 Deallocates a mailbox. If there are messages still present in the
78 mailbox when the mailbox is deallocated, it is an indication of a
79 programming error in lwIP and the developer should be notified.
81 - void sys_mbox_post(sys_mbox_t mbox, void *msg)
83 Posts the "msg" to the mailbox.
85 - u16_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u16_t timeout)
87 Blocks the thread until a message arrives in the mailbox, but does
88 not block the thread longer than "timeout" milliseconds (similar to
89 the sys_arch_sem_wait() function). The "msg" argument is a result
90 parameter that is set by the function (i.e., by doing "*msg =
91 ptr"). The "msg" parameter maybe NULL to indicate that the message
94 The return values are the same as for the sys_arch_sem_wait()
95 function and the function must not return zero even if a message was
96 present in the mailbox and the time spent waiting was zero
99 Note that a function with a similar name, sys_mbox_fetch(), is
102 - struct sys_timeouts *sys_arch_timeouts(void)
104 Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
105 each thread has a list of timeouts which is repressented as a linked
106 list of sys_timeout structures. The sys_timeouts structure holds a
107 pointer to a linked list of timeouts. This function is called by
108 the lwIP timeout scheduler and must not return a NULL value.
110 In a single threadd sys_arch implementation, this function will
111 simply return a pointer to a global sys_timeouts variable stored in
114 If threads are supported by the underlying operating system and if
115 such functionality is needed in lwIP, the following function will have
116 to be implemented as well:
118 - sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg)
120 Starts a new thread that will begin its execution in the function
121 "thread()". The "arg" argument will be passed as an argument to the
122 thread() function. The id of the new thread is returned.