refactor(simu): use C++ native threads (#6449)

Co-authored-by: raphaelcoeffic <1050031+raphaelcoeffic@users.noreply.github.com>
Co-authored-by: Malte Langermann <malte@langermann.net>
This commit is contained in:
Raphael Coeffic
2025-07-20 13:57:52 +02:00
committed by GitHub
co-authored by raphaelcoeffic Malte Langermann
parent 4eddda034d
commit 7d91cdd428
18 changed files with 47 additions and 230 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
*/
#include "async.h"
#include "timer_pthread_impl.h"
#include "timer_native_impl.h"
bool async_call(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2)
+2 -3
View File
@@ -21,12 +21,11 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#include "task_pthread.h"
#if defined(NATIVE_THREADS)
#include "task_native.h"
#elif defined(FREE_RTOS)
#include "task_freertos.h"
#endif
@@ -21,10 +21,10 @@
#include "task.h"
#include "debug.h"
#include "timer_pthread_impl.h"
#include "timer_native_impl.h"
#include <pthread.h>
#include <chrono>
#include <memory>
#include <vector>
#include <mutex>
#include <string>
@@ -33,7 +33,7 @@ static std::condition_variable _stop_cv;
static std::mutex _stop_m;
static std::mutex _tasks_m;
static std::vector<pthread_t> _tasks;
static std::vector<task_handle_t*> _tasks;
static bool _stop_tasks = false;
void task_sleep_ms(uint32_t ms)
@@ -56,7 +56,7 @@ static void stop_tasks()
_stop_tasks = true;
}
static bool next_task_to_stop(pthread_t& task)
static bool next_task_to_stop(task_handle_t*& task)
{
std::lock_guard lock(_tasks_m);
@@ -75,9 +75,10 @@ void task_shutdown_all()
stop_tasks();
_stop_cv.notify_all();
pthread_t task;
task_handle_t* task = nullptr;
while (next_task_to_stop(task)) {
pthread_join(task, nullptr);
task->_thread_handle->join();
task->_thread_handle = {};
}
timer_queue::destroy();
@@ -89,7 +90,7 @@ struct run_context {
std::string name;
};
static void* _task_stub(void* p)
static void* _task_stub(run_context* p)
{
std::unique_ptr<run_context> ctx{(run_context*)p};
auto name = ctx->name.c_str();
@@ -109,9 +110,8 @@ void task_create(task_handle_t* h, task_func_t func, const char* name,
h->_stack_size = stack_size;
run_context* ctx = new run_context{func, name};
if (pthread_create(&h->_thread_handle, nullptr, _task_stub, (void*)ctx) == 0) {
_tasks.emplace_back(h->_thread_handle);
}
h->_thread_handle = std::make_unique<std::thread>([=]() { _task_stub(ctx); });
if (h->_thread_handle) _tasks.emplace_back(h);
}
bool task_running()
@@ -132,22 +132,26 @@ unsigned task_get_stack_size(task_handle_t* h)
void mutex_create(mutex_handle_t* h)
{
pthread_mutex_init(h, nullptr);
(void)h;
}
bool mutex_lock(mutex_handle_t* h)
{
return pthread_mutex_lock(h) == 0;
if (!h) return false;
h->lock();
return true;
}
void mutex_unlock(mutex_handle_t* h)
{
pthread_mutex_unlock(h);
if (!h) return;
h->unlock();
}
bool mutex_trylock(mutex_handle_t* h)
{
return pthread_mutex_trylock(h) == 0;
if (!h) return false;
return h->try_lock();
}
@@ -21,19 +21,22 @@
#pragma once
#include "time.h"
#include "time_native.h"
#include <pthread.h>
#include <semaphore.h>
#include <thread>
#include <mutex>
#include <memory>
#define TASK_DEFINE_STACK(name, size) void* name
using _thread_ptr_t = std::unique_ptr<std::thread>;
struct task_handle_t {
pthread_t _thread_handle;
uint32_t _stack_size;
_thread_ptr_t _thread_handle;
uint32_t _stack_size;
};
typedef pthread_mutex_t mutex_handle_t;
typedef std::mutex mutex_handle_t;
bool task_running();
+1 -1
View File
@@ -24,7 +24,7 @@
#include <stdint.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#if defined(NATIVE_THREADS)
#include "time_native.h"
#elif defined(FREE_RTOS)
#include "time_freertos.h"
+2 -3
View File
@@ -21,12 +21,11 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#include "timer_pthread.h"
#if defined(NATIVE_THREADS)
#include "timer_native.h"
#elif defined(FREE_RTOS)
#include "timer_freertos.h"
#endif
@@ -1,6 +1,6 @@
#include "timer.h"
#include "debug.h"
#include "timer_pthread_impl.h"
#include "timer_native_impl.h"
#include <algorithm>
@@ -1,4 +1,4 @@
#include "timer_pthread.h"
#include "timer_native.h"
#include <condition_variable>
#include <deque>