BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
82 | #define sys_msleep(t) |
||
83 | |||
84 | #else /* NO_SYS */ |
||
85 | |||
86 | /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */ |
||
87 | #define SYS_ARCH_TIMEOUT 0xffffffffUL |
||
88 | |||
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. |
||
91 | */ |
||
92 | #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT |
||
93 | |||
94 | #include "lwip/err.h" |
||
95 | #include "arch/sys_arch.h" |
||
96 | |||
97 | /** Function prototype for thread functions */ |
||
98 | typedef void (*lwip_thread_fn)(void *arg); |
||
99 | |||
100 | /* Function prototypes for functions to be implemented by platform ports |
||
101 | (in sys_arch.c) */ |
||
102 | |||
103 | /* Mutex functions: */ |
||
104 | |||
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 |
||
109 | #endif |
||
110 | |||
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) |
||
120 | |||
121 | #else /* LWIP_COMPAT_MUTEX */ |
||
122 | |||
123 | /** |
||
124 | * @ingroup sys_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 |
||
130 | */ |
||
131 | err_t sys_mutex_new(sys_mutex_t *mutex); |
||
132 | /** |
||
133 | * @ingroup sys_mutex |
||
134 | * Lock a mutex |
||
135 | * @param mutex the mutex to lock |
||
136 | */ |
||
137 | void sys_mutex_lock(sys_mutex_t *mutex); |
||
138 | /** |
||
139 | * @ingroup sys_mutex |
||
140 | * Unlock a mutex |
||
141 | * @param mutex the mutex to unlock |
||
142 | */ |
||
143 | void sys_mutex_unlock(sys_mutex_t *mutex); |
||
144 | /** |
||
145 | * @ingroup sys_mutex |
||
146 | * Delete a semaphore |
||
147 | * @param mutex the mutex to delete |
||
148 | */ |
||
149 | void sys_mutex_free(sys_mutex_t *mutex); |
||
150 | #ifndef sys_mutex_valid |
||
151 | /** |
||
152 | * @ingroup sys_mutex |
||
153 | * Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid |
||
154 | */ |
||
155 | int sys_mutex_valid(sys_mutex_t *mutex); |
||
156 | #endif |
||
157 | #ifndef sys_mutex_set_invalid |
||
158 | /** |
||
159 | * @ingroup sys_mutex |
||
160 | * Set a mutex invalid so that sys_mutex_valid returns 0 |
||
161 | */ |
||
162 | void sys_mutex_set_invalid(sys_mutex_t *mutex); |
||
163 | #endif |
||
164 | #endif /* LWIP_COMPAT_MUTEX */ |
||
165 | |||
166 | /* Semaphore functions: */ |
||
167 | |||
168 | /** |
||
169 | * @ingroup sys_sem |
||
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 |
||
174 | */ |
||
175 | err_t sys_sem_new(sys_sem_t *sem, u8_t count); |
||
176 | /** |
||
177 | * @ingroup sys_sem |
||
178 | * Signals a semaphore |
||
179 | * @param sem the semaphore to signal |
||
180 | */ |
||
181 | void sys_sem_signal(sys_sem_t *sem); |
||
182 | /** |
||
183 | * @ingroup sys_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 SYS_ARCH_TIMEOUT on timeout, any other value on success |
||
188 | */ |
||
189 | u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout); |
||
190 | /** |
||
191 | * @ingroup sys_sem |
||
192 | * Delete a semaphore |
||
193 | * @param sem semaphore to delete |
||
194 | */ |
||
195 | void sys_sem_free(sys_sem_t *sem); |
||
196 | /** Wait for a semaphore - forever/no timeout */ |
||
197 | #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0) |
||
198 | #ifndef sys_sem_valid |
||
199 | /** |
||
200 | * @ingroup sys_sem |
||
201 | * Check if a semaphore is valid/allocated: return 1 for valid, 0 for invalid |
||
202 | */ |
||
203 | int sys_sem_valid(sys_sem_t *sem); |
||
204 | #endif |
||
205 | #ifndef sys_sem_set_invalid |
||
206 | /** |
||
207 | * @ingroup sys_sem |
||
208 | * Set a semaphore invalid so that sys_sem_valid returns 0 |
||
209 | */ |
||
210 | void sys_sem_set_invalid(sys_sem_t *sem); |
||
211 | #endif |
||
212 | #ifndef sys_sem_valid_val |
||
213 | /** |
||
214 | * Same as sys_sem_valid() but taking a value, not a pointer |
||
215 | */ |
||
216 | #define sys_sem_valid_val(sem) sys_sem_valid(&(sem)) |
||
217 | #endif |
||
218 | #ifndef sys_sem_set_invalid_val |
||
219 | /** |
||
220 | * Same as sys_sem_set_invalid() but taking a value, not a pointer |
||
221 | */ |
||
222 | #define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem)) |
||
223 | #endif |
||
224 | |||
225 | #ifndef sys_msleep |
||
226 | /** |
||
227 | * @ingroup sys_misc |
||
228 | * Sleep for specified number of ms |
||
229 | */ |
||
230 | void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */ |
||
231 | #endif |
||
232 | |||
233 | /* Mailbox functions. */ |
||
234 | |||
235 | /** |
||
236 | * @ingroup sys_mbox |
||
237 | * Create a new mbox of specified size |
||
238 | * @param mbox pointer to the mbox to create |
||
239 | * @param size (minimum) number of messages in this mbox |
||
240 | * @return ERR_OK if successful, another err_t otherwise |
||
241 | */ |
||
242 | err_t sys_mbox_new(sys_mbox_t *mbox, int size); |
||
243 | /** |
||
244 | * @ingroup sys_mbox |
||
245 | * Post a message to an mbox - may not fail |
||
246 | * -> blocks if full, only used from tasks not from ISR |
||
247 | * @param mbox mbox to posts the message |
||
248 | * @param msg message to post (ATTENTION: can be NULL) |
||
249 | */ |
||
250 | void sys_mbox_post(sys_mbox_t *mbox, void *msg); |
||
251 | /** |
||
252 | * @ingroup sys_mbox |
||
253 | * Try to post a message to an mbox - may fail if full or ISR |
||
254 | * @param mbox mbox to posts the message |
||
255 | * @param msg message to post (ATTENTION: can be NULL) |
||
256 | */ |
||
257 | err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg); |
||
258 | /** |
||
259 | * @ingroup sys_mbox |
||
260 | * Wait for a new message to arrive in the mbox |
||
261 | * @param mbox mbox to get a message from |
||
262 | * @param msg pointer where the message is stored |
||
263 | * @param timeout maximum time (in milliseconds) to wait for a message (0 = wait forever) |
||
264 | * @return SYS_ARCH_TIMEOUT on timeout, any other value if a message has been received |
||
265 | */ |
||
266 | u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout); |
||
267 | /* Allow port to override with a macro, e.g. special timeout for sys_arch_mbox_fetch() */ |
||
268 | #ifndef sys_arch_mbox_tryfetch |
||
269 | /** |
||
270 | * @ingroup sys_mbox |
||
271 | * Wait for a new message to arrive in the mbox |
||
272 | * @param mbox mbox to get a message from |
||
273 | * @param msg pointer where the message is stored |
||
274 | * @return 0 (milliseconds) if a message has been received |
||
275 | * or SYS_MBOX_EMPTY if the mailbox is empty |
||
276 | */ |
||
277 | u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg); |
||
278 | #endif |
||
279 | /** |
||
280 | * For now, we map straight to sys_arch implementation. |
||
281 | */ |
||
282 | #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg) |
||
283 | /** |
||
284 | * @ingroup sys_mbox |
||
285 | * Delete an mbox |
||
286 | * @param mbox mbox to delete |
||
287 | */ |
||
288 | void sys_mbox_free(sys_mbox_t *mbox); |
||
289 | #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0) |
||
290 | #ifndef sys_mbox_valid |
||
291 | /** |
||
292 | * @ingroup sys_mbox |
||
293 | * Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid |
||
294 | */ |
||
295 | int sys_mbox_valid(sys_mbox_t *mbox); |
||
296 | #endif |
||
297 | #ifndef sys_mbox_set_invalid |
||
298 | /** |
||
299 | * @ingroup sys_mbox |
||
300 | * Set an mbox invalid so that sys_mbox_valid returns 0 |
||
301 | */ |
||
302 | void sys_mbox_set_invalid(sys_mbox_t *mbox); |
||
303 | #endif |
||
304 | #ifndef sys_mbox_valid_val |
||
305 | /** |
||
306 | * Same as sys_mbox_valid() but taking a value, not a pointer |
||
307 | */ |
||
308 | #define sys_mbox_valid_val(mbox) sys_mbox_valid(&(mbox)) |
||
309 | #endif |
||
310 | #ifndef sys_mbox_set_invalid_val |
||
311 | /** |
||
312 | * Same as sys_mbox_set_invalid() but taking a value, not a pointer |
||
313 | */ |
||
314 | #define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox)) |
||
315 | #endif |
||
316 | |||
317 | |||
318 | /** |
||
319 | * @ingroup sys_misc |
||
320 | * The only thread function: |
||
321 | * Creates a new thread |
||
322 | * ATTENTION: although this function returns a value, it MUST NOT FAIL (ports have to assert this!) |
||
323 | * @param name human-readable name for the thread (used for debugging purposes) |
||
324 | * @param thread thread-function |
||
325 | * @param arg parameter passed to 'thread' |
||
326 | * @param stacksize stack size in bytes for the new thread (may be ignored by ports) |
||
327 | * @param prio priority of the new thread (may be ignored by ports) */ |
||
328 | sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio); |
||
329 | |||
330 | #endif /* NO_SYS */ |
||
331 | |||
332 | /* sys_init() must be called before anything else. */ |
||
333 | void sys_init(void); |
||
334 | |||
335 | #ifndef sys_jiffies |
||
336 | /** |
||
337 | * Ticks/jiffies since power up. |
||
338 | */ |
||
339 | u32_t sys_jiffies(void); |
||
340 | #endif |
||
341 | |||
342 | /** |
||
343 | * @ingroup sys_time |
||
344 | * Returns the current time in milliseconds, |
||
345 | * may be the same as sys_jiffies or at least based on it. |
||
346 | */ |
||
347 | u32_t sys_now(void); |
||
348 | |||
349 | /* Critical Region Protection */ |
||
350 | /* These functions must be implemented in the sys_arch.c file. |
||
351 | In some implementations they can provide a more light-weight protection |
||
352 | mechanism than using semaphores. Otherwise semaphores can be used for |
||
353 | implementation */ |
||
354 | #ifndef SYS_ARCH_PROTECT |
||
355 | /** SYS_LIGHTWEIGHT_PROT |
||
356 | * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection |
||
357 | * for certain critical regions during buffer allocation, deallocation and memory |
||
358 | * allocation and deallocation. |
||
359 | */ |
||
360 | #if SYS_LIGHTWEIGHT_PROT |
||
361 | |||
362 | /** |
||
363 | * @ingroup sys_prot |
||
364 | * SYS_ARCH_DECL_PROTECT |
||
365 | * declare a protection variable. This macro will default to defining a variable of |
||
366 | * type sys_prot_t. If a particular port needs a different implementation, then |
||
367 | * this macro may be defined in sys_arch.h. |
||
368 | */ |
||
369 | #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev |
||
370 | /** |
||
371 | * @ingroup sys_prot |
||
372 | * SYS_ARCH_PROTECT |
||
373 | * Perform a "fast" protect. This could be implemented by |
||
374 | * disabling interrupts for an embedded system or by using a semaphore or |
||
375 | * mutex. The implementation should allow calling SYS_ARCH_PROTECT when |
||
376 | * already protected. The old protection level is returned in the variable |
||
377 | * "lev". This macro will default to calling the sys_arch_protect() function |
||
378 | * which should be implemented in sys_arch.c. If a particular port needs a |
||
379 | * different implementation, then this macro may be defined in sys_arch.h |
||
380 | */ |
||
381 | #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect() |
||
382 | /** |
||
383 | * @ingroup sys_prot |
||
384 | * SYS_ARCH_UNPROTECT |
||
385 | * Perform a "fast" set of the protection level to "lev". This could be |
||
386 | * implemented by setting the interrupt level to "lev" within the MACRO or by |
||
387 | * using a semaphore or mutex. This macro will default to calling the |
||
388 | * sys_arch_unprotect() function which should be implemented in |
||
389 | * sys_arch.c. If a particular port needs a different implementation, then |
||
390 | * this macro may be defined in sys_arch.h |
||
391 | */ |
||
392 | #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev) |
||
393 | sys_prot_t sys_arch_protect(void); |
||
394 | void sys_arch_unprotect(sys_prot_t pval); |
||
395 | |||
396 | #else |
||
397 | |||
398 | #define SYS_ARCH_DECL_PROTECT(lev) |
||
399 | #define SYS_ARCH_PROTECT(lev) |
||
400 | #define SYS_ARCH_UNPROTECT(lev) |
||
401 | |||
402 | #endif /* SYS_LIGHTWEIGHT_PROT */ |
||
403 | |||
404 | #endif /* SYS_ARCH_PROTECT */ |
||
405 | |||
406 | /* |
||
407 | * Macros to set/get and increase/decrease variables in a thread-safe way. |
||
408 | * Use these for accessing variable that are used from more than one thread. |
||
409 | */ |
||
410 | |||
411 | #ifndef SYS_ARCH_INC |
||
412 | #define SYS_ARCH_INC(var, val) do { \ |
||
413 | SYS_ARCH_DECL_PROTECT(old_level); \ |
||
414 | SYS_ARCH_PROTECT(old_level); \ |
||
415 | var += val; \ |
||
416 | SYS_ARCH_UNPROTECT(old_level); \ |
||
417 | } while(0) |
||
418 | #endif /* SYS_ARCH_INC */ |
||
419 | |||
420 | #ifndef SYS_ARCH_DEC |
||
421 | #define SYS_ARCH_DEC(var, val) do { \ |
||
422 | SYS_ARCH_DECL_PROTECT(old_level); \ |
||
423 | SYS_ARCH_PROTECT(old_level); \ |
||
424 | var -= val; \ |
||
425 | SYS_ARCH_UNPROTECT(old_level); \ |
||
426 | } while(0) |
||
427 | #endif /* SYS_ARCH_DEC */ |
||
428 | |||
429 | #ifndef SYS_ARCH_GET |
||
430 | #define SYS_ARCH_GET(var, ret) do { \ |
||
431 | SYS_ARCH_DECL_PROTECT(old_level); \ |
||
432 | SYS_ARCH_PROTECT(old_level); \ |
||
433 | ret = var; \ |
||
434 | SYS_ARCH_UNPROTECT(old_level); \ |
||
435 | } while(0) |
||
436 | #endif /* SYS_ARCH_GET */ |
||
437 | |||
438 | #ifndef SYS_ARCH_SET |
||
439 | #define SYS_ARCH_SET(var, val) do { \ |
||
440 | SYS_ARCH_DECL_PROTECT(old_level); \ |
||
441 | SYS_ARCH_PROTECT(old_level); \ |
||
442 | var = val; \ |
||
443 | SYS_ARCH_UNPROTECT(old_level); \ |
||
444 | } while(0) |
||
445 | #endif /* SYS_ARCH_SET */ |
||
446 | |||
447 | #ifndef SYS_ARCH_LOCKED |
||
448 | #define SYS_ARCH_LOCKED(code) do { \ |
||
449 | SYS_ARCH_DECL_PROTECT(old_level); \ |
||
450 | SYS_ARCH_PROTECT(old_level); \ |
||
451 | code; \ |
||
452 | SYS_ARCH_UNPROTECT(old_level); \ |
||
453 | } while(0) |
||
454 | #endif /* SYS_ARCH_LOCKED */ |
||
455 | |||
456 | |||
457 | #ifdef __cplusplus |
||
458 | } |
||
459 | #endif |
||
460 | |||
461 | #endif /* LWIP_HDR_SYS_H */ |