The Pedigree Project  0.1
sys.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
25 /*
26  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without modification,
30  * are permitted provided that the following conditions are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright notice,
33  * this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright notice,
35  * this list of conditions and the following disclaimer in the documentation
36  * and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  * derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49  * OF SUCH DAMAGE.
50  *
51  * This file is part of the lwIP TCP/IP stack.
52  *
53  * Author: Adam Dunkels <adam@sics.se>
54  */
55 
56 #ifndef LWIP_HDR_SYS_H
57 #define LWIP_HDR_SYS_H
58 
59 #include "lwip/opt.h"
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 #if NO_SYS
66 
67 /* For a totally minimal and standalone system, we provide null
68  definitions of the sys_ functions. */
69 typedef u8_t sys_sem_t;
70 typedef u8_t sys_mutex_t;
71 typedef u8_t sys_mbox_t;
72 
73 #define sys_sem_new(s, c) ERR_OK
74 #define sys_sem_signal(s)
75 #define sys_sem_wait(s)
76 #define sys_arch_sem_wait(s,t)
77 #define sys_sem_free(s)
78 #define sys_sem_valid(s) 0
79 #define sys_sem_valid_val(s) 0
80 #define sys_sem_set_invalid(s)
81 #define sys_sem_set_invalid_val(s)
82 #define sys_mutex_new(mu) ERR_OK
83 #define sys_mutex_lock(mu)
84 #define sys_mutex_unlock(mu)
85 #define sys_mutex_free(mu)
86 #define sys_mutex_valid(mu) 0
87 #define sys_mutex_set_invalid(mu)
88 #define sys_mbox_new(m, s) ERR_OK
89 #define sys_mbox_fetch(m,d)
90 #define sys_mbox_tryfetch(m,d)
91 #define sys_mbox_post(m,d)
92 #define sys_mbox_trypost(m,d)
93 #define sys_mbox_free(m)
94 #define sys_mbox_valid(m)
95 #define sys_mbox_valid_val(m)
96 #define sys_mbox_set_invalid(m)
97 #define sys_mbox_set_invalid_val(m)
98 
99 #define sys_thread_new(n,t,a,s,p)
100 
101 #define sys_msleep(t)
102 
103 #else /* NO_SYS */
104 
106 #define SYS_ARCH_TIMEOUT 0xffffffffUL
107 
111 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
112 
113 #include "lwip/err.h"
114 #include "arch/sys_arch.h"
115 
117 typedef void (*lwip_thread_fn)(void *arg);
118 
119 /* Function prototypes for functions to be implemented by platform ports
120  (in sys_arch.c) */
121 
122 /* Mutex functions: */
123 
126 #ifndef LWIP_COMPAT_MUTEX
127 #define LWIP_COMPAT_MUTEX 0
128 #endif
129 
130 #if LWIP_COMPAT_MUTEX
131 /* for old ports that don't have mutexes: define them to binary semaphores */
132 #define sys_mutex_t sys_sem_t
133 #define sys_mutex_new(mutex) sys_sem_new(mutex, 1)
134 #define sys_mutex_lock(mutex) sys_sem_wait(mutex)
135 #define sys_mutex_unlock(mutex) sys_sem_signal(mutex)
136 #define sys_mutex_free(mutex) sys_sem_free(mutex)
137 #define sys_mutex_valid(mutex) sys_sem_valid(mutex)
138 #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex)
139 
140 #else /* LWIP_COMPAT_MUTEX */
141 
150 err_t sys_mutex_new(sys_mutex_t *mutex);
156 void sys_mutex_lock(sys_mutex_t *mutex);
162 void sys_mutex_unlock(sys_mutex_t *mutex);
168 void sys_mutex_free(sys_mutex_t *mutex);
169 #ifndef sys_mutex_valid
170 
174 int sys_mutex_valid(sys_mutex_t *mutex);
175 #endif
176 #ifndef sys_mutex_set_invalid
177 
181 void sys_mutex_set_invalid(sys_mutex_t *mutex);
182 #endif
183 #endif /* LWIP_COMPAT_MUTEX */
184 
185 /* Semaphore functions: */
186 
194 err_t sys_sem_new(sys_sem_t *sem, u8_t count);
200 void sys_sem_signal(sys_sem_t *sem);
209 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
215 void sys_sem_free(sys_sem_t *sem);
217 #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0)
218 #ifndef sys_sem_valid
219 
223 int sys_sem_valid(sys_sem_t *sem);
224 #endif
225 #ifndef sys_sem_set_invalid
226 
230 void sys_sem_set_invalid(sys_sem_t *sem);
231 #endif
232 #ifndef sys_sem_valid_val
233 
236 #define sys_sem_valid_val(sem) sys_sem_valid(&(sem))
237 #endif
238 #ifndef sys_sem_set_invalid_val
239 
242 #define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem))
243 #endif
244 
245 #ifndef sys_msleep
246 
250 void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */
251 #endif
252 
253 /* Mailbox functions. */
254 
262 err_t sys_mbox_new(sys_mbox_t *mbox, int size);
270 void sys_mbox_post(sys_mbox_t *mbox, void *msg);
277 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
288 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
289 /* Allow port to override with a macro, e.g. special timeout for sys_arch_mbox_fetch() */
290 #ifndef sys_arch_mbox_tryfetch
291 
299 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
300 #endif
301 
304 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
305 
310 void sys_mbox_free(sys_mbox_t *mbox);
311 #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
312 #ifndef sys_mbox_valid
313 
317 int sys_mbox_valid(sys_mbox_t *mbox);
318 #endif
319 #ifndef sys_mbox_set_invalid
320 
324 void sys_mbox_set_invalid(sys_mbox_t *mbox);
325 #endif
326 #ifndef sys_mbox_valid_val
327 
330 #define sys_mbox_valid_val(mbox) sys_mbox_valid(&(mbox))
331 #endif
332 #ifndef sys_mbox_set_invalid_val
333 
336 #define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox))
337 #endif
338 
339 
350 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);
351 
352 #endif /* NO_SYS */
353 
354 /* sys_init() must be called before anything else. */
355 void sys_init(void);
356 
357 #ifndef sys_jiffies
358 
361 u32_t sys_jiffies(void);
362 #endif
363 
369 u32_t sys_now(void);
370 
371 /* Critical Region Protection */
372 /* These functions must be implemented in the sys_arch.c file.
373  In some implementations they can provide a more light-weight protection
374  mechanism than using semaphores. Otherwise semaphores can be used for
375  implementation */
376 #ifndef SYS_ARCH_PROTECT
377 
382 #if SYS_LIGHTWEIGHT_PROT
383 
391 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
392 
403 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
404 
414 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
415 sys_prot_t sys_arch_protect(void);
416 void sys_arch_unprotect(sys_prot_t pval);
417 
418 #else
419 
420 #define SYS_ARCH_DECL_PROTECT(lev)
421 #define SYS_ARCH_PROTECT(lev)
422 #define SYS_ARCH_UNPROTECT(lev)
423 
424 #endif /* SYS_LIGHTWEIGHT_PROT */
425 
426 #endif /* SYS_ARCH_PROTECT */
427 
428 /*
429  * Macros to set/get and increase/decrease variables in a thread-safe way.
430  * Use these for accessing variable that are used from more than one thread.
431  */
432 
433 #ifndef SYS_ARCH_INC
434 #define SYS_ARCH_INC(var, val) do { \
435  SYS_ARCH_DECL_PROTECT(old_level); \
436  SYS_ARCH_PROTECT(old_level); \
437  var += val; \
438  SYS_ARCH_UNPROTECT(old_level); \
439  } while(0)
440 #endif /* SYS_ARCH_INC */
441 
442 #ifndef SYS_ARCH_DEC
443 #define SYS_ARCH_DEC(var, val) do { \
444  SYS_ARCH_DECL_PROTECT(old_level); \
445  SYS_ARCH_PROTECT(old_level); \
446  var -= val; \
447  SYS_ARCH_UNPROTECT(old_level); \
448  } while(0)
449 #endif /* SYS_ARCH_DEC */
450 
451 #ifndef SYS_ARCH_GET
452 #define SYS_ARCH_GET(var, ret) do { \
453  SYS_ARCH_DECL_PROTECT(old_level); \
454  SYS_ARCH_PROTECT(old_level); \
455  ret = var; \
456  SYS_ARCH_UNPROTECT(old_level); \
457  } while(0)
458 #endif /* SYS_ARCH_GET */
459 
460 #ifndef SYS_ARCH_SET
461 #define SYS_ARCH_SET(var, val) do { \
462  SYS_ARCH_DECL_PROTECT(old_level); \
463  SYS_ARCH_PROTECT(old_level); \
464  var = val; \
465  SYS_ARCH_UNPROTECT(old_level); \
466  } while(0)
467 #endif /* SYS_ARCH_SET */
468 
469 
470 #ifdef __cplusplus
471 }
472 #endif
473 
474 #endif /* LWIP_HDR_SYS_H */
void sys_sem_free(sys_sem_t *sem)
Definition: sys_arch.cc:113
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition: sys_arch.cc:248
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
Definition: sys_arch.cc:157
void sys_msleep(u32_t ms)
void sys_mutex_free(sys_mutex_t *mutex)
Definition: sys_arch.cc:314
void sys_mbox_free(sys_mbox_t *mbox)
Definition: sys_arch.cc:226
void sys_mbox_set_invalid(sys_mbox_t *mbox)
Definition: sys_arch.cc:289
int sys_mbox_valid(sys_mbox_t *mbox)
Definition: sys_arch.cc:284
void sys_mutex_unlock(sys_mutex_t *mutex)
Definition: sys_arch.cc:308
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.cc:83
u32_t sys_now(void)
Definition: sys_arch.cc:57
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
Definition: sys_arch.cc:220
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.cc:232
void(* lwip_thread_fn)(void *arg)
Definition: sys.h:117
void sys_sem_signal(sys_sem_t *sem)
Definition: sys_arch.cc:147
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.cc:95
s8_t err_t
Definition: err.h:76
int sys_mutex_valid(sys_mutex_t *mutex)
Definition: sys_arch.cc:321
void sys_mutex_lock(sys_mutex_t *mutex)
Definition: sys_arch.cc:301
void sys_sem_set_invalid(sys_sem_t *sem)
Definition: sys_arch.cc:140
void sys_mutex_set_invalid(sys_mutex_t *mutex)
Definition: sys_arch.cc:326
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.cc:272
u32_t sys_jiffies(void)
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
Definition: sys_arch.cc:237
int sys_sem_valid(sys_sem_t *sem)
Definition: sys_arch.cc:124
err_t sys_mutex_new(sys_mutex_t *mutex)
Definition: sys_arch.cc:294