libcoap 4.3.2rc1
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-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#define COAP_MUTEX_DEFINE(_name) \
33 static coap_mutex_t _name = PTHREAD_MUTEX_INITIALIZER
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
38#elif defined(RIOT_VERSION)
39/* use RIOT's mutex API */
40#include <mutex.h>
41
42typedef mutex_t coap_mutex_t;
43#define COAP_MUTEX_DEFINE(_name) \
44 static coap_mutex_t _name = MUTEX_INIT
45#define coap_mutex_lock(a) mutex_lock(a)
46#define coap_mutex_trylock(a) mutex_trylock(a)
47#define coap_mutex_unlock(a) mutex_unlock(a)
48
49#elif defined(WITH_LWIP)
50/* Use LwIP's mutex API */
51
52#if NO_SYS
53/* Single threaded, no-op'd in lwip/sys.h */
54typedef int coap_mutex_t;
55#define COAP_MUTEX_DEFINE(_name) \
56 static coap_mutex_t _name
57#define coap_mutex_lock(a) *(a) = 1
58#define coap_mutex_trylock(a) *(a) = 1
59#define coap_mutex_unlock(a) *(a) = 0
60#else /* !NO SYS */
61#include <lwip/sys.h>
62typedef sys_mutex_t *coap_mutex_t;
63#define COAP_MUTEX_DEFINE(_name) \
64 static coap_mutex_t _name
65#define TOKENPASTE(x, y) x ## y
66#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
67#define COAP_MUTEX_INITIALIZER (&TOKENPASTE2(coapMutexAt, __LINE__))
68#define coap_mutex_lock(a) sys_mutex_lock(*a)
69#define coap_mutex_unlock(a) sys_mutex_unlock(*a)
70#endif /* !NO SYS */
71
72#elif defined(WITH_CONTIKI)
73/* Contiki does not have a mutex API, used as single thread */
74typedef int coap_mutex_t;
75#define COAP_MUTEX_DEFINE(_name) \
76 static coap_mutex_t _name
77#define coap_mutex_lock(a) *(a) = 1
78#define coap_mutex_trylock(a) *(a) = 1
79#define coap_mutex_unlock(a) *(a) = 0
80
81#elif defined(__ZEPHYR__)
82#include <zephyr/sys/mutex.h>
83
84typedef struct k_mutex coap_mutex_t;
85#define COAP_MUTEX_DEFINE(_name) \
86 static SYS_MUTEX_DEFINE(_name)
87#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER)
88#define coap_mutex_trylock(a) sys_mutex_lock(a, K_NO_WAIT)
89#define coap_mutex_unlock(a) sys_mutex_unlock(a)
90
91#else /* !__ZEPYR__ && !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
92/* define stub mutex functions */
93#warning "stub mutex functions"
94typedef int coap_mutex_t;
95#define COAP_MUTEX_DEFINE(_name) \
96 static coap_mutex_t _name
97#define coap_mutex_lock(a) *(a) = 1
98#define coap_mutex_trylock(a) *(a) = 1
99#define coap_mutex_unlock(a) *(a) = 0
100
101#endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
102
103#endif /* COAP_CONSTRAINED_STACK */
104
105#endif /* COAP_MUTEX_INTERNAL_H_ */