libcoap  4.3.0
coap_mutex.h
Go to the documentation of this file.
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  * SPDX-License-Identifier: BSD-2-Clause
8  *
9  * This file is part of the CoAP library libcoap. Please see README for terms
10  * of use.
11  */
12 
18 #ifndef COAP_MUTEX_H_
19 #define COAP_MUTEX_H_
20 
21 /*
22  * Mutexes are currently only used if there is a constrained stack,
23  * and large static variables (instead of the large variable being on
24  * the stack) need to be protected.
25  */
26 #if COAP_CONSTRAINED_STACK
27 
28 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
29 #include <pthread.h>
30 
31 typedef pthread_mutex_t coap_mutex_t;
32 #define COAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
33 #define coap_mutex_lock(a) pthread_mutex_lock(a)
34 #define coap_mutex_trylock(a) pthread_mutex_trylock(a)
35 #define coap_mutex_unlock(a) pthread_mutex_unlock(a)
36 
37 #elif defined(RIOT_VERSION)
38 /* use RIOT's mutex API */
39 #include <mutex.h>
40 
41 typedef mutex_t coap_mutex_t;
42 #define COAP_MUTEX_INITIALIZER MUTEX_INIT
43 #define coap_mutex_lock(a) mutex_lock(a)
44 #define coap_mutex_trylock(a) mutex_trylock(a)
45 #define coap_mutex_unlock(a) mutex_unlock(a)
46 
47 #else
48 /* define stub mutex functions */
49 typedef int coap_mutex_t;
50 #define COAP_MUTEX_INITIALIZER 0
51 #define coap_mutex_lock(a) *(a) = 1
52 #define coap_mutex_trylock(a) *(a) = 1
53 #define coap_mutex_unlock(a) *(a) = 0
54 
55 #endif /* !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
56 
57 #endif /* COAP_CONSTRAINED_STACK */
58 
59 #endif /* COAP_MUTEX_H_ */