libcoap 4.3.5-develop-e78a250
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#ifdef _WIN32
113#define DWORD sys_thread_t
114#define coap_thread_pid GetCurrentThreadId()
115#else /* ! _WIN32 */
116/* Assume this is therefore FreeRTOS */
117#define coap_thread_pid_t TaskHandle_t
118#define coap_thread_pid xTaskGetCurrentTaskHandle()
119#endif /* ! _WIN32 */
120
121#if COAP_THREAD_RECURSIVE_CHECK
122#error COAP_THREAD_RECURSIVE_CHECK not supported (no coap_mutex_trylock())
123#endif /* COAP_THREAD_RECURSIVE_CHECK */
124#endif /* !LWIP_UNIX_LINUX */
125#endif /* !NO_SYS */
126
127#elif defined(WITH_CONTIKI)
128#if COAP_THREAD_SAFE
129#error Multi-threading not supported (no mutex support)
130#endif /* ! COAP_THREAD_SAFE */
131/* Contiki does not have a mutex API, used as single thread */
132typedef int coap_mutex_t;
133
134#define coap_mutex_init(a) *(a) = 0
135#define coap_mutex_destroy(a) *(a) = 0
136#define coap_mutex_lock(a) *(a) = 1
137#define coap_mutex_trylock(a) *(a) = 1
138#define coap_mutex_unlock(a) *(a) = 0
139#define coap_thread_pid_t int
140#define coap_thread_pid 1
141
142#elif defined(__ZEPHYR__)
143
144typedef struct sys_mutex coap_mutex_t;
145
146#define coap_mutex_init(a) sys_mutex_init(a)
147#define coap_mutex_destroy(a)
148#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER)
149#define coap_mutex_trylock(a) sys_mutex_lock(a, K_NO_WAIT)
150#define coap_mutex_unlock(a) sys_mutex_unlock(a)
151
152#else /* !__ZEPHYR__ && !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
153/* define stub mutex functions */
154#if COAP_THREAD_SAFE
155#error Multi-threading not supported (no mutex support)
156#else /* ! COAP_THREAD_SAFE */
157#if COAP_CONSTRAINED_STACK
158#warning "stub mutex functions"
159#endif /* COAP_CONSTRAINED_STACK */
160#endif /* ! COAP_THREAD_SAFE */
161typedef int coap_mutex_t;
162
163#define coap_mutex_init(a) *(a) = 0
164#define coap_mutex_destroy(a) *(a) = 0
165#define coap_mutex_lock(a) *(a) = 1
166#define coap_mutex_trylock(a) *(a) = 1
167#define coap_mutex_unlock(a) *(a) = 0
168#define coap_thread_pid_t int
169#define coap_thread_pid 1
170
171#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
172
173#if COAP_THREAD_SAFE
174
175extern coap_mutex_t m_show_pdu;
176extern coap_mutex_t m_log_impl;
177extern coap_mutex_t m_io_threads;
178
179#endif /* COAP_THREAD_SAFE */
180
181#ifdef __cplusplus
182}
183#endif
184
185#endif /* COAP_MUTEX_INTERNAL_H_ */
int coap_mutex_t