• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mutex_t;
47 typedef u8_t sys_mbox_t;
48 
49 #define sys_sem_new(s, c) ERR_OK
50 #define sys_sem_signal(s)
51 #define sys_sem_wait(s)
52 #define sys_arch_sem_wait(s,t)
53 #define sys_sem_free(s)
54 #define sys_mutex_new(mu) ERR_OK
55 #define sys_mutex_lock(mu)
56 #define sys_mutex_unlock(mu)
57 #define sys_mutex_free(mu)
58 #define sys_mbox_new(m, s) ERR_OK
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 #define sys_msleep(t)
68 
69 #else /* NO_SYS */
70 
71 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
72 #define SYS_ARCH_TIMEOUT 0xffffffffUL
73 
74 /** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.
75  * For now we use the same magic value, but we allow this to change in future.
76  */
77 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
78 
79 #include "lwip/err.h"
80 #include "arch/sys_arch.h"
81 
82 /** Function prototype for thread functions */
83 typedef void (*lwip_thread_fn)(void *arg);
84 
85 /* Function prototypes for functions to be implemented by platform ports
86    (in sys_arch.c) */
87 
88 /* Mutex functions: */
89 
90 /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores
91     should be used instead */
92 #if LWIP_COMPAT_MUTEX
93 /* for old ports that don't have mutexes: define them to binary semaphores */
94 #define sys_mutex_t                   sys_sem_t
95 #define sys_mutex_new(mutex)          sys_sem_new(mutex, 1)
96 #define sys_mutex_lock(mutex)         sys_sem_wait(mutex)
97 #define sys_mutex_unlock(mutex)       sys_sem_signal(mutex)
98 #define sys_mutex_free(mutex)         sys_sem_free(mutex)
99 #define sys_mutex_valid(mutex)        sys_sem_valid(mutex)
100 #define sys_mutex_set_invalid(mutex)  sys_sem_set_invalid(mutex)
101 
102 #else /* LWIP_COMPAT_MUTEX */
103 
104 /** Create a new mutex
105  * @param mutex pointer to the mutex to create
106  * @return a new mutex */
107 err_t sys_mutex_new(sys_mutex_t *mutex);
108 /** Lock a mutex
109  * @param mutex the mutex to lock */
110 void sys_mutex_lock(sys_mutex_t *mutex);
111 /** Unlock a mutex
112  * @param mutex the mutex to unlock */
113 void sys_mutex_unlock(sys_mutex_t *mutex);
114 /** Delete a semaphore
115  * @param mutex the mutex to delete */
116 void sys_mutex_free(sys_mutex_t *mutex);
117 #ifndef sys_mutex_valid
118 /** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */
119 int sys_mutex_valid(sys_mutex_t *mutex);
120 #endif
121 #ifndef sys_mutex_set_invalid
122 /** Set a mutex invalid so that sys_mutex_valid returns 0 */
123 void sys_mutex_set_invalid(sys_mutex_t *mutex);
124 #endif
125 #endif /* LWIP_COMPAT_MUTEX */
126 
127 /* Semaphore functions: */
128 
129 /** Create a new semaphore
130  * @param sem pointer to the semaphore to create
131  * @param count initial count of the semaphore
132  * @return ERR_OK if successful, another err_t otherwise */
133 err_t sys_sem_new(sys_sem_t *sem, u8_t count);
134 /** Signals a semaphore
135  * @param sem the semaphore to signal */
136 void sys_sem_signal(sys_sem_t *sem);
137 /** Wait for a semaphore for the specified timeout
138  * @param sem the semaphore to wait for
139  * @param timeout timeout in milliseconds to wait (0 = wait forever)
140  * @return time (in milliseconds) waited for the semaphore
141  *         or SYS_ARCH_TIMEOUT on timeout */
142 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
143 /** Delete a semaphore
144  * @param sem semaphore to delete */
145 void sys_sem_free(sys_sem_t *sem);
146 /** Wait for a semaphore - forever/no timeout */
147 #define sys_sem_wait(sem)                  sys_arch_sem_wait(sem, 0)
148 #ifndef sys_sem_valid
149 /** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */
150 int sys_sem_valid(sys_sem_t *sem);
151 #endif
152 #ifndef sys_sem_set_invalid
153 /** Set a semaphore invalid so that sys_sem_valid returns 0 */
154 void sys_sem_set_invalid(sys_sem_t *sem);
155 #endif
156 
157 /* Time functions. */
158 #ifndef sys_msleep
159 void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
160 #endif
161 
162 /* Mailbox functions. */
163 
164 /** Create a new mbox of specified size
165  * @param mbox pointer to the mbox to create
166  * @param size (miminum) number of messages in this mbox
167  * @return ERR_OK if successful, another err_t otherwise */
168 err_t sys_mbox_new(sys_mbox_t *mbox, int size);
169 /** Post a message to an mbox - may not fail
170  * -> blocks if full, only used from tasks not from ISR
171  * @param mbox mbox to posts the message
172  * @param msg message to post (ATTENTION: can be NULL) */
173 void sys_mbox_post(sys_mbox_t *mbox, void *msg);
174 /** Try to post a message to an mbox - may fail if full or ISR
175  * @param mbox mbox to posts the message
176  * @param msg message to post (ATTENTION: can be NULL) */
177 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
178 /** Wait for a new message to arrive in the mbox
179  * @param mbox mbox to get a message from
180  * @param msg pointer where the message is stored
181  * @param timeout maximum time (in milliseconds) to wait for a message
182  * @return time (in milliseconds) waited for a message, may be 0 if not waited
183            or SYS_ARCH_TIMEOUT on timeout
184  *         The returned time has to be accurate to prevent timer jitter! */
185 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
186 /* Allow port to override with a macro, e.g. special timout for sys_arch_mbox_fetch() */
187 #ifndef sys_arch_mbox_tryfetch
188 /** Wait for a new message to arrive in the mbox
189  * @param mbox mbox to get a message from
190  * @param msg pointer where the message is stored
191  * @param timeout maximum time (in milliseconds) to wait for a message
192  * @return 0 (milliseconds) if a message has been received
193  *         or SYS_MBOX_EMPTY if the mailbox is empty */
194 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
195 #endif
196 /** For now, we map straight to sys_arch implementation. */
197 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
198 /** Delete an mbox
199  * @param mbox mbox to delete */
200 void sys_mbox_free(sys_mbox_t *mbox);
201 #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
202 #ifndef sys_mbox_valid
203 /** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */
204 int sys_mbox_valid(sys_mbox_t *mbox);
205 #endif
206 #ifndef sys_mbox_set_invalid
207 /** Set an mbox invalid so that sys_mbox_valid returns 0 */
208 void sys_mbox_set_invalid(sys_mbox_t *mbox);
209 #endif
210 
211 /** The only thread function:
212  * Creates a new thread
213  * @param name human-readable name for the thread (used for debugging purposes)
214  * @param thread thread-function
215  * @param arg parameter passed to 'thread'
216  * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
217  * @param prio priority of the new thread (may be ignored by ports) */
218 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);
219 
220 #endif /* NO_SYS */
221 
222 /* sys_init() must be called before anthing else. */
223 void sys_init(void);
224 
225 #ifndef sys_jiffies
226 /** Ticks/jiffies since power up. */
227 u32_t sys_jiffies(void);
228 #endif
229 
230 /** Returns the current time in milliseconds,
231  * may be the same as sys_jiffies or at least based on it. */
232 u32_t sys_now(void);
233 
234 /* Critical Region Protection */
235 /* These functions must be implemented in the sys_arch.c file.
236    In some implementations they can provide a more light-weight protection
237    mechanism than using semaphores. Otherwise semaphores can be used for
238    implementation */
239 #ifndef SYS_ARCH_PROTECT
240 /** SYS_LIGHTWEIGHT_PROT
241  * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
242  * for certain critical regions during buffer allocation, deallocation and memory
243  * allocation and deallocation.
244  */
245 #if SYS_LIGHTWEIGHT_PROT
246 
247 /** SYS_ARCH_DECL_PROTECT
248  * declare a protection variable. This macro will default to defining a variable of
249  * type sys_prot_t. If a particular port needs a different implementation, then
250  * this macro may be defined in sys_arch.h.
251  */
252 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
253 /** SYS_ARCH_PROTECT
254  * Perform a "fast" protect. This could be implemented by
255  * disabling interrupts for an embedded system or by using a semaphore or
256  * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
257  * already protected. The old protection level is returned in the variable
258  * "lev". This macro will default to calling the sys_arch_protect() function
259  * which should be implemented in sys_arch.c. If a particular port needs a
260  * different implementation, then this macro may be defined in sys_arch.h
261  */
262 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
263 /** SYS_ARCH_UNPROTECT
264  * Perform a "fast" set of the protection level to "lev". This could be
265  * implemented by setting the interrupt level to "lev" within the MACRO or by
266  * using a semaphore or mutex.  This macro will default to calling the
267  * sys_arch_unprotect() function which should be implemented in
268  * sys_arch.c. If a particular port needs a different implementation, then
269  * this macro may be defined in sys_arch.h
270  */
271 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
272 sys_prot_t sys_arch_protect(void);
273 void sys_arch_unprotect(sys_prot_t pval);
274 
275 #else
276 
277 #define SYS_ARCH_DECL_PROTECT(lev)
278 #define SYS_ARCH_PROTECT(lev)
279 #define SYS_ARCH_UNPROTECT(lev)
280 
281 #endif /* SYS_LIGHTWEIGHT_PROT */
282 
283 #endif /* SYS_ARCH_PROTECT */
284 
285 /*
286  * Macros to set/get and increase/decrease variables in a thread-safe way.
287  * Use these for accessing variable that are used from more than one thread.
288  */
289 
290 #ifndef SYS_ARCH_INC
291 #define SYS_ARCH_INC(var, val) do { \
292                                 SYS_ARCH_DECL_PROTECT(old_level); \
293                                 SYS_ARCH_PROTECT(old_level); \
294                                 var += val; \
295                                 SYS_ARCH_UNPROTECT(old_level); \
296                               } while(0)
297 #endif /* SYS_ARCH_INC */
298 
299 #ifndef SYS_ARCH_DEC
300 #define SYS_ARCH_DEC(var, val) do { \
301                                 SYS_ARCH_DECL_PROTECT(old_level); \
302                                 SYS_ARCH_PROTECT(old_level); \
303                                 var -= val; \
304                                 SYS_ARCH_UNPROTECT(old_level); \
305                               } while(0)
306 #endif /* SYS_ARCH_DEC */
307 
308 #ifndef SYS_ARCH_GET
309 #define SYS_ARCH_GET(var, ret) do { \
310                                 SYS_ARCH_DECL_PROTECT(old_level); \
311                                 SYS_ARCH_PROTECT(old_level); \
312                                 ret = var; \
313                                 SYS_ARCH_UNPROTECT(old_level); \
314                               } while(0)
315 #endif /* SYS_ARCH_GET */
316 
317 #ifndef SYS_ARCH_SET
318 #define SYS_ARCH_SET(var, val) do { \
319                                 SYS_ARCH_DECL_PROTECT(old_level); \
320                                 SYS_ARCH_PROTECT(old_level); \
321                                 var = val; \
322                                 SYS_ARCH_UNPROTECT(old_level); \
323                               } while(0)
324 #endif /* SYS_ARCH_SET */
325 
326 
327 #ifdef __cplusplus
328 }
329 #endif
330 
331 #endif /* __LWIP_SYS_H__ */
332