7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * This file is part of the lwIP TCP/IP stack.
34 * Author: Adam Dunkels <adam@sics.se>
37 #ifndef LWIP_HDR_SYS_H
38 #define LWIP_HDR_SYS_H
48 /* For a totally minimal and standalone system, we provide null
49 definitions of the sys_ functions. */
50 typedef u8_t sys_sem_t;
51 typedef u8_t sys_mutex_t;
52 typedef u8_t sys_mbox_t;
54 #define sys_sem_new(s, c) ERR_OK
55 #define sys_sem_signal(s)
56 #define sys_sem_wait(s)
57 #define sys_arch_sem_wait(s,t)
58 #define sys_sem_free(s)
59 #define sys_sem_valid(s) 0
60 #define sys_sem_valid_val(s) 0
61 #define sys_sem_set_invalid(s)
62 #define sys_sem_set_invalid_val(s)
63 #define sys_mutex_new(mu) ERR_OK
64 #define sys_mutex_lock(mu)
65 #define sys_mutex_unlock(mu)
66 #define sys_mutex_free(mu)
67 #define sys_mutex_valid(mu) 0
68 #define sys_mutex_set_invalid(mu)
69 #define sys_mbox_new(m, s) ERR_OK
70 #define sys_mbox_fetch(m,d)
71 #define sys_mbox_tryfetch(m,d)
72 #define sys_mbox_post(m,d)
73 #define sys_mbox_trypost(m,d)
74 #define sys_mbox_free(m)
75 #define sys_mbox_valid(m)
76 #define sys_mbox_valid_val(m)
77 #define sys_mbox_set_invalid(m)
78 #define sys_mbox_set_invalid_val(m)
80 #define sys_thread_new(n,t,a,s,p)
81 #define sys_thread_finish(a,b)
87 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
88 #define SYS_ARCH_TIMEOUT 0xffffffffUL
90 /** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.
91 * For now we use the same magic value, but we allow this to change in future.
93 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
96 #include "arch/sys_arch.h"
98 /** Function prototype for thread functions */
99 typedef void (*lwip_thread_fn)(void *arg);
101 /* Function prototypes for functions to be implemented by platform ports
104 /* Mutex functions: */
106 /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores
107 should be used instead */
108 #ifndef LWIP_COMPAT_MUTEX
109 #define LWIP_COMPAT_MUTEX 0
112 #if LWIP_COMPAT_MUTEX
113 /* for old ports that don't have mutexes: define them to binary semaphores */
114 #define sys_mutex_t sys_sem_t
115 #define sys_mutex_new(mutex) sys_sem_new(mutex, 1)
116 #define sys_mutex_lock(mutex) sys_sem_wait(mutex)
117 #define sys_mutex_unlock(mutex) sys_sem_signal(mutex)
118 #define sys_mutex_free(mutex) sys_sem_free(mutex)
119 #define sys_mutex_valid(mutex) sys_sem_valid(mutex)
120 #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex)
122 #else /* LWIP_COMPAT_MUTEX */
126 * Create a new mutex.
127 * Note that mutexes are expected to not be taken recursively by the lwIP code,
128 * so both implementation types (recursive or non-recursive) should work.
129 * @param mutex pointer to the mutex to create
130 * @return ERR_OK if successful, another err_t otherwise
132 err_t sys_mutex_new(sys_mutex_t *mutex);
136 * @param mutex the mutex to lock
138 void sys_mutex_lock(sys_mutex_t *mutex);
142 * @param mutex the mutex to unlock
144 void sys_mutex_unlock(sys_mutex_t *mutex);
148 * @param mutex the mutex to delete
150 void sys_mutex_free(sys_mutex_t *mutex);
151 #ifndef sys_mutex_valid
154 * Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid
156 int sys_mutex_valid(sys_mutex_t *mutex);
158 #ifndef sys_mutex_set_invalid
161 * Set a mutex invalid so that sys_mutex_valid returns 0
163 void sys_mutex_set_invalid(sys_mutex_t *mutex);
165 #endif /* LWIP_COMPAT_MUTEX */
167 /* Semaphore functions: */
171 * Create a new semaphore
172 * @param sem pointer to the semaphore to create
173 * @param count initial count of the semaphore
174 * @return ERR_OK if successful, another err_t otherwise
176 err_t sys_sem_new(sys_sem_t *sem, u8_t count);
179 * Signals a semaphore
180 * @param sem the semaphore to signal
182 void sys_sem_signal(sys_sem_t *sem);
185 * Wait for a semaphore for the specified timeout
186 * @param sem the semaphore to wait for
187 * @param timeout timeout in milliseconds to wait (0 = wait forever)
188 * @return time (in milliseconds) waited for the semaphore
189 * or SYS_ARCH_TIMEOUT on timeout
191 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
195 * @param sem semaphore to delete
197 void sys_sem_free(sys_sem_t *sem);
198 /** Wait for a semaphore - forever/no timeout */
199 #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0)
200 #ifndef sys_sem_valid
203 * Check if a semaphore is valid/allocated: return 1 for valid, 0 for invalid
205 int sys_sem_valid(sys_sem_t *sem);
207 #ifndef sys_sem_set_invalid
210 * Set a semaphore invalid so that sys_sem_valid returns 0
212 void sys_sem_set_invalid(sys_sem_t *sem);
214 #ifndef sys_sem_valid_val
216 * Same as sys_sem_valid() but taking a value, not a pointer
218 #define sys_sem_valid_val(sem) sys_sem_valid(&(sem))
220 #ifndef sys_sem_set_invalid_val
222 * Same as sys_sem_set_invalid() but taking a value, not a pointer
224 #define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem))
230 * Sleep for specified number of ms
232 void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */
235 /* Mailbox functions. */
239 * Create a new mbox of specified size
240 * @param mbox pointer to the mbox to create
241 * @param size (minimum) number of messages in this mbox
242 * @return ERR_OK if successful, another err_t otherwise
244 err_t sys_mbox_new(sys_mbox_t *mbox, int size);
247 * Post a message to an mbox - may not fail
248 * -> blocks if full, only used from tasks not from ISR
249 * @param mbox mbox to posts the message
250 * @param msg message to post (ATTENTION: can be NULL)
252 void sys_mbox_post(sys_mbox_t *mbox, void *msg);
255 * Try to post a message to an mbox - may fail if full or ISR
256 * @param mbox mbox to posts the message
257 * @param msg message to post (ATTENTION: can be NULL)
259 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
262 * Wait for a new message to arrive in the mbox
263 * @param mbox mbox to get a message from
264 * @param msg pointer where the message is stored
265 * @param timeout maximum time (in milliseconds) to wait for a message (0 = wait forever)
266 * @return time (in milliseconds) waited for a message, may be 0 if not waited
267 or SYS_ARCH_TIMEOUT on timeout
268 * The returned time has to be accurate to prevent timer jitter!
270 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
271 /* Allow port to override with a macro, e.g. special timeout for sys_arch_mbox_fetch() */
272 #ifndef sys_arch_mbox_tryfetch
275 * Wait for a new message to arrive in the mbox
276 * @param mbox mbox to get a message from
277 * @param msg pointer where the message is stored
278 * @return 0 (milliseconds) if a message has been received
279 * or SYS_MBOX_EMPTY if the mailbox is empty
281 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
284 * For now, we map straight to sys_arch implementation.
286 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
290 * @param mbox mbox to delete
292 void sys_mbox_free(sys_mbox_t *mbox);
293 #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
294 #ifndef sys_mbox_valid
297 * Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid
299 int sys_mbox_valid(sys_mbox_t *mbox);
301 #ifndef sys_mbox_set_invalid
304 * Set an mbox invalid so that sys_mbox_valid returns 0
306 void sys_mbox_set_invalid(sys_mbox_t *mbox);
308 #ifndef sys_mbox_valid_val
310 * Same as sys_mbox_valid() but taking a value, not a pointer
312 #define sys_mbox_valid_val(mbox) sys_mbox_valid(&(mbox))
314 #ifndef sys_mbox_set_invalid_val
316 * Same as sys_mbox_set_invalid() but taking a value, not a pointer
318 #define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox))
324 * The only thread function:
325 * Creates a new thread
326 * ATTENTION: although this function returns a value, it MUST NOT FAIL (ports have to assert this!)
327 * @param name human-readable name for the thread (used for debugging purposes)
328 * @param thread thread-function
329 * @param arg parameter passed to 'thread'
330 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
331 * @param prio priority of the new thread (may be ignored by ports) */
332 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);
335 * Finishes an existing thread
336 * @param thread identifier returned by sys_thread_new
338 err_t sys_thread_finish(sys_thread_t t);
342 /* sys_init() must be called before anything else. */
347 * Ticks/jiffies since power up.
349 u32_t sys_jiffies(void);
354 * Returns the current time in milliseconds,
355 * may be the same as sys_jiffies or at least based on it.
359 /* Critical Region Protection */
360 /* These functions must be implemented in the sys_arch.c file.
361 In some implementations they can provide a more light-weight protection
362 mechanism than using semaphores. Otherwise semaphores can be used for
364 #ifndef SYS_ARCH_PROTECT
365 /** SYS_LIGHTWEIGHT_PROT
366 * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
367 * for certain critical regions during buffer allocation, deallocation and memory
368 * allocation and deallocation.
370 #if SYS_LIGHTWEIGHT_PROT
374 * SYS_ARCH_DECL_PROTECT
375 * declare a protection variable. This macro will default to defining a variable of
376 * type sys_prot_t. If a particular port needs a different implementation, then
377 * this macro may be defined in sys_arch.h.
379 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
383 * Perform a "fast" protect. This could be implemented by
384 * disabling interrupts for an embedded system or by using a semaphore or
385 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
386 * already protected. The old protection level is returned in the variable
387 * "lev". This macro will default to calling the sys_arch_protect() function
388 * which should be implemented in sys_arch.c. If a particular port needs a
389 * different implementation, then this macro may be defined in sys_arch.h
391 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
395 * Perform a "fast" set of the protection level to "lev". This could be
396 * implemented by setting the interrupt level to "lev" within the MACRO or by
397 * using a semaphore or mutex. This macro will default to calling the
398 * sys_arch_unprotect() function which should be implemented in
399 * sys_arch.c. If a particular port needs a different implementation, then
400 * this macro may be defined in sys_arch.h
402 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
403 sys_prot_t sys_arch_protect(void);
404 void sys_arch_unprotect(sys_prot_t pval);
408 #define SYS_ARCH_DECL_PROTECT(lev)
409 #define SYS_ARCH_PROTECT(lev)
410 #define SYS_ARCH_UNPROTECT(lev)
412 #endif /* SYS_LIGHTWEIGHT_PROT */
414 #endif /* SYS_ARCH_PROTECT */
417 * Macros to set/get and increase/decrease variables in a thread-safe way.
418 * Use these for accessing variable that are used from more than one thread.
422 #define SYS_ARCH_INC(var, val) do { \
423 SYS_ARCH_DECL_PROTECT(old_level); \
424 SYS_ARCH_PROTECT(old_level); \
426 SYS_ARCH_UNPROTECT(old_level); \
428 #endif /* SYS_ARCH_INC */
431 #define SYS_ARCH_DEC(var, val) do { \
432 SYS_ARCH_DECL_PROTECT(old_level); \
433 SYS_ARCH_PROTECT(old_level); \
435 SYS_ARCH_UNPROTECT(old_level); \
437 #endif /* SYS_ARCH_DEC */
440 #define SYS_ARCH_GET(var, ret) do { \
441 SYS_ARCH_DECL_PROTECT(old_level); \
442 SYS_ARCH_PROTECT(old_level); \
444 SYS_ARCH_UNPROTECT(old_level); \
446 #endif /* SYS_ARCH_GET */
449 #define SYS_ARCH_SET(var, val) do { \
450 SYS_ARCH_DECL_PROTECT(old_level); \
451 SYS_ARCH_PROTECT(old_level); \
453 SYS_ARCH_UNPROTECT(old_level); \
455 #endif /* SYS_ARCH_SET */
462 #endif /* LWIP_HDR_SYS_H */