14#include "modules/core/threads_arch.h"
36 fprintf(stderr,
"%s %u sent outside the main thread\n", what,
id);
42 return pthread_mutex_init(&
mtx->mtx, NULL);
46 return pthread_mutex_lock(&
mtx->mtx);
50 return pthread_mutex_trylock(&
mtx->mtx);
54 return pthread_mutex_unlock(&
mtx->mtx);
60 pthread_mutex_init(&
bsem->mtx, NULL);
61 sem_init(&
bsem->sem, 0, taken ? 0: 1);
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;
76 if (ts.tv_nsec >= 1000000000L) {
78 ts.tv_nsec -= 1000000000L;
80 return sem_timedwait(&
bsem->sem, &ts);
85 pthread_mutex_lock(&
bsem->mtx);
86 if(!sem_getvalue(&
bsem->sem, &
val)) {
91 pthread_mutex_unlock(&
bsem->mtx);
117 int ret = pthread_create(thread, NULL,
stub, (
void*)sa);
118 if(ret) {
return ret;}
119 return pthread_setname_np(*thread, name);
123 pthread_exit(retval);
127 return pthread_join(*thread, retval);
131 return pthread_tryjoin_np(*thread, retval);
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)
static void * stub(void *arg)
static bool main_thread_set
struct pprzBSem pprz_bsem_t
struct pprzMutex pprz_mutex_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.