libcoap 4.3.5-develop-f0533d3
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-2026 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
127void coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line);
128
143int coap_lock_lock_func(coap_lock_t *lock, const char *file, int line);
144
160#define coap_lock_lock(failed) do { \
161 if (!coap_lock_lock_func(&global_lock, __FILE__, __LINE__)) { \
162 failed; \
163 } \
164 } while (0)
165
173#define coap_lock_unlock() do { \
174 coap_lock_unlock_func(&global_lock, __FILE__, __LINE__); \
175 } while (0)
176
193#define coap_lock_specific_lock(lock, failed) do { \
194 if (!coap_lock_lock_func(lock, __FILE__, __LINE__)) { \
195 failed; \
196 } \
197 } while (0)
198
209#define coap_lock_specific_unlock(lock) do { \
210 coap_lock_unlock_func(lock, __FILE__, __LINE__); \
211 } while (0)
212
222#define coap_lock_callback(func) do { \
223 coap_lock_check_locked(); \
224 global_lock.in_callback++; \
225 global_lock.callback_file = __FILE__; \
226 global_lock.callback_line = __LINE__; \
227 func; \
228 global_lock.in_callback--; \
229 } while (0)
230
242#define coap_lock_callback_ret(r,func) do { \
243 coap_lock_check_locked(); \
244 global_lock.in_callback++; \
245 global_lock.callback_file = __FILE__; \
246 global_lock.callback_line = __LINE__; \
247 (r) = func; \
248 global_lock.in_callback--; \
249 } while (0)
250
261#define coap_lock_callback_release(func,failed) do { \
262 coap_lock_check_locked(); \
263 coap_lock_unlock(); \
264 func; \
265 coap_lock_lock(failed); \
266 } while (0)
267
280#define coap_lock_callback_ret_release(r,func,failed) do { \
281 coap_lock_check_locked(); \
282 coap_lock_unlock(); \
283 (r) = func; \
284 coap_lock_lock(failed); \
285 } while (0)
286
300#define coap_lock_specific_callback_release(lock,func,failed) do { \
301 coap_lock_check_locked(); \
302 coap_lock_unlock(); \
303 coap_lock_specific_lock(lock, failed); \
304 (lock)->in_callback++; \
305 (lock)->callback_file = __FILE__; \
306 (lock)->callback_line = __LINE__; \
307 func; \
308 (lock)->in_callback--; \
309 coap_lock_specific_unlock(lock); \
310 coap_lock_lock(failed); \
311 } while (0)
312
313extern coap_lock_t global_lock;
314
315# else /* ! COAP_THREAD_RECURSIVE_CHECK */
316
317/*
318 * Locking, but no deadlock detection
319 */
320typedef struct coap_lock_t {
321 coap_mutex_t mutex;
323 uint32_t in_callback;
324 volatile uint32_t lock_count;
326
338void coap_lock_unlock_func(coap_lock_t *lock);
339
352int coap_lock_lock_func(coap_lock_t *lock);
353
369#define coap_lock_lock(failed) do { \
370 if (!coap_lock_lock_func(&global_lock)) { \
371 failed; \
372 } \
373 } while (0)
374
391#define coap_lock_specific_lock(lock, failed) do { \
392 if (!coap_lock_lock_func(lock)) { \
393 failed; \
394 } \
395 } while (0)
396
404#define coap_lock_unlock() do { \
405 coap_lock_unlock_func(&global_lock); \
406 } while (0)
407
418#define coap_lock_specific_unlock(lock) do { \
419 coap_lock_unlock_func(lock); \
420 } while (0)
421
431#define coap_lock_callback(func) do { \
432 coap_lock_check_locked(); \
433 global_lock.in_callback++; \
434 func; \
435 global_lock.in_callback--; \
436 } while (0)
437
449#define coap_lock_callback_ret(r,func) do { \
450 coap_lock_check_locked(); \
451 global_lock.in_callback++; \
452 (r) = func; \
453 global_lock.in_callback--; \
454 } while (0)
455
466#define coap_lock_callback_release(func,failed) do { \
467 coap_lock_check_locked(); \
468 coap_lock_unlock(); \
469 func; \
470 coap_lock_lock(failed); \
471 } while (0)
472
486#define coap_lock_specific_callback_release(lock,func,failed) do { \
487 coap_lock_check_locked(); \
488 coap_lock_unlock(); \
489 coap_lock_specific_lock(lock, failed); \
490 (lock)->in_callback++; \
491 func; \
492 (lock)->in_callback--; \
493 coap_lock_specific_unlock(lock); \
494 coap_lock_lock(failed); \
495 } while (0)
496
509#define coap_lock_callback_ret_release(r,func,failed) do { \
510 coap_lock_check_locked(); \
511 coap_lock_unlock(); \
512 (r) = func; \
513 coap_lock_lock(failed); \
514 } while (0)
515
516# endif /* ! COAP_THREAD_RECURSIVE_CHECK */
517
521#define coap_lock_init(lock) do { \
522 memset(&(lock)->mutex, 0, sizeof((lock)->mutex)); \
523 coap_mutex_init(&(lock)->mutex); \
524 } while (0)
525
529#define coap_lock_check_locked() do { \
530 assert(coap_thread_pid == global_lock.pid); \
531 } while (0)
532
545#define coap_lock_invert(alt_lock,failed) do { \
546 coap_lock_check_locked(); \
547 coap_lock_unlock(); \
548 alt_lock; \
549 coap_lock_lock(failed); \
550 } while (0)
551
552extern coap_lock_t global_lock;
553
554#else /* ! COAP_THREAD_SAFE */
555
556/*
557 * No locking - single thread
558 */
560
578#define coap_lock_lock(failed)
579
598#define coap_lock_specific_lock(lock,failed)
599
608#define coap_lock_unlock()
609
620#define coap_lock_specific_unlock(lock)
621
627#define coap_lock_init(lock)
628
634#define coap_lock_check_locked() {}
635
647#define coap_lock_callback(func) func
648
662#define coap_lock_callback_ret(r,func) (r) = func
663
676#define coap_lock_callback_release(func,failed) func
677
688#define coap_lock_specific_callback_release(lock,func,failed) func
689
705#define coap_lock_callback_ret_release(r,func,failed) (r) = func
706
721#define coap_lock_invert(alt_lock,failed) alt_lock
722
723#endif /* ! COAP_THREAD_SAFE */
724
727#ifdef __cplusplus
728}
729#endif
730
731#endif /* COAP_THREADSAFE_INTERNAL_H_ */
int coap_mutex_t
#define coap_thread_pid_t
coap_mutex_t coap_lock_t