Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / lwip / repo / lwip / src / include / lwip / sys.h
1 /**
2  * @file
3  * OS abstraction layer
4  */
5
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
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.
20  *
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
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  */
36
37 #ifndef LWIP_HDR_SYS_H
38 #define LWIP_HDR_SYS_H
39
40 #include "lwip/opt.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 #if NO_SYS
47
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;
53
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)
79
80 #define sys_thread_new(n,t,a,s,p)
81 #define sys_thread_finish(a,b)
82
83 #define sys_msleep(t)
84
85 #else /* NO_SYS */
86
87 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
88 #define SYS_ARCH_TIMEOUT 0xffffffffUL
89
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.
92  */
93 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
94
95 #include "lwip/err.h"
96 #include "arch/sys_arch.h"
97
98 /** Function prototype for thread functions */
99 typedef void (*lwip_thread_fn)(void *arg);
100
101 /* Function prototypes for functions to be implemented by platform ports
102    (in sys_arch.c) */
103
104 /* Mutex functions: */
105
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
110 #endif
111
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)
121
122 #else /* LWIP_COMPAT_MUTEX */
123
124 /**
125  * @ingroup sys_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
131  */
132 err_t sys_mutex_new(sys_mutex_t *mutex);
133 /**
134  * @ingroup sys_mutex
135  * Lock a mutex
136  * @param mutex the mutex to lock
137  */
138 void sys_mutex_lock(sys_mutex_t *mutex);
139 /**
140  * @ingroup sys_mutex
141  * Unlock a mutex
142  * @param mutex the mutex to unlock
143  */
144 void sys_mutex_unlock(sys_mutex_t *mutex);
145 /**
146  * @ingroup sys_mutex
147  * Delete a semaphore
148  * @param mutex the mutex to delete
149  */
150 void sys_mutex_free(sys_mutex_t *mutex);
151 #ifndef sys_mutex_valid
152 /**
153  * @ingroup sys_mutex
154  * Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid
155  */
156 int sys_mutex_valid(sys_mutex_t *mutex);
157 #endif
158 #ifndef sys_mutex_set_invalid
159 /**
160  * @ingroup sys_mutex
161  * Set a mutex invalid so that sys_mutex_valid returns 0
162  */
163 void sys_mutex_set_invalid(sys_mutex_t *mutex);
164 #endif
165 #endif /* LWIP_COMPAT_MUTEX */
166
167 /* Semaphore functions: */
168
169 /**
170  * @ingroup sys_sem
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
175  */
176 err_t sys_sem_new(sys_sem_t *sem, u8_t count);
177 /**
178  * @ingroup sys_sem
179  * Signals a semaphore
180  * @param sem the semaphore to signal
181  */
182 void sys_sem_signal(sys_sem_t *sem);
183 /**
184  * @ingroup sys_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
190  */
191 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
192 /**
193  * @ingroup sys_sem
194  * Delete a semaphore
195  * @param sem semaphore to delete
196  */
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
201 /**
202  * @ingroup sys_sem
203  * Check if a semaphore is valid/allocated: return 1 for valid, 0 for invalid
204  */
205 int sys_sem_valid(sys_sem_t *sem);
206 #endif
207 #ifndef sys_sem_set_invalid
208 /**
209  * @ingroup sys_sem
210  * Set a semaphore invalid so that sys_sem_valid returns 0
211  */
212 void sys_sem_set_invalid(sys_sem_t *sem);
213 #endif
214 #ifndef sys_sem_valid_val
215 /**
216  * Same as sys_sem_valid() but taking a value, not a pointer
217  */
218 #define sys_sem_valid_val(sem)       sys_sem_valid(&(sem))
219 #endif
220 #ifndef sys_sem_set_invalid_val
221 /**
222  * Same as sys_sem_set_invalid() but taking a value, not a pointer
223  */
224 #define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem))
225 #endif
226
227 #ifndef sys_msleep
228 /**
229  * @ingroup sys_misc
230  * Sleep for specified number of ms
231  */
232 void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */
233 #endif
234
235 /* Mailbox functions. */
236
237 /**
238  * @ingroup sys_mbox
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
243  */
244 err_t sys_mbox_new(sys_mbox_t *mbox, int size);
245 /**
246  * @ingroup sys_mbox
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)
251  */
252 void sys_mbox_post(sys_mbox_t *mbox, void *msg);
253 /**
254  * @ingroup sys_mbox
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)
258  */
259 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
260 /**
261  * @ingroup sys_mbox
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!
269  */
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
273 /**
274  * @ingroup sys_mbox
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
280  */
281 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
282 #endif
283 /**
284  * For now, we map straight to sys_arch implementation.
285  */
286 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
287 /**
288  * @ingroup sys_mbox
289  * Delete an mbox
290  * @param mbox mbox to delete
291  */
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
295 /**
296  * @ingroup sys_mbox
297  * Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid
298  */
299 int sys_mbox_valid(sys_mbox_t *mbox);
300 #endif
301 #ifndef sys_mbox_set_invalid
302 /**
303  * @ingroup sys_mbox
304  * Set an mbox invalid so that sys_mbox_valid returns 0
305  */
306 void sys_mbox_set_invalid(sys_mbox_t *mbox);
307 #endif
308 #ifndef sys_mbox_valid_val
309 /**
310  * Same as sys_mbox_valid() but taking a value, not a pointer
311  */
312 #define sys_mbox_valid_val(mbox)       sys_mbox_valid(&(mbox))
313 #endif
314 #ifndef sys_mbox_set_invalid_val
315 /**
316  * Same as sys_mbox_set_invalid() but taking a value, not a pointer
317  */
318 #define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox))
319 #endif
320
321
322 /**
323  * @ingroup sys_misc
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);
333
334 /**
335  * Finishes an existing thread
336  * @param thread identifier returned by sys_thread_new
337  */
338 err_t sys_thread_finish(sys_thread_t t);
339
340 #endif /* NO_SYS */
341
342 /* sys_init() must be called before anything else. */
343 void sys_init(void);
344
345 #ifndef sys_jiffies
346 /**
347  * Ticks/jiffies since power up.
348  */
349 u32_t sys_jiffies(void);
350 #endif
351
352 /**
353  * @ingroup sys_time
354  * Returns the current time in milliseconds,
355  * may be the same as sys_jiffies or at least based on it.
356  */
357 u32_t sys_now(void);
358
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
363    implementation */
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.
369  */
370 #if SYS_LIGHTWEIGHT_PROT
371
372 /**
373  * @ingroup sys_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.
378  */
379 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
380 /**
381  * @ingroup sys_prot
382  * SYS_ARCH_PROTECT
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
390  */
391 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
392 /**
393  * @ingroup sys_prot
394  * SYS_ARCH_UNPROTECT
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
401  */
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);
405
406 #else
407
408 #define SYS_ARCH_DECL_PROTECT(lev)
409 #define SYS_ARCH_PROTECT(lev)
410 #define SYS_ARCH_UNPROTECT(lev)
411
412 #endif /* SYS_LIGHTWEIGHT_PROT */
413
414 #endif /* SYS_ARCH_PROTECT */
415
416 /*
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.
419  */
420
421 #ifndef SYS_ARCH_INC
422 #define SYS_ARCH_INC(var, val) do { \
423                                 SYS_ARCH_DECL_PROTECT(old_level); \
424                                 SYS_ARCH_PROTECT(old_level); \
425                                 var += val; \
426                                 SYS_ARCH_UNPROTECT(old_level); \
427                               } while(0)
428 #endif /* SYS_ARCH_INC */
429
430 #ifndef SYS_ARCH_DEC
431 #define SYS_ARCH_DEC(var, val) do { \
432                                 SYS_ARCH_DECL_PROTECT(old_level); \
433                                 SYS_ARCH_PROTECT(old_level); \
434                                 var -= val; \
435                                 SYS_ARCH_UNPROTECT(old_level); \
436                               } while(0)
437 #endif /* SYS_ARCH_DEC */
438
439 #ifndef SYS_ARCH_GET
440 #define SYS_ARCH_GET(var, ret) do { \
441                                 SYS_ARCH_DECL_PROTECT(old_level); \
442                                 SYS_ARCH_PROTECT(old_level); \
443                                 ret = var; \
444                                 SYS_ARCH_UNPROTECT(old_level); \
445                               } while(0)
446 #endif /* SYS_ARCH_GET */
447
448 #ifndef SYS_ARCH_SET
449 #define SYS_ARCH_SET(var, val) do { \
450                                 SYS_ARCH_DECL_PROTECT(old_level); \
451                                 SYS_ARCH_PROTECT(old_level); \
452                                 var = val; \
453                                 SYS_ARCH_UNPROTECT(old_level); \
454                               } while(0)
455 #endif /* SYS_ARCH_SET */
456
457
458 #ifdef __cplusplus
459 }
460 #endif
461
462 #endif /* LWIP_HDR_SYS_H */