[LWIP]
[reactos.git] / lib / drivers / lwip / src / include / lwip / sys.h
1 /*
2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32 #ifndef __LWIP_SYS_H__
33 #define __LWIP_SYS_H__
34
35 #include "lwip/opt.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #if NO_SYS
42
43 /* For a totally minimal and standalone system, we provide null
44 definitions of the sys_ functions. */
45 typedef u8_t sys_sem_t;
46 typedef u8_t sys_mbox_t;
47 struct sys_timeo {u8_t dummy;};
48
49 #define sys_init()
50 #define sys_timeout(m,h,a)
51 #define sys_untimeout(m,a)
52 #define sys_sem_new(c) c
53 #define sys_sem_signal(s)
54 #define sys_sem_wait(s)
55 #define sys_sem_wait_timeout(s,t)
56 #define sys_arch_sem_wait(s,t)
57 #define sys_sem_free(s)
58 #define sys_mbox_new(s) 0
59 #define sys_mbox_fetch(m,d)
60 #define sys_mbox_tryfetch(m,d)
61 #define sys_mbox_post(m,d)
62 #define sys_mbox_trypost(m,d)
63 #define sys_mbox_free(m)
64
65 #define sys_thread_new(n,t,a,s,p)
66
67 #else /* NO_SYS */
68
69 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
70 #define SYS_ARCH_TIMEOUT 0xffffffffUL
71
72 /* sys_mbox_tryfetch returns SYS_MBOX_EMPTY if appropriate.
73 * For now we use the same magic value, but we allow this to change in future.
74 */
75 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
76
77 #include "lwip/err.h"
78 #include "arch/sys_arch.h"
79
80 typedef void (* sys_timeout_handler)(void *arg);
81
82 struct sys_timeo {
83 struct sys_timeo *next;
84 u32_t time;
85 sys_timeout_handler h;
86 void *arg;
87 };
88
89 struct sys_timeouts {
90 struct sys_timeo *next;
91 };
92
93 /* sys_init() must be called before anthing else. */
94 void sys_init(void);
95
96 /*
97 * sys_timeout():
98 *
99 * Schedule a timeout a specified amount of milliseconds in the
100 * future. When the timeout occurs, the specified timeout handler will
101 * be called. The handler will be passed the "arg" argument when
102 * called.
103 *
104 */
105 void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg);
106 void sys_untimeout(sys_timeout_handler h, void *arg);
107 struct sys_timeouts *sys_arch_timeouts(void);
108
109 /* Semaphore functions. */
110 sys_sem_t sys_sem_new(u8_t count);
111 void sys_sem_signal(sys_sem_t sem);
112 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout);
113 void sys_sem_free(sys_sem_t sem);
114 void sys_sem_wait(sys_sem_t sem);
115 int sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout);
116
117 /* Time functions. */
118 #ifndef sys_msleep
119 void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
120 #endif
121 #ifndef sys_jiffies
122 u32_t sys_jiffies(void); /* since power up. */
123 #endif
124
125 /* Mailbox functions. */
126 sys_mbox_t sys_mbox_new(int size);
127 void sys_mbox_post(sys_mbox_t mbox, void *msg);
128 err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg);
129 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout);
130 #ifndef sys_arch_mbox_tryfetch /* Allow port to override with a macro */
131 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg);
132 #endif
133 /* For now, we map straight to sys_arch implementation. */
134 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
135 void sys_mbox_free(sys_mbox_t mbox);
136 void sys_mbox_fetch(sys_mbox_t mbox, void **msg);
137
138 /* Thread functions. */
139 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio);
140
141 #endif /* NO_SYS */
142
143 /** Returns the current time in milliseconds. */
144 u32_t sys_now(void);
145
146 /* Critical Region Protection */
147 /* These functions must be implemented in the sys_arch.c file.
148 In some implementations they can provide a more light-weight protection
149 mechanism than using semaphores. Otherwise semaphores can be used for
150 implementation */
151 #ifndef SYS_ARCH_PROTECT
152 /** SYS_LIGHTWEIGHT_PROT
153 * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
154 * for certain critical regions during buffer allocation, deallocation and memory
155 * allocation and deallocation.
156 */
157 #if SYS_LIGHTWEIGHT_PROT
158
159 /** SYS_ARCH_DECL_PROTECT
160 * declare a protection variable. This macro will default to defining a variable of
161 * type sys_prot_t. If a particular port needs a different implementation, then
162 * this macro may be defined in sys_arch.h.
163 */
164 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
165 /** SYS_ARCH_PROTECT
166 * Perform a "fast" protect. This could be implemented by
167 * disabling interrupts for an embedded system or by using a semaphore or
168 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
169 * already protected. The old protection level is returned in the variable
170 * "lev". This macro will default to calling the sys_arch_protect() function
171 * which should be implemented in sys_arch.c. If a particular port needs a
172 * different implementation, then this macro may be defined in sys_arch.h
173 */
174 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
175 /** SYS_ARCH_UNPROTECT
176 * Perform a "fast" set of the protection level to "lev". This could be
177 * implemented by setting the interrupt level to "lev" within the MACRO or by
178 * using a semaphore or mutex. This macro will default to calling the
179 * sys_arch_unprotect() function which should be implemented in
180 * sys_arch.c. If a particular port needs a different implementation, then
181 * this macro may be defined in sys_arch.h
182 */
183 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
184 sys_prot_t sys_arch_protect(void);
185 void sys_arch_unprotect(sys_prot_t pval);
186
187 #else
188
189 #define SYS_ARCH_DECL_PROTECT(lev)
190 #define SYS_ARCH_PROTECT(lev)
191 #define SYS_ARCH_UNPROTECT(lev)
192
193 #endif /* SYS_LIGHTWEIGHT_PROT */
194
195 #endif /* SYS_ARCH_PROTECT */
196
197 /*
198 * Macros to set/get and increase/decrease variables in a thread-safe way.
199 * Use these for accessing variable that are used from more than one thread.
200 */
201
202 #ifndef SYS_ARCH_INC
203 #define SYS_ARCH_INC(var, val) do { \
204 SYS_ARCH_DECL_PROTECT(old_level); \
205 SYS_ARCH_PROTECT(old_level); \
206 var += val; \
207 SYS_ARCH_UNPROTECT(old_level); \
208 } while(0)
209 #endif /* SYS_ARCH_INC */
210
211 #ifndef SYS_ARCH_DEC
212 #define SYS_ARCH_DEC(var, val) do { \
213 SYS_ARCH_DECL_PROTECT(old_level); \
214 SYS_ARCH_PROTECT(old_level); \
215 var -= val; \
216 SYS_ARCH_UNPROTECT(old_level); \
217 } while(0)
218 #endif /* SYS_ARCH_DEC */
219
220 #ifndef SYS_ARCH_GET
221 #define SYS_ARCH_GET(var, ret) do { \
222 SYS_ARCH_DECL_PROTECT(old_level); \
223 SYS_ARCH_PROTECT(old_level); \
224 ret = var; \
225 SYS_ARCH_UNPROTECT(old_level); \
226 } while(0)
227 #endif /* SYS_ARCH_GET */
228
229 #ifndef SYS_ARCH_SET
230 #define SYS_ARCH_SET(var, val) do { \
231 SYS_ARCH_DECL_PROTECT(old_level); \
232 SYS_ARCH_PROTECT(old_level); \
233 var = val; \
234 SYS_ARCH_UNPROTECT(old_level); \
235 } while(0)
236 #endif /* SYS_ARCH_SET */
237
238
239 #ifdef __cplusplus
240 }
241 #endif
242
243 #endif /* __LWIP_SYS_H__ */