libcoap 4.3.5-develop-bdd05e7
Loading...
Searching...
No Matches
coap_threadsafe.c
Go to the documentation of this file.
1/* coap_threadsafe.c -- Thread safe function locking wrappers
2 *
3 * Copyright (C) 2023-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
17
18#if COAP_THREAD_SAFE
19#if COAP_THREAD_RECURSIVE_CHECK
20void
21coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line) {
22 assert(coap_thread_pid == lock->pid);
23 if (lock->in_callback) {
24 assert(lock->lock_count > 0);
25 lock->lock_count--;
26 } else {
27 lock->pid = 0;
28 lock->unlock_file = file;
29 lock->unlock_line = line;
30 coap_mutex_unlock(&lock->mutex);
31 }
32}
33
34int
35coap_lock_lock_func(coap_lock_t *lock, const char *file, int line) {
36 if (!coap_started) {
37 /* libcoap not initialized with coap_startup() */
38 return 0;
39 }
40 if (coap_mutex_trylock(&lock->mutex)) {
41 if (coap_thread_pid == lock->pid) {
42 /* This thread locked the mutex */
43 if (lock->in_callback) {
44 /* This is called from within an app callback */
45 lock->lock_count++;
46 assert(lock->in_callback == lock->lock_count);
47 return 1;
48 } else {
49 coap_log_alert("Thread Deadlock: Last %s: %u, this %s: %u\n",
50 lock->lock_file, lock->lock_line, file, line);
51 assert(0);
52 }
53 }
54 /* Wait for the other thread to unlock */
55 coap_mutex_lock(&lock->mutex);
56 }
57 /* Just got the lock, so should not be in a locked callback */
58 assert(!lock->in_callback);
59 lock->pid = coap_thread_pid;
60 lock->lock_file = file;
61 lock->lock_line = line;
62 return 1;
63}
64
65#else /* ! COAP_THREAD_RECURSIVE_CHECK */
66
67void
68coap_lock_unlock_func(coap_lock_t *lock) {
69 assert(coap_thread_pid == lock->pid);
70 if (lock->in_callback) {
71 assert(lock->lock_count > 0);
72 lock->lock_count--;
73 } else {
74 lock->pid = 0;
75 coap_mutex_unlock(&lock->mutex);
76 }
77}
78
79int
80coap_lock_lock_func(coap_lock_t *lock) {
81 if (!coap_started) {
82 /* libcoap not initialized with coap_startup() */
83 return 0;
84 }
85 /*
86 * Some OS do not have support for coap_mutex_trylock() so
87 * cannot use that here and have to rely on lock-pid being stable
88 */
89 if (lock->in_callback && coap_thread_pid == lock->pid) {
90 lock->lock_count++;
91 assert(lock->in_callback == lock->lock_count);
92 return 1;
93 }
94 coap_mutex_lock(&lock->mutex);
95 /* Just got the lock, so should not be in a locked callback */
96 assert(!lock->in_callback);
97 lock->pid = coap_thread_pid;
98 return 1;
99}
100#endif /* ! COAP_THREAD_RECURSIVE_CHECK */
101
102#if !WITH_LWIP
103extern volatile int coap_thread_quit;
104static pthread_t *thread_id = NULL;
105static uint32_t thread_id_count = 0;
106
107/* Visible to only this thread */
108COAP_THREAD_LOCAL_VAR uint32_t thread_no = 0;
109/* Visible across all threads */
110uint32_t max_thread_no = 0;
111
112typedef struct {
113 coap_context_t *context;
114 uint32_t thread_no;
115} coap_thread_param_t;
116
117static void *
118coap_io_process_worker_thread(void *arg) {
119 coap_thread_param_t *thread_param = (coap_thread_param_t *)arg;
120 coap_context_t *context = thread_param->context;
121#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG)
122 long unsigned int thread_pid = (long unsigned int)coap_thread_pid;
123#endif
124
125 thread_no = thread_param->thread_no;
126 coap_free_type(COAP_STRING, thread_param);
127
128 coap_log_debug("Thread %lx start\n", thread_pid);
129
130 while (!coap_thread_quit) {
131 int result;
132
133 coap_lock_lock(return 0);
134#ifndef __ZEPHYR__
135 result = coap_io_process_lkd(context, COAP_IO_WAIT);
136#else /* __ZEPHYR__ */
137 result = coap_io_process_lkd(context, 1000);
138#endif /* __ZEPHYR__ */
140 if (result < 0 || coap_thread_quit)
141 break;
142 }
144 coap_log_debug("Thread %lx exit\n", thread_pid);
145 return 0;
146}
147
148#if defined(HAVE_SIGNAL_H) || defined(__ZEPHYR__)
149#include <signal.h>
150#endif /* HAVE_SIGNAL_H || __ZEPHYR__ */
151
152/* sighandler_t is not defined is all OS */
153typedef void (*coap_sig_handler_t)(int);
154
155static coap_sig_handler_t old_sigint_handler = SIG_IGN;
156
157static void
158coap_thread_sigint_handler(int signum) {
160 if (old_sigint_handler != SIG_IGN && old_sigint_handler != SIG_DFL) {
161 old_sigint_handler(signum);
162 }
163}
164
165#ifndef _WIN32
166typedef void (*coap_sig_action_t)(int, siginfo_t *, void *);
167static coap_sig_action_t old_sigint_action = NULL;
168
169static void
170coap_thread_sigint_action(int signum, siginfo_t *siginfo, void *ptr) {
172 if (old_sigint_action != NULL) {
173 old_sigint_action(signum, siginfo, ptr);
174 }
175}
176#endif
178int
179coap_io_process_configure_threads(coap_context_t *context, uint32_t thread_count) {
180 uint32_t i;
181
182 coap_mutex_lock(&m_io_threads);
183#ifdef _WIN32
184 old_sigint_handler = signal(SIGINT, coap_thread_sigint_handler);
185#else /* ! _WIN32 */
186 struct sigaction oldact;
187 struct sigaction newact;
188
189 /* Get the old handler / action */
190 if (sigaction(SIGINT, NULL, &oldact) != -1) {
191 if (oldact.sa_flags & SA_SIGINFO) {
192 memset(&newact, 0, sizeof(newact));
193 sigemptyset(&newact.sa_mask);
194 newact.sa_sigaction = coap_thread_sigint_action;
195 newact.sa_flags = SA_SIGINFO;
196 if (sigaction(SIGINT, &newact, &oldact) != -1) {
197 old_sigint_action = oldact.sa_sigaction;
198 }
199 } else {
200 memset(&newact, 0, sizeof(newact));
201 sigemptyset(&newact.sa_mask);
202 newact.sa_handler = coap_thread_sigint_handler;
203 newact.sa_flags = 0;
204 if (sigaction(SIGINT, &newact, &oldact) != -1) {
205 old_sigint_handler = oldact.sa_handler;
206 }
207 }
208 }
209#endif /* ! _WIN32 */
210
211 thread_no = 1;
212 max_thread_no = 1 + thread_count;
213 coap_free_type(COAP_STRING, thread_id);
214 thread_id = coap_malloc_type(COAP_STRING, thread_count * sizeof(pthread_t));
215 if (!thread_id) {
216 coap_log_err("thread start up memory allocate failure\n");
217 coap_mutex_unlock(&m_io_threads);
218 return 0;
219 }
220 for (i = 0; i < thread_count ; i++) {
221 coap_thread_param_t *thread_param = coap_malloc_type(COAP_STRING, sizeof(coap_thread_param_t));
222 int s;
223
224 thread_param->context = context;
225 thread_param->thread_no = i + 2;
226 s = pthread_create(&thread_id[i], NULL,
227 &coap_io_process_worker_thread, thread_param);
228 if (s != 0) {
229 coap_log_err("thread start up failure (%s)\n", coap_socket_strerror());
230 coap_mutex_unlock(&m_io_threads);
231 return 0;
232 }
233 thread_id_count++;
234 }
235 coap_mutex_unlock(&m_io_threads);
236 return 1;
237}
238
239COAP_API void
244}
245
246void
248 uint32_t i;
249
250 (void)context;
251
253 coap_mutex_lock(&m_io_threads);
254
255#ifndef __ZEPHYR__
256 for (i = 0; i < thread_id_count ; i++) {
257 int s = pthread_kill(thread_id[i], SIGINT);
258 if (s != 0) {
259 coap_log_err("thread kill failure\n");
260 }
261 }
262#else /* __ZEPHYR__ */
265#endif /* __ZEPHYR__ */
266
267 for (i = 0; i < thread_id_count ; i++) {
268 void *retval;
269 int s = pthread_join(thread_id[i], &retval);
270 if (s != 0) {
271 coap_log_err("thread join failure\n");
272 }
273 }
274 coap_free_type(COAP_STRING, thread_id);
275 thread_id = NULL;
276 thread_id_count = 0;
277
278 coap_mutex_unlock(&m_io_threads);
279 coap_lock_lock(return);
280}
281#endif /* !WITH_LWIP */
282
283#else /* ! COAP_THREAD_SAFE */
284
285int
286coap_io_process_configure_threads(coap_context_t *context, uint32_t thread_count) {
287 (void)context;
288 (void)thread_count;
289 return 0;
290}
291
292void
294 (void)context;
295}
296
297#endif /* ! COAP_THREAD_SAFE */
const char * coap_socket_strerror(void)
Definition coap_io.c:935
volatile int coap_thread_quit
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:33
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define coap_thread_pid
#define coap_mutex_unlock(a)
#define coap_mutex_trylock(a)
#define coap_mutex_lock(a)
int coap_started
Definition coap_net.c:5188
#define NULL
Definition coap_option.h:30
void coap_io_process_remove_threads_lkd(coap_context_t *context)
Release the coap_io_process() worker threads.
int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
int coap_io_process_configure_threads(coap_context_t *context, uint32_t thread_count)
Configure a defined number of threads to do the alternate coap_io_process() work with traffic load ba...
void coap_io_process_remove_threads(coap_context_t *context)
Release the coap_io_process() worker threads.
#define COAP_IO_WAIT
Definition coap_net.h:831
void coap_send_recv_terminate(void)
Terminate any active coap_send_recv() sessions.
Definition coap_net.c:2149
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition coap_notls.c:171
coap_mutex_t coap_lock_t
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
#define coap_log_alert(...)
Definition coap_debug.h:90
#define coap_log_err(...)
Definition coap_debug.h:102
#define COAP_THREAD_LOCAL_VAR
Definition libcoap.h:84
The CoAP stack's global state is stored in a coap_context_t object.