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
17
19 chMtxObjectInit(&mtx->mtx);
20 return 0;
21}
22
24 chMtxLock(&mtx->mtx);
25 return 0;
26}
27
29 return chMtxTryLock(&mtx->mtx) ? 0 : -1;
30}
31
33 chMtxUnlock(&mtx->mtx);
34 return 0;
35}
36
37
38
39void pprz_bsem_init(pprz_bsem_t* bsem, bool taken) {
40 chBSemObjectInit(&bsem->bsem, taken);
41}
42
44 chBSemWait(&bsem->bsem);
45}
46
48 sysinterval_t chtimeout;
49 if(timeout < 0.002) {
50 chtimeout = chTimeUS2I(timeout*1e6);
51 } else {
52 chtimeout = chTimeMS2I(timeout*1e3);
53 }
54 return chBSemWaitTimeout(&bsem->bsem, chtimeout);
55}
56
58 chBSemSignal(&bsem->bsem);
59}
60
61
62static thread_t *main_thread = NULL;
63
65 main_thread = chThdGetSelfX();
66}
67
69 return main_thread == NULL || chThdGetSelfX() == main_thread;
70}
71
72void pprz_thread_assert_main(const char *what, unsigned int id) {
73 (void)what;
74 (void)id;
75 if (!pprz_thread_is_main()) {
76 chSysHalt("not called from the main thread");
77 }
78}
79
80int pprz_thread_create(pprz_thread_t* thread, size_t size, const char *name, uint8_t prio, void (*pf)(void *), void* arg) {
81 *thread = chThdCreateFromHeap(NULL, THD_WORKING_AREA_SIZE(size), name, prio, pf, arg);
82 if(*thread == NULL) {
83 return -1;
84 }
85 return 0;
86}
87
88void pprz_thread_exit(void *retval) {
89 chThdExit((msg_t)retval);
90}
91
92
93int pprz_thread_join(pprz_thread_t* thread, void** retval) {
94 *retval = (void*)chThdWait(*thread);
95 return 0;
96}
97
98
99int pprz_thread_tryjoin(pprz_thread_t* thread, void** retval) {
100 if(chThdTerminatedX(*thread)) {
101 return pprz_thread_join(thread, retval);
102 }
103 return -1;
104}
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 float timeout
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.