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)
86 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
87 #define SYS_ARCH_TIMEOUT 0xffffffffUL
89 /** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.
90 * For now we use the same magic value, but we allow this to change in future.
92 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
95 #include "arch/sys_arch.h"
97 /** Function prototype for thread functions */
98 typedef void (*lwip_thread_fn
)(void *arg
);
100 /* Function prototypes for functions to be implemented by platform ports
103 /* Mutex functions: */
105 /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores
106 should be used instead */
107 #ifndef LWIP_COMPAT_MUTEX
108 #define LWIP_COMPAT_MUTEX 0
111 #if LWIP_COMPAT_MUTEX
112 /* for old ports that don't have mutexes: define them to binary semaphores */
113 #define sys_mutex_t sys_sem_t
114 #define sys_mutex_new(mutex) sys_sem_new(mutex, 1)
115 #define sys_mutex_lock(mutex) sys_sem_wait(mutex)
116 #define sys_mutex_unlock(mutex) sys_sem_signal(mutex)
117 #define sys_mutex_free(mutex) sys_sem_free(mutex)
118 #define sys_mutex_valid(mutex) sys_sem_valid(mutex)
119 #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex)
121 #else /* LWIP_COMPAT_MUTEX */
125 * Create a new mutex.
126 * Note that mutexes are expected to not be taken recursively by the lwIP code,
127 * so both implementation types (recursive or non-recursive) should work.
128 * @param mutex pointer to the mutex to create
129 * @return ERR_OK if successful, another err_t otherwise
131 err_t
sys_mutex_new(sys_mutex_t
*mutex
);
135 * @param mutex the mutex to lock
137 void sys_mutex_lock(sys_mutex_t
*mutex
);
141 * @param mutex the mutex to unlock
143 void sys_mutex_unlock(sys_mutex_t
*mutex
);
147 * @param mutex the mutex to delete
149 void sys_mutex_free(sys_mutex_t
*mutex
);
150 #ifndef sys_mutex_valid
153 * Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid
155 int sys_mutex_valid(sys_mutex_t
*mutex
);
157 #ifndef sys_mutex_set_invalid
160 * Set a mutex invalid so that sys_mutex_valid returns 0
162 void sys_mutex_set_invalid(sys_mutex_t
*mutex
);
164 #endif /* LWIP_COMPAT_MUTEX */
166 /* Semaphore functions: */
170 * Create a new semaphore
171 * @param sem pointer to the semaphore to create
172 * @param count initial count of the semaphore
173 * @return ERR_OK if successful, another err_t otherwise
175 err_t
sys_sem_new(sys_sem_t
*sem
, u8_t count
);
178 * Signals a semaphore
179 * @param sem the semaphore to signal
181 void sys_sem_signal(sys_sem_t
*sem
);
184 * Wait for a semaphore for the specified timeout
185 * @param sem the semaphore to wait for
186 * @param timeout timeout in milliseconds to wait (0 = wait forever)
187 * @return time (in milliseconds) waited for the semaphore
188 * or SYS_ARCH_TIMEOUT on timeout
190 u32_t
sys_arch_sem_wait(sys_sem_t
*sem
, u32_t timeout
);
194 * @param sem semaphore to delete
196 void sys_sem_free(sys_sem_t
*sem
);
197 /** Wait for a semaphore - forever/no timeout */
198 #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0)
199 #ifndef sys_sem_valid
202 * Check if a semaphore is valid/allocated: return 1 for valid, 0 for invalid
204 int sys_sem_valid(sys_sem_t
*sem
);
206 #ifndef sys_sem_set_invalid
209 * Set a semaphore invalid so that sys_sem_valid returns 0
211 void sys_sem_set_invalid(sys_sem_t
*sem
);
213 #ifndef sys_sem_valid_val
215 * Same as sys_sem_valid() but taking a value, not a pointer
217 #define sys_sem_valid_val(sem) sys_sem_valid(&(sem))
219 #ifndef sys_sem_set_invalid_val
221 * Same as sys_sem_set_invalid() but taking a value, not a pointer
223 #define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem))
229 * Sleep for specified number of ms
231 void sys_msleep(u32_t ms
); /* only has a (close to) 1 ms resolution. */
234 /* Mailbox functions. */
238 * Create a new mbox of specified size
239 * @param mbox pointer to the mbox to create
240 * @param size (minimum) number of messages in this mbox
241 * @return ERR_OK if successful, another err_t otherwise
243 err_t
sys_mbox_new(sys_mbox_t
*mbox
, int size
);
246 * Post a message to an mbox - may not fail
247 * -> blocks if full, only used from tasks not from ISR
248 * @param mbox mbox to posts the message
249 * @param msg message to post (ATTENTION: can be NULL)
251 void sys_mbox_post(sys_mbox_t
*mbox
, void *msg
);
254 * Try to post a message to an mbox - may fail if full or ISR
255 * @param mbox mbox to posts the message
256 * @param msg message to post (ATTENTION: can be NULL)
258 err_t
sys_mbox_trypost(sys_mbox_t
*mbox
, void *msg
);
261 * Wait for a new message to arrive in the mbox
262 * @param mbox mbox to get a message from
263 * @param msg pointer where the message is stored
264 * @param timeout maximum time (in milliseconds) to wait for a message (0 = wait forever)
265 * @return time (in milliseconds) waited for a message, may be 0 if not waited
266 or SYS_ARCH_TIMEOUT on timeout
267 * The returned time has to be accurate to prevent timer jitter!
269 u32_t
sys_arch_mbox_fetch(sys_mbox_t
*mbox
, void **msg
, u32_t timeout
);
270 /* Allow port to override with a macro, e.g. special timeout for sys_arch_mbox_fetch() */
271 #ifndef sys_arch_mbox_tryfetch
274 * Wait for a new message to arrive in the mbox
275 * @param mbox mbox to get a message from
276 * @param msg pointer where the message is stored
277 * @return 0 (milliseconds) if a message has been received
278 * or SYS_MBOX_EMPTY if the mailbox is empty
280 u32_t
sys_arch_mbox_tryfetch(sys_mbox_t
*mbox
, void **msg
);
283 * For now, we map straight to sys_arch implementation.
285 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
289 * @param mbox mbox to delete
291 void sys_mbox_free(sys_mbox_t
*mbox
);
292 #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
293 #ifndef sys_mbox_valid
296 * Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid
298 int sys_mbox_valid(sys_mbox_t
*mbox
);
300 #ifndef sys_mbox_set_invalid
303 * Set an mbox invalid so that sys_mbox_valid returns 0
305 void sys_mbox_set_invalid(sys_mbox_t
*mbox
);
307 #ifndef sys_mbox_valid_val
309 * Same as sys_mbox_valid() but taking a value, not a pointer
311 #define sys_mbox_valid_val(mbox) sys_mbox_valid(&(mbox))
313 #ifndef sys_mbox_set_invalid_val
315 * Same as sys_mbox_set_invalid() but taking a value, not a pointer
317 #define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox))
323 * The only thread function:
324 * Creates a new thread
325 * ATTENTION: although this function returns a value, it MUST NOT FAIL (ports have to assert this!)
326 * @param name human-readable name for the thread (used for debugging purposes)
327 * @param thread thread-function
328 * @param arg parameter passed to 'thread'
329 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
330 * @param prio priority of the new thread (may be ignored by ports) */
331 sys_thread_t
sys_thread_new(const char *name
, lwip_thread_fn thread
, void *arg
, int stacksize
, int prio
);
335 /* sys_init() must be called before anything else. */
340 * Ticks/jiffies since power up.
342 u32_t
sys_jiffies(void);
347 * Returns the current time in milliseconds,
348 * may be the same as sys_jiffies or at least based on it.
352 /* Critical Region Protection */
353 /* These functions must be implemented in the sys_arch.c file.
354 In some implementations they can provide a more light-weight protection
355 mechanism than using semaphores. Otherwise semaphores can be used for
357 #ifndef SYS_ARCH_PROTECT
358 /** SYS_LIGHTWEIGHT_PROT
359 * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
360 * for certain critical regions during buffer allocation, deallocation and memory
361 * allocation and deallocation.
363 #if SYS_LIGHTWEIGHT_PROT
367 * SYS_ARCH_DECL_PROTECT
368 * declare a protection variable. This macro will default to defining a variable of
369 * type sys_prot_t. If a particular port needs a different implementation, then
370 * this macro may be defined in sys_arch.h.
372 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
376 * Perform a "fast" protect. This could be implemented by
377 * disabling interrupts for an embedded system or by using a semaphore or
378 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
379 * already protected. The old protection level is returned in the variable
380 * "lev". This macro will default to calling the sys_arch_protect() function
381 * which should be implemented in sys_arch.c. If a particular port needs a
382 * different implementation, then this macro may be defined in sys_arch.h
384 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
388 * Perform a "fast" set of the protection level to "lev". This could be
389 * implemented by setting the interrupt level to "lev" within the MACRO or by
390 * using a semaphore or mutex. This macro will default to calling the
391 * sys_arch_unprotect() function which should be implemented in
392 * sys_arch.c. If a particular port needs a different implementation, then
393 * this macro may be defined in sys_arch.h
395 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
396 sys_prot_t
sys_arch_protect(void);
397 void sys_arch_unprotect(sys_prot_t pval
);
401 #define SYS_ARCH_DECL_PROTECT(lev)
402 #define SYS_ARCH_PROTECT(lev)
403 #define SYS_ARCH_UNPROTECT(lev)
405 #endif /* SYS_LIGHTWEIGHT_PROT */
407 #endif /* SYS_ARCH_PROTECT */
410 * Macros to set/get and increase/decrease variables in a thread-safe way.
411 * Use these for accessing variable that are used from more than one thread.
415 #define SYS_ARCH_INC(var, val) do { \
416 SYS_ARCH_DECL_PROTECT(old_level); \
417 SYS_ARCH_PROTECT(old_level); \
419 SYS_ARCH_UNPROTECT(old_level); \
421 #endif /* SYS_ARCH_INC */
424 #define SYS_ARCH_DEC(var, val) do { \
425 SYS_ARCH_DECL_PROTECT(old_level); \
426 SYS_ARCH_PROTECT(old_level); \
428 SYS_ARCH_UNPROTECT(old_level); \
430 #endif /* SYS_ARCH_DEC */
433 #define SYS_ARCH_GET(var, ret) do { \
434 SYS_ARCH_DECL_PROTECT(old_level); \
435 SYS_ARCH_PROTECT(old_level); \
437 SYS_ARCH_UNPROTECT(old_level); \
439 #endif /* SYS_ARCH_GET */
442 #define SYS_ARCH_SET(var, val) do { \
443 SYS_ARCH_DECL_PROTECT(old_level); \
444 SYS_ARCH_PROTECT(old_level); \
446 SYS_ARCH_UNPROTECT(old_level); \
448 #endif /* SYS_ARCH_SET */
455 #endif /* LWIP_HDR_SYS_H */