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
thd_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 fab <fab@github.com>
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 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
28#include "led.h"
30#include <stdio.h>
31
33
35
36pprz_mutex_t mtx; // mutex protecting syracuse
37static int syracuse = 14;
38
39// Remember if the thread has already been joined or not.
40// From https://linux.die.net/man/3/pthread_join:
41// "Joining with a thread that has previously been joined results in undefined behavior."
42static bool joined = true;
43
44static void test_thd(void*);
45
47
49 // try changing syracuse
50 // non blocking because we are in the main loop.
51 // If it doesn't work, just try again!
52 if(!pprz_mtx_trylock(&mtx)) {
53 syracuse = (int)s;
56
57 // if it was stopped, restart it!
58 if(joined) {
60 joined = false;
61 }
62 }
63 } else {
64 char err[] = "Could not lock";
66 }
67}
68
69void thd_test_init(void)
70{
71 pprz_bsem_init(&bsem, true); // init binary semaphore
72 pprz_mtx_init(&mtx); // init mutex
73 // create thread, and remember to try joining it.
75 joined = false;
76 }
77}
78
80{
81 if(!joined) {
82 size_t ret;
83 char msg[30];
84 size_t len = 0;
85 if(!pprz_thread_tryjoin(&thd_handle, (void**)(&ret))) {
86 // try joining the thread. If successful, send its exit value.
87 len = snprintf(msg, sizeof(msg), "Finished in %zu steps!", ret);
88 joined = true;
89 } else {
90 // the thread is still running, print the current value of the syracuse suit (mutex protected)
91 // then signal the binary semaphore to allow the thread to resume execution
92 if(!pprz_mtx_trylock(&mtx)) {
93 len = snprintf(msg, sizeof(msg), "%d", syracuse);
96 }
97 }
98 // send the message to the ground (current value or end message)
99 if(len > 0) {
101 }
102
103
104 }
105
106}
107
108
109static void test_thd(void* arg) {
110 (void)arg;
111 size_t count = 0;
112 while(syracuse != 1) {
113 pprz_bsem_wait(&bsem); // wait to be woken up by the AP thread
114
116 if(syracuse%2) {
117 syracuse = 3*syracuse + 1;
118 } else {
119 syracuse = syracuse / 2;
120 }
122 count++;
123 }
124
125 pprz_thread_exit((void*)((size_t)count));
126}
int pprz_mtx_trylock(pprz_mutex_t *mtx)
Performs a nonblocking lock on the mutex.
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)
#define PPRZ_NORMAL_PRIO
thread_t * pprz_thread_t
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
arch independent LED (Light Emitting Diodes) API
static uint32_t s
uint16_t foo
Definition main_demo5.c:58
Periodic telemetry system header (includes downlink utility and generated code).
pprz_bsem_t bsem
Definition thd_test.c:34
static void test_thd(void *)
Definition thd_test.c:109
static int syracuse
Definition thd_test.c:37
void thd_test_periodic(void)
Definition thd_test.c:79
void thd_test_init(void)
Definition thd_test.c:69
void thd_test_syracuse_restart(float s)
Definition thd_test.c:48
pprz_mutex_t mtx
Definition thd_test.c:36
static bool joined
Definition thd_test.c:42
pprz_thread_t thd_handle
Definition thd_test.c:46
float thd_test_start_value
Definition thd_test.c:32