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