1 /* 2 * coap_mutex.h -- mutex utilities 3 * 4 * Copyright (C) 2019 Jon Shallow <supjps-libcoap@jpshallow.com> 5 * 2019 Olaf Bergmann <bergmann@tzi.org> 6 * 7 * This file is part of the CoAP library libcoap. Please see README for terms 8 * of use. 9 */ 10 11 /** 12 * @file coap_mutex.h 13 * @brief COAP mutex mechanism wrapper 14 */ 15 16 #ifndef COAP_MUTEX_H_ 17 #define COAP_MUTEX_H_ 18 19 /* 20 * Mutexes are currently only used if there is a constrained stack, 21 * and large static variables (instead of the large variable being on 22 * the stack) need to be protected. 23 */ 24 #if COAP_CONSTRAINED_STACK 25 26 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK) 27 #include <pthread.h> 28 29 typedef pthread_mutex_t coap_mutex_t; 30 #define COAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 31 #define coap_mutex_lock(a) pthread_mutex_lock(a) 32 #define coap_mutex_trylock(a) pthread_mutex_trylock(a) 33 #define coap_mutex_unlock(a) pthread_mutex_unlock(a) 34 35 #elif defined(RIOT_VERSION) 36 /* use RIOT's mutex API */ 37 #include <mutex.h> 38 39 typedef mutex_t coap_mutex_t; 40 #define COAP_MUTEX_INITIALIZER MUTEX_INIT 41 #define coap_mutex_lock(a) mutex_lock(a) 42 #define coap_mutex_trylock(a) mutex_trylock(a) 43 #define coap_mutex_unlock(a) mutex_unlock(a) 44 45 #else 46 /* define stub mutex functions */ 47 typedef int coap_mutex_t; 48 #define COAP_MUTEX_INITIALIZER 0 49 #define coap_mutex_lock(a) *(a) = 1 50 #define coap_mutex_trylock(a) *(a) = 1 51 #define coap_mutex_unlock(a) *(a) = 0 52 53 #endif /* !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */ 54 55 #endif /* COAP_CONSTRAINED_STACK */ 56 57 #endif /* COAP_MUTEX_H_ */ 58