libcoap 4.3.4
coap_mutex_internal.h
Go to the documentation of this file.
1/*
2 * coap_mutex.h -- mutex utilities
3 *
4 * Copyright (C) 2019-2023 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_INTERNAL_H_
19#define COAP_MUTEX_INTERNAL_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
33#define coap_mutex_init(a) pthread_mutex_init(a, NULL)
34#define coap_mutex_destroy(a) pthread_mutex_destroy(a)
35#define coap_mutex_lock(a) pthread_mutex_lock(a)
36#define coap_mutex_trylock(a) pthread_mutex_trylock(a)
37#define coap_mutex_unlock(a) pthread_mutex_unlock(a)
38
39#elif defined(RIOT_VERSION)
40/* use RIOT's mutex API */
41#include <mutex.h>
42
43typedef mutex_t coap_mutex_t;
44
45#define coap_mutex_init(a) mutex_init(a)
46#define coap_mutex_destroy(a)
47#define coap_mutex_lock(a) mutex_lock(a)
48#define coap_mutex_trylock(a) mutex_trylock(a)
49#define coap_mutex_unlock(a) mutex_unlock(a)
50
51#elif defined(WITH_LWIP)
52/* Use LwIP's mutex API */
53
54#if NO_SYS
55/* Single threaded, no-op'd in lwip/sys.h */
56typedef int coap_mutex_t;
57
58#define coap_mutex_init(a) *(a) = 0
59#define coap_mutex_destroy(a) *(a) = 0
60#define coap_mutex_lock(a) *(a) = 1
61#define coap_mutex_trylock(a) *(a) = 1
62#define coap_mutex_unlock(a) *(a) = 0
63
64#else /* !NO SYS */
65#include <lwip/sys.h>
66typedef sys_mutex_t coap_mutex_t;
67
68#define coap_mutex_init(a) sys_mutex_new(a)
69#define coap_mutex_destroy(a) sys_mutex_set_invalid(a)
70#define coap_mutex_lock(a) sys_mutex_lock(a)
71#define coap_mutex_trylock(a) sys_mutex_lock(a)
72#define coap_mutex_unlock(a) sys_mutex_unlock(a)
73#endif /* !NO SYS */
74
75#elif defined(WITH_CONTIKI)
76/* Contiki does not have a mutex API, used as single thread */
77typedef int coap_mutex_t;
78
79#define coap_mutex_init(a) *(a) = 0
80#define coap_mutex_destroy(a) *(a) = 0
81#define coap_mutex_lock(a) *(a) = 1
82#define coap_mutex_trylock(a) *(a) = 1
83#define coap_mutex_unlock(a) *(a) = 0
84
85#elif defined(__ZEPHYR__)
86#include <zephyr/sys/mutex.h>
87
88typedef struct sys_mutex coap_mutex_t;
89
90#define coap_mutex_init(a) sys_mutex_init(a)
91#define coap_mutex_destroy(a)
92#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER)
93#define coap_mutex_trylock(a) sys_mutex_lock(a, K_NO_WAIT)
94#define coap_mutex_unlock(a) sys_mutex_unlock(a)
95
96#else /* !__ZEPYR__ && !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
97/* define stub mutex functions */
98#warning "stub mutex functions"
99typedef int coap_mutex_t;
100
101#define coap_mutex_init(a) *(a) = 0
102#define coap_mutex_destroy(a) *(a) = 0
103#define coap_mutex_lock(a) *(a) = 1
104#define coap_mutex_trylock(a) *(a) = 1
105#define coap_mutex_unlock(a) *(a) = 0
106
107#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
108
109extern coap_mutex_t m_show_pdu;
110extern coap_mutex_t m_log_impl;
111extern coap_mutex_t m_dtls_recv;
112extern coap_mutex_t m_read_session;
113extern coap_mutex_t m_read_endpoint;
114extern coap_mutex_t m_persist_add;
115
116#endif /* COAP_CONSTRAINED_STACK */
117
118#endif /* COAP_MUTEX_INTERNAL_H_ */