Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 <stdlib.h>
18
19
23
27
31
35
36
37
42
46
48 int val;
50 if(!sem_getvalue(&bsem->sem, &val)) {
51 if(val < 1) {
52 sem_post(&bsem->sem);
53 }
54 }
56}
57
58
59
60// stub function argument: the thread function to run with its argument.
61struct stub_arg
62{
63 void (*func)(void*);
64 void* arg;
65};
66
67// stub function, ran in a new thread
68static void* stub(void* arg) {
69 struct stub_arg* sa = (struct stub_arg*) arg;
70 sa->func(sa->arg);
71 free(sa);
72 return NULL;
73}
74
75int pprz_thread_create(pprz_thread_t* thread, size_t size, const char *name, uint8_t prio, void (*func)(void*), void* arg) {
76 (void)size;
77 (void)prio;
78 struct stub_arg *sa = malloc(sizeof(struct stub_arg)); // allocate a stub arg. Must be allocated on the heap
79 sa->func = func;
80 sa->arg = arg;
81 int ret = pthread_create(thread, NULL, stub, (void*)sa); // launch the stub in a new thread
82 if(ret) {return ret;}
83 return pthread_setname_np(*thread, name);
84}
85
89
91 return pthread_join(*thread, retval);
92}
93
95 return pthread_tryjoin_np(*thread, retval);
96}
int pprz_mtx_trylock(pprz_mutex_t *mtx)
Performs a nonblocking lock on the mutex.
int pprz_thread_join(pprz_thread_t *thread, void **retval)
Wait for the thread to terminate.
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.
int pprz_mtx_init(pprz_mutex_t *mtx)
int pprz_mtx_lock(pprz_mutex_t *mtx)
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)
uint16_t foo
Definition main_demo5.c:58
sem_t sem
pthread_mutex_t mtx
mutex_t mtx
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
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.