Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/include/lwip/sys.h need to be fixed.

0001 /*
0002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0003  * All rights reserved. 
0004  * 
0005  * Redistribution and use in source and binary forms, with or without modification, 
0006  * are permitted provided that the following conditions are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright notice,
0009  *    this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright notice,
0011  *    this list of conditions and the following disclaimer in the documentation
0012  *    and/or other materials provided with the distribution.
0013  * 3. The name of the author may not be used to endorse or promote products
0014  *    derived from this software without specific prior written permission. 
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
0017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
0018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
0019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
0020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
0021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
0024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
0025  * OF SUCH DAMAGE.
0026  *
0027  * This file is part of the lwIP TCP/IP stack.
0028  * 
0029  * Author: Adam Dunkels <adam@sics.se>
0030  *
0031  */
0032 #ifndef __LWIP_SYS_H__
0033 #define __LWIP_SYS_H__
0034 
0035 #include "lwip/opt.h"
0036 
0037 #ifdef __cplusplus
0038 extern "C" {
0039 #endif
0040 
0041 #if NO_SYS
0042 
0043 /* For a totally minimal and standalone system, we provide null
0044    definitions of the sys_ functions. */
0045 typedef u8_t sys_sem_t;
0046 typedef u8_t sys_mbox_t;
0047 struct sys_timeo {u8_t dummy;};
0048 
0049 #define sys_init()
0050 #define sys_timeout(m,h,a)
0051 #define sys_untimeout(m,a)
0052 #define sys_sem_new(c) c
0053 #define sys_sem_signal(s)
0054 #define sys_sem_wait(s)
0055 #define sys_sem_wait_timeout(s,t)
0056 #define sys_arch_sem_wait(s,t)
0057 #define sys_sem_free(s)
0058 #define sys_mbox_new(s) 0
0059 #define sys_mbox_fetch(m,d)
0060 #define sys_mbox_tryfetch(m,d)
0061 #define sys_mbox_post(m,d)
0062 #define sys_mbox_trypost(m,d)
0063 #define sys_mbox_free(m)
0064 
0065 #define sys_thread_new(n,t,a,s,p)
0066 
0067 #else /* NO_SYS */
0068 
0069 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
0070 #define SYS_ARCH_TIMEOUT 0xffffffffUL
0071 
0072 /* sys_mbox_tryfetch returns SYS_MBOX_EMPTY if appropriate.
0073  * For now we use the same magic value, but we allow this to change in future.
0074  */
0075 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT 
0076 
0077 #include "lwip/err.h"
0078 #include "arch/sys_arch.h"
0079 
0080 typedef void (* sys_timeout_handler)(void *arg);
0081 
0082 struct sys_timeo {
0083   struct sys_timeo *next;
0084   u32_t time;
0085   sys_timeout_handler h;
0086   void *arg;
0087 };
0088 
0089 struct sys_timeouts {
0090   struct sys_timeo *next;
0091 };
0092 
0093 /* sys_init() must be called before anthing else. */
0094 void sys_init(void);
0095 
0096 /*
0097  * sys_timeout():
0098  *
0099  * Schedule a timeout a specified amount of milliseconds in the
0100  * future. When the timeout occurs, the specified timeout handler will
0101  * be called. The handler will be passed the "arg" argument when
0102  * called.
0103  *
0104  */
0105 void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg);
0106 void sys_untimeout(sys_timeout_handler h, void *arg);
0107 struct sys_timeouts *sys_arch_timeouts(void);
0108 
0109 /* Semaphore functions. */
0110 sys_sem_t sys_sem_new(u8_t count);
0111 void sys_sem_signal(sys_sem_t sem);
0112 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout);
0113 void sys_sem_free(sys_sem_t sem);
0114 void sys_sem_wait(sys_sem_t sem);
0115 int sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout);
0116 
0117 /* Time functions. */
0118 #ifndef sys_msleep
0119 void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
0120 #endif
0121 #ifndef sys_jiffies
0122 u32_t sys_jiffies(void); /* since power up. */
0123 #endif
0124 
0125 /* Mailbox functions. */
0126 sys_mbox_t sys_mbox_new(int size);
0127 void sys_mbox_post(sys_mbox_t mbox, void *msg);
0128 err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg);
0129 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout);
0130 #ifndef sys_arch_mbox_tryfetch /* Allow port to override with a macro */
0131 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg);
0132 #endif
0133 /* For now, we map straight to sys_arch implementation. */
0134 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
0135 void sys_mbox_free(sys_mbox_t mbox);
0136 void sys_mbox_fetch(sys_mbox_t mbox, void **msg);
0137 
0138 /* Thread functions. */
0139 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio);
0140 
0141 #endif /* NO_SYS */
0142 
0143 /** Returns the current time in milliseconds. */
0144 u32_t sys_now(void);
0145 
0146 /* Critical Region Protection */
0147 /* These functions must be implemented in the sys_arch.c file.
0148    In some implementations they can provide a more light-weight protection
0149    mechanism than using semaphores. Otherwise semaphores can be used for
0150    implementation */
0151 #ifndef SYS_ARCH_PROTECT
0152 /** SYS_LIGHTWEIGHT_PROT
0153  * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
0154  * for certain critical regions during buffer allocation, deallocation and memory
0155  * allocation and deallocation.
0156  */
0157 #if SYS_LIGHTWEIGHT_PROT
0158 
0159 /** SYS_ARCH_DECL_PROTECT
0160  * declare a protection variable. This macro will default to defining a variable of
0161  * type sys_prot_t. If a particular port needs a different implementation, then
0162  * this macro may be defined in sys_arch.h.
0163  */
0164 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
0165 /** SYS_ARCH_PROTECT
0166  * Perform a "fast" protect. This could be implemented by
0167  * disabling interrupts for an embedded system or by using a semaphore or
0168  * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
0169  * already protected. The old protection level is returned in the variable
0170  * "lev". This macro will default to calling the sys_arch_protect() function
0171  * which should be implemented in sys_arch.c. If a particular port needs a
0172  * different implementation, then this macro may be defined in sys_arch.h
0173  */
0174 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
0175 /** SYS_ARCH_UNPROTECT
0176  * Perform a "fast" set of the protection level to "lev". This could be
0177  * implemented by setting the interrupt level to "lev" within the MACRO or by
0178  * using a semaphore or mutex.  This macro will default to calling the
0179  * sys_arch_unprotect() function which should be implemented in
0180  * sys_arch.c. If a particular port needs a different implementation, then
0181  * this macro may be defined in sys_arch.h
0182  */
0183 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
0184 sys_prot_t sys_arch_protect(void);
0185 void sys_arch_unprotect(sys_prot_t pval);
0186 
0187 #else
0188 
0189 #define SYS_ARCH_DECL_PROTECT(lev)
0190 #define SYS_ARCH_PROTECT(lev)
0191 #define SYS_ARCH_UNPROTECT(lev)
0192 
0193 #endif /* SYS_LIGHTWEIGHT_PROT */
0194 
0195 #endif /* SYS_ARCH_PROTECT */
0196 
0197 /*
0198  * Macros to set/get and increase/decrease variables in a thread-safe way.
0199  * Use these for accessing variable that are used from more than one thread.
0200  */
0201 
0202 #ifndef SYS_ARCH_INC
0203 #define SYS_ARCH_INC(var, val) do { \
0204                                 SYS_ARCH_DECL_PROTECT(old_level); \
0205                                 SYS_ARCH_PROTECT(old_level); \
0206                                 var += val; \
0207                                 SYS_ARCH_UNPROTECT(old_level); \
0208                               } while(0)
0209 #endif /* SYS_ARCH_INC */
0210 
0211 #ifndef SYS_ARCH_DEC
0212 #define SYS_ARCH_DEC(var, val) do { \
0213                                 SYS_ARCH_DECL_PROTECT(old_level); \
0214                                 SYS_ARCH_PROTECT(old_level); \
0215                                 var -= val; \
0216                                 SYS_ARCH_UNPROTECT(old_level); \
0217                               } while(0)
0218 #endif /* SYS_ARCH_DEC */
0219 
0220 #ifndef SYS_ARCH_GET
0221 #define SYS_ARCH_GET(var, ret) do { \
0222                                 SYS_ARCH_DECL_PROTECT(old_level); \
0223                                 SYS_ARCH_PROTECT(old_level); \
0224                                 ret = var; \
0225                                 SYS_ARCH_UNPROTECT(old_level); \
0226                               } while(0)
0227 #endif /* SYS_ARCH_GET */
0228 
0229 #ifndef SYS_ARCH_SET
0230 #define SYS_ARCH_SET(var, val) do { \
0231                                 SYS_ARCH_DECL_PROTECT(old_level); \
0232                                 SYS_ARCH_PROTECT(old_level); \
0233                                 var = val; \
0234                                 SYS_ARCH_UNPROTECT(old_level); \
0235                               } while(0)
0236 #endif /* SYS_ARCH_SET */
0237 
0238 
0239 #ifdef __cplusplus
0240 }
0241 #endif
0242 
0243 #endif /* __LWIP_SYS_H__ */