sys_arch.txt 13 KB

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