Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
threads_arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 The Paparazzi Team
3 *
4 * This file is part of paparazzi.
5 *
6 * Paparazzi is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * See LICENSE file for the full license version, or see http://www.gnu.org/licenses/
12 */
14#include "modules/core/threads_arch.h"
15#include "stdbool.h"
16#include <semaphore.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <time.h>
20
21
22static pthread_t main_thread;
23static bool main_thread_set = false;
24
26 main_thread = pthread_self();
27 main_thread_set = true;
28}
29
31 return !main_thread_set || pthread_equal(pthread_self(), main_thread);
32}
33
34void pprz_thread_assert_main(const char *what, unsigned int id) {
35 if (!pprz_thread_is_main()) {
36 fprintf(stderr, "%s %u sent outside the main thread\n", what, id);
37 abort();
38 }
39}
40
42 return pthread_mutex_init(&mtx->mtx, NULL);
43}
44
46 return pthread_mutex_lock(&mtx->mtx);
47}
48
50 return pthread_mutex_trylock(&mtx->mtx);
51}
52
54 return pthread_mutex_unlock(&mtx->mtx);
55}
56
57
58
59void pprz_bsem_init(pprz_bsem_t* bsem, bool taken) {
60 pthread_mutex_init(&bsem->mtx, NULL);
61 sem_init(&bsem->sem, 0, taken ? 0: 1);
62}
63
65 sem_wait(&bsem->sem);
66}
67
69 struct timespec ts;
70 clock_gettime(CLOCK_REALTIME, &ts);
71 time_t timeout_secs = (time_t)timeout;
72 long timeout_nsecs = (timeout - timeout_secs) * 1e9;
73 ts.tv_sec += timeout_secs;
74 ts.tv_nsec += timeout_nsecs;
75 // tv_nsec must stay below 1 s, else sem_timedwait fails at once with EINVAL
76 if (ts.tv_nsec >= 1000000000L) {
77 ts.tv_sec += 1;
78 ts.tv_nsec -= 1000000000L;
79 }
80 return sem_timedwait(&bsem->sem, &ts);
81}
82
84 int val;
85 pthread_mutex_lock(&bsem->mtx);
86 if(!sem_getvalue(&bsem->sem, &val)) {
87 if(val < 1) {
88 sem_post(&bsem->sem);
89 }
90 }
91 pthread_mutex_unlock(&bsem->mtx);
92}
93
94
95
96// stub function argument: the thread function to run with its argument.
98{
99 void (*func)(void*);
100 void* arg;
101};
102
103// stub function, ran in a new thread
104static void* stub(void* arg) {
105 struct stub_arg* sa = (struct stub_arg*) arg;
106 sa->func(sa->arg);
107 free(sa);
108 return NULL;
109}
110
111int pprz_thread_create(pprz_thread_t* thread, size_t size, const char *name, uint8_t prio, void (*func)(void*), void* arg) {
112 (void)size;
113 (void)prio;
114 struct stub_arg *sa = malloc(sizeof(struct stub_arg)); // allocate a stub arg. Must be allocated on the heap
115 sa->func = func;
116 sa->arg = arg;
117 int ret = pthread_create(thread, NULL, stub, (void*)sa); // launch the stub in a new thread
118 if(ret) {return ret;}
119 return pthread_setname_np(*thread, name);
120}
121
122void pprz_thread_exit(void *retval) {
123 pthread_exit(retval);
124}
125
126int pprz_thread_join(pprz_thread_t* thread, void** retval) {
127 return pthread_join(*thread, retval);
128}
129
130int pprz_thread_tryjoin(pprz_thread_t* thread, void** retval) {
131 return pthread_tryjoin_np(*thread, retval);
132}
void pprz_thread_init_main(void)
Remember the calling thread as the main thread, the one running the periodic and event functions of t...
int pprz_mtx_trylock(pprz_mutex_t *mtx)
Performs a nonblocking lock on the mutex.
bool pprz_thread_is_main(void)
The calling thread is the main thread (also true before pprz_thread_init_main() was called).
int pprz_thread_join(pprz_thread_t *thread, void **retval)
Wait for the thread to terminate.
int pprz_bsem_wait_timeout(pprz_bsem_t *bsem, float timeout)
Wait on semaphore no more than timeout.
int pprz_mtx_unlock(pprz_mutex_t *mtx)
void pprz_bsem_init(pprz_bsem_t *bsem, bool taken)
int pprz_thread_create(pprz_thread_t *thread, size_t size, const char *name, uint8_t prio, void(*pf)(void *), void *arg)
Creates a new thread whose stack is dynamically allocated.
void pprz_thread_assert_main(const char *what, unsigned int id)
Stop the program (abort on Linux/NPS, halt on ChibiOS) when not called from the main thread,...
int pprz_mtx_init(pprz_mutex_t *mtx)
int pprz_mtx_lock(pprz_mutex_t *mtx)
static thread_t * main_thread
void pprz_bsem_signal(pprz_bsem_t *bsem)
int pprz_thread_tryjoin(pprz_thread_t *thread, void **retval)
Performs a nonblocking join with the thread.
void pprz_thread_exit(void *retval)
Exit the current thread.
void pprz_bsem_wait(pprz_bsem_t *bsem)
thread_t * pprz_thread_t
static void * stub(void *arg)
static bool main_thread_set
static float timeout
void(* func)(void *)
void * arg
uint16_t val[TCOUPLE_NB]
pprz_bsem_t bsem
Definition thd_test.c:34
pprz_mutex_t mtx
Definition thd_test.c:36
struct pprzBSem pprz_bsem_t
Definition threads.h:22
struct pprzMutex pprz_mutex_t
Definition threads.h:21
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.