A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sys.h Source File

sys.h

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  * 
00005  * Redistribution and use in source and binary forms, with or without modification, 
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission. 
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  * 
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 #ifndef __LWIP_SYS_H__
00033 #define __LWIP_SYS_H__
00034 
00035 #include "lwip/opt.h"
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 #if NO_SYS
00042 
00043 /* For a totally minimal and standalone system, we provide null
00044    definitions of the sys_ functions. */
00045 typedef u8_t sys_sem_t;
00046 typedef u8_t sys_mbox_t;
00047 typedef u8_t sys_prot_t;
00048 struct sys_timeo {u8_t dummy;};
00049 
00050 #define sys_init()
00051 #define sys_timeout(m,h,a)
00052 #define sys_untimeout(m,a)
00053 #define sys_sem_new(c) c
00054 #define sys_sem_signal(s)
00055 #define sys_sem_wait(s)
00056 #define sys_sem_wait_timeout(s,t)
00057 #define sys_arch_sem_wait(s,t)
00058 #define sys_sem_free(s)
00059 #define sys_mbox_new(s) 0
00060 #define sys_mbox_fetch(m,d)
00061 #define sys_mbox_tryfetch(m,d)
00062 #define sys_mbox_post(m,d)
00063 #define sys_mbox_trypost(m,d)
00064 #define sys_mbox_free(m)
00065 
00066 #define sys_thread_new(n,t,a,s,p)
00067 
00068 #else /* NO_SYS */
00069 
00070 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
00071 #define SYS_ARCH_TIMEOUT 0xffffffffUL
00072 
00073 /* sys_mbox_tryfetch returns SYS_MBOX_EMPTY if appropriate.
00074  * For now we use the same magic value, but we allow this to change in future.
00075  */
00076 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT 
00077 
00078 #include "lwip/err.h"
00079 #include "arch/sys_arch.h"
00080 
00081 typedef void (* sys_timeout_handler)(void *arg);
00082 
00083 struct sys_timeo {
00084   struct sys_timeo *next;
00085   u32_t time;
00086   sys_timeout_handler h;
00087   void *arg;
00088 };
00089 
00090 struct sys_timeouts {
00091   struct sys_timeo *next;
00092 };
00093 
00094 /* sys_init() must be called before anthing else. */
00095 void sys_init(void);
00096 
00097 /*
00098  * sys_timeout():
00099  *
00100  * Schedule a timeout a specified amount of milliseconds in the
00101  * future. When the timeout occurs, the specified timeout handler will
00102  * be called. The handler will be passed the "arg" argument when
00103  * called.
00104  *
00105  */
00106 void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg);
00107 void sys_untimeout(sys_timeout_handler h, void *arg);
00108 struct sys_timeouts *sys_arch_timeouts(void);
00109 
00110 /* Semaphore functions. */
00111 sys_sem_t sys_sem_new(u8_t count);
00112 void sys_sem_signal(sys_sem_t sem);
00113 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout);
00114 void sys_sem_free(sys_sem_t sem);
00115 void sys_sem_wait(sys_sem_t sem);
00116 int sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout);
00117 
00118 /* Time functions. */
00119 #ifndef sys_msleep
00120 void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
00121 #endif
00122 #ifndef sys_jiffies
00123 u32_t sys_jiffies(void); /* since power up. */
00124 #endif
00125 
00126 /* Mailbox functions. */
00127 sys_mbox_t sys_mbox_new(int size);
00128 void sys_mbox_post(sys_mbox_t mbox, void *msg);
00129 err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg);
00130 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout);
00131 #ifndef sys_arch_mbox_tryfetch /* Allow port to override with a macro */
00132 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg);
00133 #endif
00134 /* For now, we map straight to sys_arch implementation. */
00135 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
00136 void sys_mbox_free(sys_mbox_t mbox);
00137 void sys_mbox_fetch(sys_mbox_t mbox, void **msg);
00138 
00139 /* Thread functions. */
00140 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio);
00141 
00142 /* The following functions are used only in Unix code, and
00143    can be omitted when porting the stack. */
00144 /* Returns the current time in microseconds. */
00145 unsigned long sys_now(void);
00146 
00147 #endif /* NO_SYS */
00148 
00149 /* Critical Region Protection */
00150 /* These functions must be implemented in the sys_arch.c file.
00151    In some implementations they can provide a more light-weight protection
00152    mechanism than using semaphores. Otherwise semaphores can be used for
00153    implementation */
00154 #ifndef SYS_ARCH_PROTECT
00155 /** SYS_LIGHTWEIGHT_PROT
00156  * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
00157  * for certain critical regions during buffer allocation, deallocation and memory
00158  * allocation and deallocation.
00159  */
00160 #if SYS_LIGHTWEIGHT_PROT
00161 
00162 /** SYS_ARCH_DECL_PROTECT
00163  * declare a protection variable. This macro will default to defining a variable of
00164  * type sys_prot_t. If a particular port needs a different implementation, then
00165  * this macro may be defined in sys_arch.h.
00166  */
00167 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
00168 /** SYS_ARCH_PROTECT
00169  * Perform a "fast" protect. This could be implemented by
00170  * disabling interrupts for an embedded system or by using a semaphore or
00171  * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
00172  * already protected. The old protection level is returned in the variable
00173  * "lev". This macro will default to calling the sys_arch_protect() function
00174  * which should be implemented in sys_arch.c. If a particular port needs a
00175  * different implementation, then this macro may be defined in sys_arch.h
00176  */
00177 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
00178 /** SYS_ARCH_UNPROTECT
00179  * Perform a "fast" set of the protection level to "lev". This could be
00180  * implemented by setting the interrupt level to "lev" within the MACRO or by
00181  * using a semaphore or mutex.  This macro will default to calling the
00182  * sys_arch_unprotect() function which should be implemented in
00183  * sys_arch.c. If a particular port needs a different implementation, then
00184  * this macro may be defined in sys_arch.h
00185  */
00186 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
00187 sys_prot_t sys_arch_protect(void);
00188 void sys_arch_unprotect(sys_prot_t pval);
00189 
00190 #else
00191 
00192 #define SYS_ARCH_DECL_PROTECT(lev)
00193 #define SYS_ARCH_PROTECT(lev)
00194 #define SYS_ARCH_UNPROTECT(lev)
00195 
00196 #endif /* SYS_LIGHTWEIGHT_PROT */
00197 
00198 #endif /* SYS_ARCH_PROTECT */
00199 
00200 /*
00201  * Macros to set/get and increase/decrease variables in a thread-safe way.
00202  * Use these for accessing variable that are used from more than one thread.
00203  */
00204 
00205 #ifndef SYS_ARCH_INC
00206 #define SYS_ARCH_INC(var, val) do { \
00207                                 SYS_ARCH_DECL_PROTECT(old_level); \
00208                                 SYS_ARCH_PROTECT(old_level); \
00209                                 var += val; \
00210                                 SYS_ARCH_UNPROTECT(old_level); \
00211                               } while(0)
00212 #endif /* SYS_ARCH_INC */
00213 
00214 #ifndef SYS_ARCH_DEC
00215 #define SYS_ARCH_DEC(var, val) do { \
00216                                 SYS_ARCH_DECL_PROTECT(old_level); \
00217                                 SYS_ARCH_PROTECT(old_level); \
00218                                 var -= val; \
00219                                 SYS_ARCH_UNPROTECT(old_level); \
00220                               } while(0)
00221 #endif /* SYS_ARCH_DEC */
00222 
00223 #ifndef SYS_ARCH_GET
00224 #define SYS_ARCH_GET(var, ret) do { \
00225                                 SYS_ARCH_DECL_PROTECT(old_level); \
00226                                 SYS_ARCH_PROTECT(old_level); \
00227                                 ret = var; \
00228                                 SYS_ARCH_UNPROTECT(old_level); \
00229                               } while(0)
00230 #endif /* SYS_ARCH_GET */
00231 
00232 #ifndef SYS_ARCH_SET
00233 #define SYS_ARCH_SET(var, val) do { \
00234                                 SYS_ARCH_DECL_PROTECT(old_level); \
00235                                 SYS_ARCH_PROTECT(old_level); \
00236                                 var = val; \
00237                                 SYS_ARCH_UNPROTECT(old_level); \
00238                               } while(0)
00239 #endif /* SYS_ARCH_SET */
00240 
00241 
00242 #ifdef __cplusplus
00243 }
00244 #endif
00245 
00246 #endif /* __LWIP_SYS_H__ */