sys.h
Upload User: tym7xi
Upload Date: 2018-01-17
Package Size: 6975k
Code Size: 6k
Development Platform:

C/C++

  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. #include "arch/cc.h"
  35. #include "lwip/opt.h"
  36. #if NO_SYS
  37. /* For a totally minimal and standalone system, we provide null
  38.    definitions of the sys_ functions. */
  39. typedef u8_t sys_sem_t;
  40. typedef u8_t sys_mbox_t;
  41. struct sys_timeo {u8_t dummy;};
  42. #define sys_init()
  43. #define sys_timeout(m,h,a)
  44. #define sys_untimeout(m,a)
  45. #define sys_sem_new(c) c
  46. #define sys_sem_signal(s)
  47. #define sys_sem_wait(s)
  48. #define sys_sem_free(s)
  49. #define sys_mbox_new() 0
  50. #define sys_mbox_fetch(m,d)
  51. #define sys_mbox_post(m,d)
  52. #define sys_mbox_free(m)
  53. #define sys_thread_new(t,a,p)
  54. #else /* NO_SYS */
  55. #include "arch/sys_arch.h"
  56. /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
  57. #define SYS_ARCH_TIMEOUT 0xffffffff
  58. typedef void (* sys_timeout_handler)(void *arg);
  59. struct sys_timeo {
  60.   struct sys_timeo *next;
  61.   u32_t time;
  62.   sys_timeout_handler h;
  63.   void *arg;
  64. };
  65. struct sys_timeouts {
  66.   struct sys_timeo *next;
  67. };
  68. /* sys_init() must be called before anthing else. */
  69. void sys_init(void);
  70. /*
  71.  * sys_timeout():
  72.  *
  73.  * Schedule a timeout a specified amount of milliseconds in the
  74.  * future. When the timeout occurs, the specified timeout handler will
  75.  * be called. The handler will be passed the "arg" argument when
  76.  * called.
  77.  *
  78.  */
  79. void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg);
  80. void sys_untimeout(sys_timeout_handler h, void *arg);
  81. struct sys_timeouts *sys_arch_timeouts(void);
  82. /* Semaphore functions. */
  83. sys_sem_t sys_sem_new(u8_t count);
  84. void sys_sem_signal(sys_sem_t sem);
  85. u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout);
  86. void sys_sem_free(sys_sem_t sem);
  87. void sys_sem_wait(sys_sem_t sem);
  88. int sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout);
  89. /* Time functions. */
  90. #ifndef sys_msleep
  91. void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
  92. #endif
  93. #ifndef sys_jiffies
  94. u32_t sys_jiffies(void); /* since power up. */
  95. #endif
  96. /* Mailbox functions. */
  97. sys_mbox_t sys_mbox_new(void);
  98. void sys_mbox_post(sys_mbox_t mbox, void *msg);
  99. u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout);
  100. void sys_mbox_free(sys_mbox_t mbox);
  101. void sys_mbox_fetch(sys_mbox_t mbox, void **msg);
  102. /* Thread functions. */
  103. sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio);
  104. /* The following functions are used only in Unix code, and
  105.    can be omitted when porting the stack. */
  106. /* Returns the current time in microseconds. */
  107. unsigned long sys_now(void);
  108. #endif /* NO_SYS */
  109. /* Critical Region Protection */
  110. /* These functions must be implemented in the sys_arch.c file.
  111.    In some implementations they can provide a more light-weight protection
  112.    mechanism than using semaphores. Otherwise semaphores can be used for
  113.    implementation */
  114. #ifndef SYS_ARCH_PROTECT
  115. /** SYS_LIGHTWEIGHT_PROT
  116.  * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
  117.  * for certain critical regions during buffer allocation, deallocation and memory
  118.  * allocation and deallocation.
  119.  */
  120. #if SYS_LIGHTWEIGHT_PROT
  121. /** SYS_ARCH_DECL_PROTECT
  122.  * declare a protection variable. This macro will default to defining a variable of
  123.  * type sys_prot_t. If a particular port needs a different implementation, then
  124.  * this macro may be defined in sys_arch.h.
  125.  */
  126. #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
  127. /** SYS_ARCH_PROTECT
  128.  * Perform a "fast" protect. This could be implemented by
  129.  * disabling interrupts for an embedded system or by using a semaphore or
  130.  * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
  131.  * already protected. The old protection level is returned in the variable
  132.  * "lev". This macro will default to calling the sys_arch_protect() function
  133.  * which should be implemented in sys_arch.c. If a particular port needs a
  134.  * different implementation, then this macro may be defined in sys_arch.h
  135.  */
  136. #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
  137. /** SYS_ARCH_UNPROTECT
  138.  * Perform a "fast" set of the protection level to "lev". This could be
  139.  * implemented by setting the interrupt level to "lev" within the MACRO or by
  140.  * using a semaphore or mutex.  This macro will default to calling the
  141.  * sys_arch_unprotect() function which should be implemented in
  142.  * sys_arch.c. If a particular port needs a different implementation, then
  143.  * this macro may be defined in sys_arch.h
  144.  */
  145. #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
  146. sys_prot_t sys_arch_protect(void);
  147. void sys_arch_unprotect(sys_prot_t pval);
  148. #else
  149. #define SYS_ARCH_DECL_PROTECT(lev)
  150. #define SYS_ARCH_PROTECT(lev)
  151. #define SYS_ARCH_UNPROTECT(lev)
  152. #endif /* SYS_LIGHTWEIGHT_PROT */
  153. #endif /* SYS_ARCH_PROTECT */
  154. #endif /* __LWIP_SYS_H__ */