libcoap 4.3.5-develop-ea01661
Loading...
Searching...
No Matches
coap_mutex_internal.h
Go to the documentation of this file.
1/*
2 * coap_mutex.h -- mutex utilities
3 *
4 * Copyright (C) 2019-2025 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 * Do all the #include before the extern "C"
23 */
24#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
25#include <pthread.h>
26#elif defined(RIOT_VERSION)
27/* use RIOT's mutex API */
28#include <mutex.h>
29#elif defined(WITH_LWIP) && defined(NO_SYS)
30#include <lwip/sys.h>
31#elif defined(__ZEPHYR__)
32#include <zephyr/sys/mutex.h>
33#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/*
40 * Mutexes are used for
41 * 1) If there is a constrained stack, and large static variables (instead
42 * of the large variable being on the stack) need to be protected.
43 * 2) libcoap if built with thread safe support.
44 */
45
46#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
47typedef pthread_mutex_t coap_mutex_t;
48
49#define coap_mutex_init(a) pthread_mutex_init(a, NULL)
50#define coap_mutex_destroy(a) pthread_mutex_destroy(a)
51#define coap_mutex_lock(a) pthread_mutex_lock(a)
52#define coap_mutex_trylock(a) pthread_mutex_trylock(a)
53#define coap_mutex_unlock(a) pthread_mutex_unlock(a)
54#if defined(ESPIDF_VERSION)
55#define coap_thread_pid_t TaskHandle_t
56#define coap_thread_pid xTaskGetCurrentTaskHandle()
57#else /* !ESPIDF_VERSION */
58#define coap_thread_pid_t pthread_t
59#define coap_thread_pid pthread_self()
60#endif /* !ESPIDF_VERSION */
61
62#elif defined(RIOT_VERSION)
63
64typedef mutex_t coap_mutex_t;
65
66#define coap_mutex_init(a) mutex_init(a)
67#define coap_mutex_destroy(a)
68#define coap_mutex_lock(a) mutex_lock(a)
69#define coap_mutex_trylock(a) mutex_trylock(a)
70#define coap_mutex_unlock(a) mutex_unlock(a)
71#define coap_thread_pid_t kernel_pid_t
72#define coap_thread_pid thread_getpid()
73
74#elif defined(WITH_LWIP)
75/* Use LwIP's mutex API */
76
77#if NO_SYS
78#if COAP_THREAD_SAFE
79#error Multi-threading not supported (no mutex support)
80#endif /* ! COAP_THREAD_SAFE */
81/* Single threaded, no-op'd in lwip/sys.h */
82typedef int coap_mutex_t;
83
84#define coap_mutex_init(a) *(a) = 0
85#define coap_mutex_destroy(a) *(a) = 0
86#define coap_mutex_lock(a) *(a) = 1
87#define coap_mutex_trylock(a) *(a) = 1
88#define coap_mutex_unlock(a) *(a) = 0
89#define coap_thread_pid_t int
90#define coap_thread_pid 1
91
92#else /* !NO_SYS */
93#include <lwip/sys.h>
94#ifdef LWIP_UNIX_LINUX
95#include <pthread.h>
96typedef pthread_mutex_t coap_mutex_t;
97
98#define coap_mutex_init(a) pthread_mutex_init(a, NULL)
99#define coap_mutex_destroy(a) pthread_mutex_destroy(a)
100#define coap_mutex_lock(a) pthread_mutex_lock(a)
101#define coap_mutex_trylock(a) pthread_mutex_trylock(a)
102#define coap_mutex_unlock(a) pthread_mutex_unlock(a)
103#define coap_thread_pid_t pthread_t
104#define coap_thread_pid pthread_self()
105#else /* ! LWIP_UNIX_LINUX */
106typedef sys_mutex_t coap_mutex_t;
107
108#define coap_mutex_init(a) sys_mutex_new(a)
109#define coap_mutex_destroy(a) sys_mutex_set_invalid(a)
110#define coap_mutex_lock(a) sys_mutex_lock(a)
111#define coap_mutex_unlock(a) sys_mutex_unlock(a)
112#define coap_thread_pid_t sys_thread_t
113#define coap_thread_pid (coap_thread_pid_t)1
114
115#if COAP_THREAD_RECURSIVE_CHECK
116#error COAP_THREAD_RECURSIVE_CHECK not supported (no coap_mutex_trylock())
117#endif /* COAP_THREAD_RECURSIVE_CHECK */
118#endif /* !LWIP_UNIX_LINUX */
119#endif /* !NO_SYS */
120
121#elif defined(WITH_CONTIKI)
122#if COAP_THREAD_SAFE
123#error Multi-threading not supported (no mutex support)
124#endif /* ! COAP_THREAD_SAFE */
125/* Contiki does not have a mutex API, used as single thread */
126typedef int coap_mutex_t;
127
128#define coap_mutex_init(a) *(a) = 0
129#define coap_mutex_destroy(a) *(a) = 0
130#define coap_mutex_lock(a) *(a) = 1
131#define coap_mutex_trylock(a) *(a) = 1
132#define coap_mutex_unlock(a) *(a) = 0
133#define coap_thread_pid_t int
134#define coap_thread_pid 1
135
136#elif defined(__ZEPHYR__)
137
138typedef struct sys_mutex coap_mutex_t;
139
140#define coap_mutex_init(a) sys_mutex_init(a)
141#define coap_mutex_destroy(a)
142#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER)
143#define coap_mutex_trylock(a) sys_mutex_lock(a, K_NO_WAIT)
144#define coap_mutex_unlock(a) sys_mutex_unlock(a)
145
146#else /* !__ZEPHYR__ && !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
147/* define stub mutex functions */
148#if COAP_THREAD_SAFE
149#error Multi-threading not supported (no mutex support)
150#else /* ! COAP_THREAD_SAFE */
151#if COAP_CONSTRAINED_STACK
152#warning "stub mutex functions"
153#endif /* COAP_CONSTRAINED_STACK */
154#endif /* ! COAP_THREAD_SAFE */
155typedef int coap_mutex_t;
156
157#define coap_mutex_init(a) *(a) = 0
158#define coap_mutex_destroy(a) *(a) = 0
159#define coap_mutex_lock(a) *(a) = 1
160#define coap_mutex_trylock(a) *(a) = 1
161#define coap_mutex_unlock(a) *(a) = 0
162#define coap_thread_pid_t int
163#define coap_thread_pid 1
164
165#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
166
167#if COAP_THREAD_SAFE
168
169extern coap_mutex_t m_show_pdu;
170extern coap_mutex_t m_log_impl;
171extern coap_mutex_t m_io_threads;
172
173#endif /* COAP_THREAD_SAFE */
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* COAP_MUTEX_INTERNAL_H_ */
int coap_mutex_t