libcoap 4.3.1
coap_mutex.h
Go to the documentation of this file.
1/*
2 * coap_mutex.h -- mutex utilities
3 *
4 * Copyright (C) 2019-2022 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
31typedef 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
41typedef 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#elif defined(WITH_LWIP)
48/* Use LwIP's mutex API */
49
50#if NO_SYS
51/* Single threaded, no-op'd in lwip/sys.h */
52typedef int coap_mutex_t;
53#define COAP_MUTEX_INITIALIZER 0
54#define coap_mutex_lock(a) *(a) = 1
55#define coap_mutex_trylock(a) *(a) = 1
56#define coap_mutex_unlock(a) *(a) = 0
57#else /* !NO SYS */
58#error Need support for LwIP mutex
59#endif /* !NO SYS */
60
61#elif defined(WITH_CONTIKI)
62/* Contiki does not have a mutex API, used as single thread */
63typedef int coap_mutex_t;
64#define COAP_MUTEX_INITIALIZER 0
65#define coap_mutex_lock(a) *(a) = 1
66#define coap_mutex_trylock(a) *(a) = 1
67#define coap_mutex_unlock(a) *(a) = 0
68
69#else /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
70/* define stub mutex functions */
71#warning "stub mutex functions"
72typedef int coap_mutex_t;
73#define COAP_MUTEX_INITIALIZER 0
74#define coap_mutex_lock(a) *(a) = 1
75#define coap_mutex_trylock(a) *(a) = 1
76#define coap_mutex_unlock(a) *(a) = 0
77
78#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
79
80#endif /* COAP_CONSTRAINED_STACK */
81
82#endif /* COAP_MUTEX_H_ */