/* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels * */ #ifndef __LWIP_SYS_H__ #define __LWIP_SYS_H__ #include "opt.h" #ifdef __cplusplus extern "C" { #endif #if NO_SYS /* For a totally minimal and standalone system, we provide null definitions of the sys_ functions. */ typedef u8_t sys_sem_t; typedef u8_t sys_mutex_t; typedef u8_t sys_mbox_t; #define sys_sem_new(s, c) ERR_OK #define sys_sem_signal(s) #define sys_sem_wait(s) #define sys_arch_sem_wait(s,t) #define sys_sem_free(s) #define sys_sem_valid(s) 0 #define sys_sem_set_invalid(s) #define sys_mutex_new(mu) ERR_OK #define sys_mutex_lock(mu) #define sys_mutex_unlock(mu) #define sys_mutex_free(mu) #define sys_mutex_valid(mu) 0 #define sys_mutex_set_invalid(mu) #define sys_mbox_new(m, s) ERR_OK #define sys_mbox_fetch(m,d) #define sys_mbox_tryfetch(m,d) #define sys_mbox_post(m,d) #define sys_mbox_trypost(m,d) #define sys_mbox_free(m) #define sys_mbox_valid(m) #define sys_mbox_set_invalid(m) #define sys_thread_new(n,t,a,s,p) #define sys_msleep(t) #else /* NO_SYS */ #include "err.h" #include "sys_arch.h" /* Function prototypes for functions to be implemented by platform ports (in sys_arch.c) */ /* Mutex functions: */ /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores should be used instead */ #if LWIP_COMPAT_MUTEX /* for old ports that don't have mutexes: define them to binary semaphores */ #define sys_mutex_t sys_sem_t #define sys_mutex_new(mutex) sys_sem_new(mutex, 1) #define sys_mutex_lock(mutex) sys_sem_wait(mutex) #define sys_mutex_unlock(mutex) sys_sem_signal(mutex) #define sys_mutex_free(mutex) sys_sem_free(mutex) #define sys_mutex_valid(mutex) sys_sem_valid(mutex) #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex) #else /* LWIP_COMPAT_MUTEX */ /** Create a new mutex * @param mutex pointer to the mutex to create * @return a new mutex */ err_t sys_mutex_new(sys_mutex_t *mutex); /** Lock a mutex * @param mutex the mutex to lock */ void sys_mutex_lock(sys_mutex_t *mutex); /** Unlock a mutex * @param mutex the mutex to unlock */ void sys_mutex_unlock(sys_mutex_t *mutex); /** Delete a semaphore * @param mutex the mutex to delete */ void sys_mutex_free(sys_mutex_t *mutex); #ifndef sys_mutex_valid /** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */ int sys_mutex_valid(sys_mutex_t *mutex); #endif #ifndef sys_mutex_set_invalid /** Set a mutex invalid so that sys_mutex_valid returns 0 */ void sys_mutex_set_invalid(sys_mutex_t *mutex); #endif #endif /* LWIP_COMPAT_MUTEX */ /* Semaphore functions: */ /** Wait for a semaphore - forever/no timeout */ #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0) /* Mailbox functions. */ /** For now, we map straight to sys_arch implementation. */ #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg) #define sys_mbox_tryfetch_nosuspend(mbox, msg) sys_arch_mbox_tryfetch_nosuspend(mbox, msg) /** Delete an mbox * @param mbox mbox to delete */ #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0) #endif /* NO_SYS */ /* Critical Region Protection */ /* These functions must be implemented in the sys_arch.c file. In some implementations they can provide a more light-weight protection mechanism than using semaphores. Otherwise semaphores can be used for implementation */ #ifndef SYS_ARCH_PROTECT /** SYS_LIGHTWEIGHT_PROT * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection * for certain critical regions during buffer allocation, deallocation and memory * allocation and deallocation. */ #if SYS_LIGHTWEIGHT_PROT /** SYS_ARCH_DECL_PROTECT * declare a protection variable. This macro will default to defining a variable of * type sys_prot_t. If a particular port needs a different implementation, then * this macro may be defined in sys_arch.h. */ #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev = 0 /** SYS_ARCH_PROTECT * Perform a "fast" protect. This could be implemented by * disabling interrupts for an embedded system or by using a semaphore or * mutex. The implementation should allow calling SYS_ARCH_PROTECT when * already protected. The old protection level is returned in the variable * "lev". This macro will default to calling the sys_arch_protect() function * which should be implemented in sys_arch.c. If a particular port needs a * different implementation, then this macro may be defined in sys_arch.h */ #define SYS_ARCH_PROTECT(lev) sys_arch_protect(&(lev)) /** SYS_ARCH_UNPROTECT * Perform a "fast" set of the protection level to "lev". This could be * implemented by setting the interrupt level to "lev" within the MACRO or by * using a semaphore or mutex. This macro will default to calling the * sys_arch_unprotect() function which should be implemented in * sys_arch.c. If a particular port needs a different implementation, then * this macro may be defined in sys_arch.h */ #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev) #else #define SYS_ARCH_DECL_PROTECT(lev) #define SYS_ARCH_PROTECT(lev) #define SYS_ARCH_UNPROTECT(lev) #endif /* SYS_LIGHTWEIGHT_PROT */ #endif /* SYS_ARCH_PROTECT */ /* * Macros to set/get and increase/decrease variables in a thread-safe way. * Use these for accessing variable that are used from more than one thread. */ #ifndef SYS_ARCH_INC #define SYS_ARCH_INC(var, val) do { \ SYS_ARCH_DECL_PROTECT(old_level); \ SYS_ARCH_PROTECT(old_level); \ var += val; \ SYS_ARCH_UNPROTECT(old_level); \ } while(0) #endif /* SYS_ARCH_INC */ #ifndef SYS_ARCH_DEC #define SYS_ARCH_DEC(var, val) do { \ SYS_ARCH_DECL_PROTECT(old_level); \ SYS_ARCH_PROTECT(old_level); \ var -= val; \ SYS_ARCH_UNPROTECT(old_level); \ } while(0) #endif /* SYS_ARCH_DEC */ #ifndef SYS_ARCH_GET #define SYS_ARCH_GET(var, ret) do { \ SYS_ARCH_DECL_PROTECT(old_level); \ SYS_ARCH_PROTECT(old_level); \ ret = var; \ SYS_ARCH_UNPROTECT(old_level); \ } while(0) #endif /* SYS_ARCH_GET */ #ifndef SYS_ARCH_SET #define SYS_ARCH_SET(var, val) do { \ SYS_ARCH_DECL_PROTECT(old_level); \ SYS_ARCH_PROTECT(old_level); \ var = val; \ SYS_ARCH_UNPROTECT(old_level); \ } while(0) #endif /* SYS_ARCH_SET */ #ifdef __cplusplus } #endif #endif /* __LWIP_SYS_H__ */