etc/services - sync with NetBSD-8
[minix.git] / minix / lib / liblwip / dist / doc / sys_arch.txt
blob4fdeb2acfe17345e4f261472b4bfbf1424fcf264
1 sys_arch interface for lwIP
3 Author: Adam Dunkels
4         Simon Goldschmidt
6 The operating system emulation layer provides a common interface
7 between the lwIP code and the underlying operating system kernel. The
8 general idea is that porting lwIP to new architectures requires only
9 small changes to a few header files and a new sys_arch
10 implementation. It is also possible to do a sys_arch implementation
11 that does not rely on any underlying operating system.
13 The sys_arch provides semaphores, mailboxes and mutexes to lwIP. For the full
14 lwIP functionality, multiple threads support can be implemented in the
15 sys_arch, but this is not required for the basic lwIP
16 functionality. Timer scheduling is implemented in lwIP, but can be implemented
17 by the sys_arch port (LWIP_TIMERS_CUSTOM==1).
19 In addition to the source file providing the functionality of sys_arch,
20 the OS emulation layer must provide several header files defining
21 macros used throughout lwip.  The files required and the macros they
22 must define are listed below the sys_arch description.
24 Semaphores can be either counting or binary - lwIP works with both
25 kinds. Mailboxes should be implemented as a queue which allows multiple messages
26 to be posted (implementing as a rendez-vous point where only one message can be
27 posted at a time can have a highly negative impact on performance). A message
28 in a mailbox is just a pointer, nothing more. 
30 Semaphores are represented by the type "sys_sem_t" which is typedef'd
31 in the sys_arch.h file. Mailboxes are equivalently represented by the
32 type "sys_mbox_t". Mutexes are represented by the type "sys_mutex_t".
33 lwIP does not place any restrictions on how these types are represented
34 internally.
36 Since lwIP 1.4.0, semaphore, mutexes and mailbox functions are prototyped in a way that
37 allows both using pointers or actual OS structures to be used. This way, memory
38 required for such types can be either allocated in place (globally or on the
39 stack) or on the heap (allocated internally in the "*_new()" functions).
41 The following functions must be implemented by the sys_arch:
43 - void sys_init(void)
45   Is called to initialize the sys_arch layer.
47 - err_t sys_sem_new(sys_sem_t *sem, u8_t count)
49   Creates a new semaphore. The semaphore is allocated to the memory that 'sem'
50   points to (which can be both a pointer or the actual OS structure).
51   The "count" argument specifies the initial state of the semaphore (which is
52   either 0 or 1).
53   If the semaphore has been created, ERR_OK should be returned. Returning any
54   other error will provide a hint what went wrong, but except for assertions,
55   no real error handling is implemented.
57 - void sys_sem_free(sys_sem_t *sem)
59   Deallocates a semaphore.
61 - void sys_sem_signal(sys_sem_t *sem)
63   Signals a semaphore.
65 - u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
67   Blocks the thread while waiting for the semaphore to be
68   signaled. If the "timeout" argument is non-zero, the thread should
69   only be blocked for the specified time (measured in
70   milliseconds). If the "timeout" argument is zero, the thread should be
71   blocked until the semaphore is signalled.
73   If the timeout argument is non-zero, the return value is the number of
74   milliseconds spent waiting for the semaphore to be signaled. If the
75   semaphore wasn't signaled within the specified time, the return value is
76   SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
77   (i.e., it was already signaled), the function may return zero.
79   Notice that lwIP implements a function with a similar name,
80   sys_sem_wait(), that uses the sys_arch_sem_wait() function.
82 - int sys_sem_valid(sys_sem_t *sem)
84   Returns 1 if the semaphore is valid, 0 if it is not valid.
85   When using pointers, a simple way is to check the pointer for != NULL.
86   When directly using OS structures, implementing this may be more complex.
87   This may also be a define, in which case the function is not prototyped.
89 - void sys_sem_set_invalid(sys_sem_t *sem)
91   Invalidate a semaphore so that sys_sem_valid() returns 0.
92   ATTENTION: This does NOT mean that the semaphore shall be deallocated:
93   sys_sem_free() is always called before calling this function!
94   This may also be a define, in which case the function is not prototyped.
96 - void sys_mutex_new(sys_mutex_t *mutex)
98   Creates a new mutex. The mutex is allocated to the memory that 'mutex'
99   points to (which can be both a pointer or the actual OS structure).
100   If the mutex has been created, ERR_OK should be returned. Returning any
101   other error will provide a hint what went wrong, but except for assertions,
102   no real error handling is implemented.
104 - void sys_mutex_free(sys_mutex_t *mutex)
106   Deallocates a mutex.
108 - void sys_mutex_lock(sys_mutex_t *mutex)
109   
110   Blocks the thread until the mutex can be grabbed.
112 - void sys_mutex_unlock(sys_mutex_t *mutex)
114   Releases the mutex previously locked through 'sys_mutex_lock()'.
116 - void sys_mutex_valid(sys_mutex_t *mutex)
118   Returns 1 if the mutes is valid, 0 if it is not valid.
119   When using pointers, a simple way is to check the pointer for != NULL.
120   When directly using OS structures, implementing this may be more complex.
121   This may also be a define, in which case the function is not prototyped.
123 - void sys_mutex_set_invalid(sys_mutex_t *mutex)
125   Invalidate a mutex so that sys_mutex_valid() returns 0.
126   ATTENTION: This does NOT mean that the mutex shall be deallocated:
127   sys_mutex_free() is always called before calling this function!
128   This may also be a define, in which case the function is not prototyped.
130 - err_t sys_mbox_new(sys_mbox_t *mbox, int size)
132   Creates an empty mailbox for maximum "size" elements. Elements stored
133   in mailboxes are pointers. You have to define macros "_MBOX_SIZE"
134   in your lwipopts.h, or ignore this parameter in your implementation
135   and use a default size.
136   If the mailbox has been created, ERR_OK should be returned. Returning any
137   other error will provide a hint what went wrong, but except for assertions,
138   no real error handling is implemented.
140 - void sys_mbox_free(sys_mbox_t *mbox)
142   Deallocates a mailbox. If there are messages still present in the
143   mailbox when the mailbox is deallocated, it is an indication of a
144   programming error in lwIP and the developer should be notified.
146 - void sys_mbox_post(sys_mbox_t *mbox, void *msg)
148   Posts the "msg" to the mailbox. This function have to block until
149   the "msg" is really posted.
151 - err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
153   Try to post the "msg" to the mailbox. Returns ERR_MEM if this one
154   is full, else, ERR_OK if the "msg" is posted.
156 - u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
158   Blocks the thread until a message arrives in the mailbox, but does
159   not block the thread longer than "timeout" milliseconds (similar to
160   the sys_arch_sem_wait() function). If "timeout" is 0, the thread should
161   be blocked until a message arrives. The "msg" argument is a result
162   parameter that is set by the function (i.e., by doing "*msg =
163   ptr"). The "msg" parameter maybe NULL to indicate that the message
164   should be dropped.
166   The return values are the same as for the sys_arch_sem_wait() function:
167   Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
168   timeout.
170   Note that a function with a similar name, sys_mbox_fetch(), is
171   implemented by lwIP. 
173 - u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
175   This is similar to sys_arch_mbox_fetch, however if a message is not
176   present in the mailbox, it immediately returns with the code
177   SYS_MBOX_EMPTY. On success 0 is returned.
179   To allow for efficient implementations, this can be defined as a
180   function-like macro in sys_arch.h instead of a normal function. For
181   example, a naive implementation could be:
182     #define sys_arch_mbox_tryfetch(mbox,msg) \
183       sys_arch_mbox_fetch(mbox,msg,1)
184   although this would introduce unnecessary delays.
186 - int sys_mbox_valid(sys_mbox_t *mbox)
188   Returns 1 if the mailbox is valid, 0 if it is not valid.
189   When using pointers, a simple way is to check the pointer for != NULL.
190   When directly using OS structures, implementing this may be more complex.
191   This may also be a define, in which case the function is not prototyped.
193 - void sys_mbox_set_invalid(sys_mbox_t *mbox)
195   Invalidate a mailbox so that sys_mbox_valid() returns 0.
196   ATTENTION: This does NOT mean that the mailbox shall be deallocated:
197   sys_mbox_free() is always called before calling this function!
198   This may also be a define, in which case the function is not prototyped.
200 If threads are supported by the underlying operating system and if
201 such functionality is needed in lwIP, the following function will have
202 to be implemented as well:
204 - sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
206   Starts a new thread named "name" with priority "prio" that will begin its
207   execution in the function "thread()". The "arg" argument will be passed as an
208   argument to the thread() function. The stack size to used for this thread is
209   the "stacksize" parameter. The id of the new thread is returned. Both the id
210   and the priority are system dependent.
212 When lwIP is used from more than one context (e.g. from multiple threads OR from
213 main-loop and from interrupts), the SYS_LIGHTWEIGHT_PROT protection SHOULD be enabled!
215 - sys_prot_t sys_arch_protect(void)
217   This optional function does a "fast" critical region protection and returns
218   the previous protection level. This function is only called during very short
219   critical regions. An embedded system which supports ISR-based drivers might
220   want to implement this function by disabling interrupts. Task-based systems
221   might want to implement this by using a mutex or disabling tasking. This
222   function should support recursive calls from the same task or interrupt. In
223   other words, sys_arch_protect() could be called while already protected. In
224   that case the return value indicates that it is already protected.
226   sys_arch_protect() is only required if your port is supporting an operating
227   system.
229 - void sys_arch_unprotect(sys_prot_t pval)
231   This optional function does a "fast" set of critical region protection to the
232   value specified by pval. See the documentation for sys_arch_protect() for
233   more information. This function is only required if your port is supporting
234   an operating system.
236 For some configurations, you also need:
238 - u32_t sys_now(void)
240   This optional function returns the current time in milliseconds (don't care
241   for wraparound, this is only used for time diffs).
242   Not implementing this function means you cannot use some modules (e.g. TCP
243   timestamps, internal timeouts for NO_SYS==1).
246 Note:
248 Be careful with using mem_malloc() in sys_arch. When malloc() refers to
249 mem_malloc() you can run into a circular function call problem. In mem.c
250 mem_init() tries to allcate a semaphore using mem_malloc, which of course
251 can't be performed when sys_arch uses mem_malloc.
253 -------------------------------------------------------------------------------
254 Additional files required for the "OS support" emulation layer:
255 -------------------------------------------------------------------------------
257 cc.h       - Architecture environment, some compiler specific, some
258              environment specific (probably should move env stuff 
259              to sys_arch.h.)
261   Typedefs for the types used by lwip -
262     u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_t
264   Compiler hints for packing lwip's structures -
265     PACK_STRUCT_FIELD(x)
266     PACK_STRUCT_STRUCT
267     PACK_STRUCT_BEGIN
268     PACK_STRUCT_END
270   Platform specific diagnostic output -
271     LWIP_PLATFORM_DIAG(x)    - non-fatal, print a message.
272     LWIP_PLATFORM_ASSERT(x)  - fatal, print message and abandon execution.
273     Portability defines for printf formatters:
274     U16_F, S16_F, X16_F, U32_F, S32_F, X32_F, SZT_F
276   "lightweight" synchronization mechanisms -
277     SYS_ARCH_DECL_PROTECT(x) - declare a protection state variable.
278     SYS_ARCH_PROTECT(x)      - enter protection mode.
279     SYS_ARCH_UNPROTECT(x)    - leave protection mode.
281   If the compiler does not provide memset() this file must include a
282   definition of it, or include a file which defines it.
284   This file must either include a system-local <errno.h> which defines
285   the standard *nix error codes (or define LWIP_ERRNO_INCLUDE to that file name),
286   or it should #define LWIP_PROVIDE_ERRNO to make lwip/arch.h define the codes
287   which are used throughout.
290 perf.h     - Architecture specific performance measurement.
291   Measurement calls made throughout lwip, these can be defined to nothing.
292     PERF_START               - start measuring something.
293     PERF_STOP(x)             - stop measuring something, and record the result.
295 sys_arch.h - Tied to sys_arch.c
297   Arch dependent types for the following objects:
298     sys_sem_t, sys_mbox_t, sys_thread_t,
299   And, optionally:
300     sys_prot_t
302   Defines to set vars of sys_mbox_t and sys_sem_t to NULL.
303     SYS_MBOX_NULL NULL
304     SYS_SEM_NULL NULL