libcoap 4.3.5-develop-3c685a2
Loading...
Searching...
No Matches
coap_threadsafe_internal.h
Go to the documentation of this file.
1/*
2 * coap_threadsafe_internal.h -- Mapping of threadsafe functions
3 *
4 * Copyright (C) 2023-2025 Jon Shallow <supjps-libcoap@jpshallow.com>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see README for terms
9 * of use.
10 */
11
24#ifndef COAP_THREADSAFE_INTERNAL_H_
25#define COAP_THREADSAFE_INTERNAL_H_
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/*
32 * Support thread safe access into libcoap
33 *
34 * Locking at different component levels (i.e context and session) is
35 * problematic in that coap_process_io() needs to lock the context as
36 * it scans for all the sessions and then could lock the session being
37 * processed as well - but context needs to remain locked as a list is
38 * being scanned.
39 *
40 * Then if the session process needs to update context ( e.g. delayqueue),
41 * context needs to be locked. So, if coap_send() is done on a session,
42 * it has to be locked, but a retransmission of a PDU by coap_process_io()
43 * has the context already locked.
44 *
45 * However, when the context is going away (coap_free_context()), other
46 * threads may still be access the lock in what is now freed memory.
47 * A solution (by flagging being freed), worked, but still with a timing
48 * window when the context was finally de-allocated. Coverity Scan did
49 * not like the solution.
50 *
51 * So the initial support for thread safe is done at global lock level
52 * using global_lock.
53 *
54 * Any public API call needs to potentially lock global_lock.
55 *
56 * If a public API needs thread safe protection, the coap_X() function
57 * locks the global_lock lock, calls the coap_X_lkd() function
58 * that does all the work and on return unlocks the global_lock before
59 * returning to the caller of coap_X(). These coap_X() functions
60 * need COAP_API in their definitions.
61 *
62 * Any internal libcoap calls that are to the public API coap_X() must call
63 * coap_X_lkd() if the calling code is already locked.
64 * [The compiler will throw out a deprecation warning against any internal
65 * libcoap call to a COAP_API labelled function]
66 *
67 * Any call-back into app space must be done by using the coap_lock_callback()
68 * (or coap_lock_callback_ret()) wrapper where the global_lock remains locked.
69 *
70 * Note:
71 * libcoap may call a handler, which may in turn call into libcoap, which may
72 * then call a handler. global_lock will remain locked thoughout this process
73 * by the same thread.
74 *
75 * Alternatively, coap_lock_callback_release() (or
76 * coap_lock_callback_ret_release()), is used where the global_lock is unlocked
77 * for the duration of the call-back. Used for things like a request
78 * handler which could be busy for some time.
79 *
80 * Note: On return from the call-back, the code has to be careful not to
81 * use memory locations that may have been updated in the call-back by
82 * calling a Public API.
83 *
84 * Any wait on select() or equivalent when a thread is waiting on an event
85 * must be preceded by unlock global_lock, and then global_lock re-locked after
86 * return;
87 *
88 * To check for recursive deadlock coding errors, COAP_THREAD_RECURSIVE_CHECK
89 * needs to be defined.
90 *
91 * If thread safe is not enabled, then locking of the global_lock does not take
92 * place.
93 */
94
95#if COAP_THREAD_SAFE
96
97# if COAP_THREAD_RECURSIVE_CHECK
98
99/*
100 * Locking, with deadlock detection
101 */
102typedef struct coap_lock_t {
103 coap_mutex_t mutex;
105 const char *lock_file;
106 unsigned int lock_line;
107 unsigned int unlock_line;
108 const char *unlock_file;
109 const char *callback_file;
110 unsigned int callback_line;
111 unsigned int in_callback;
112 unsigned int lock_count;
114
126void coap_lock_unlock_func(const char *file, int line);
127
141int coap_lock_lock_func(const char *file, int line);
142
158#define coap_lock_lock(failed) do { \
159 if (!coap_lock_lock_func(__FILE__, __LINE__)) { \
160 failed; \
161 } \
162 } while (0)
163
171#define coap_lock_unlock() do { \
172 coap_lock_unlock_func(__FILE__, __LINE__); \
173 } while (0)
174
184#define coap_lock_callback(func) do { \
185 coap_lock_check_locked(); \
186 global_lock.in_callback++; \
187 global_lock.callback_file = __FILE__; \
188 global_lock.callback_line = __LINE__; \
189 func; \
190 global_lock.in_callback--; \
191 } while (0)
192
204#define coap_lock_callback_ret(r,func) do { \
205 coap_lock_check_locked(); \
206 global_lock.in_callback++; \
207 global_lock.callback_file = __FILE__; \
208 global_lock.callback_line = __LINE__; \
209 (r) = func; \
210 global_lock.in_callback--; \
211 } while (0)
212
223#define coap_lock_callback_release(func,failed) do { \
224 coap_lock_check_locked(); \
225 coap_lock_unlock(); \
226 func; \
227 coap_lock_lock(failed); \
228 } while (0)
229
242#define coap_lock_callback_ret_release(r,func,failed) do { \
243 coap_lock_check_locked(); \
244 coap_lock_unlock(); \
245 (r) = func; \
246 coap_lock_lock(failed); \
247 } while (0)
248
249extern coap_lock_t global_lock;
250
251# else /* ! COAP_THREAD_RECURSIVE_CHECK */
252
253/*
254 * Locking, but no deadlock detection
255 */
256typedef struct coap_lock_t {
257 coap_mutex_t mutex;
259 uint32_t in_callback;
260 volatile uint32_t lock_count;
262
272void coap_lock_unlock_func(void);
273
284int coap_lock_lock_func(void);
285
301#define coap_lock_lock(failed) do { \
302 if (!coap_lock_lock_func()) { \
303 failed; \
304 } \
305 } while (0)
306
314#define coap_lock_unlock() do { \
315 coap_lock_unlock_func(); \
316 } while (0)
317
327#define coap_lock_callback(func) do { \
328 coap_lock_check_locked(); \
329 global_lock.in_callback++; \
330 func; \
331 global_lock.in_callback--; \
332 } while (0)
333
345#define coap_lock_callback_ret(r,func) do { \
346 coap_lock_check_locked(); \
347 global_lock.in_callback++; \
348 (r) = func; \
349 global_lock.in_callback--; \
350 } while (0)
351
362#define coap_lock_callback_release(func,failed) do { \
363 coap_lock_check_locked(); \
364 coap_lock_unlock(); \
365 func; \
366 coap_lock_lock(failed); \
367 } while (0)
368
381#define coap_lock_callback_ret_release(r,func,failed) do { \
382 coap_lock_check_locked(); \
383 coap_lock_unlock(); \
384 (r) = func; \
385 coap_lock_lock(failed); \
386 } while (0)
387
388# endif /* ! COAP_THREAD_RECURSIVE_CHECK */
389
393#define coap_lock_init() do { \
394 memset(&global_lock.mutex, 0, sizeof(global_lock.mutex)); \
395 coap_mutex_init(&global_lock.mutex); \
396 } while (0)
397
401#define coap_lock_check_locked() do { \
402 assert(coap_thread_pid == global_lock.pid); \
403 } while (0)
404
417#define coap_lock_invert(alt_lock,failed) do { \
418 coap_lock_check_locked(); \
419 coap_lock_unlock(); \
420 alt_lock; \
421 coap_lock_lock(failed); \
422 } while (0)
423
424extern coap_lock_t global_lock;
425
426#else /* ! COAP_THREAD_SAFE */
427
428/*
429 * No locking - single thread
430 */
432
450#define coap_lock_lock(failed)
451
460#define coap_lock_unlock()
461
467#define coap_lock_init()
468
474#define coap_lock_check_locked() {}
475
487#define coap_lock_callback(func) func
488
502#define coap_lock_callback_ret(r,func) (r) = func
503
516#define coap_lock_callback_release(func,failed) func
517
533#define coap_lock_callback_ret_release(r,func,failed) (r) = func
534
549#define coap_lock_invert(alt_lock,failed) alt_lock
550
551#endif /* ! COAP_THREAD_SAFE */
552
555#ifdef __cplusplus
556}
557#endif
558
559#endif /* COAP_THREADSAFE_INTERNAL_H_ */
int coap_mutex_t
#define coap_thread_pid_t
coap_mutex_t coap_lock_t