2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2020-08-03 00:24:26 -03:00
|
|
|
#include <AP_HAL_SITL/I2CDevice.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
#include "Scheduler.h"
|
2016-01-10 02:17:32 -04:00
|
|
|
#include "UARTDriver.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include <sys/time.h>
|
2014-11-15 20:04:55 -04:00
|
|
|
#include <fenv.h>
|
2019-07-08 05:02:18 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2020-05-13 14:07:44 -03:00
|
|
|
#if defined (__clang__) || (defined (__APPLE__) && defined (__MACH__))
|
2018-08-24 21:42:50 -03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#else
|
2018-08-19 23:39:05 -03:00
|
|
|
#include <malloc.h>
|
2018-08-24 21:42:50 -03:00
|
|
|
#endif
|
2019-08-28 19:39:39 -03:00
|
|
|
#include <AP_RCProtocol/AP_RCProtocol.h>
|
2022-07-11 00:30:34 -03:00
|
|
|
#ifdef UBSAN_ENABLED
|
2024-01-19 18:34:16 -04:00
|
|
|
#include <fcntl.h>
|
2022-07-11 00:30:34 -03:00
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
#endif
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-01-14 23:50:58 -04:00
|
|
|
#ifndef SITL_STACK_CHECKING_ENABLED
|
|
|
|
//#define SITL_STACK_CHECKING_ENABLED !defined(__CYGWIN__) && !defined(__CYGWIN64__)
|
|
|
|
// stack checking is disabled until the memory corruption issues are
|
|
|
|
// fixed with pthread_attr_setstack. These may be due to
|
|
|
|
// changes in the way guard pages are handled.
|
|
|
|
#define SITL_STACK_CHECKING_ENABLED 0
|
|
|
|
#endif
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
AP_HAL::Proc Scheduler::_failsafe = nullptr;
|
2013-04-17 08:33:50 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
AP_HAL::MemberProc Scheduler::_timer_proc[SITL_SCHEDULER_MAX_TIMER_PROCS] = {nullptr};
|
2016-01-10 02:23:32 -04:00
|
|
|
uint8_t Scheduler::_num_timer_procs = 0;
|
|
|
|
bool Scheduler::_in_timer_proc = false;
|
2013-04-17 08:33:50 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
AP_HAL::MemberProc Scheduler::_io_proc[SITL_SCHEDULER_MAX_TIMER_PROCS] = {nullptr};
|
2016-01-10 02:23:32 -04:00
|
|
|
uint8_t Scheduler::_num_io_procs = 0;
|
|
|
|
bool Scheduler::_in_io_proc = false;
|
2019-09-22 21:39:42 -03:00
|
|
|
bool Scheduler::_should_exit = false;
|
2013-04-17 08:33:50 -03:00
|
|
|
|
2019-09-02 03:00:16 -03:00
|
|
|
bool Scheduler::_in_semaphore_take_wait = false;
|
|
|
|
|
2018-08-19 23:39:05 -03:00
|
|
|
Scheduler::thread_attr *Scheduler::threads;
|
|
|
|
HAL_Semaphore Scheduler::_thread_sem;
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
Scheduler::Scheduler(SITL_State *sitlState) :
|
2015-03-21 11:27:25 -03:00
|
|
|
_sitlState(sitlState),
|
2015-11-11 09:43:18 -04:00
|
|
|
_stopped_clock_usec(0)
|
2015-03-21 11:27:25 -03:00
|
|
|
{
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2022-07-11 00:30:34 -03:00
|
|
|
#ifdef UBSAN_ENABLED
|
|
|
|
/*
|
|
|
|
catch ubsan errors and append to a log file
|
|
|
|
*/
|
|
|
|
extern "C" {
|
|
|
|
void __ubsan_get_current_report_data(const char **OutIssueKind,
|
|
|
|
const char **OutMessage,
|
|
|
|
const char **OutFilename, unsigned *OutLine,
|
|
|
|
unsigned *OutCol, char **OutMemoryAddr);
|
|
|
|
|
2024-01-19 18:34:16 -04:00
|
|
|
void __ubsan_on_report();
|
2022-07-11 00:30:34 -03:00
|
|
|
void __ubsan_on_report()
|
|
|
|
{
|
|
|
|
static int fd = -1;
|
|
|
|
if (fd == -1) {
|
|
|
|
const char *ubsan_log_path = getenv("UBSAN_LOG_PATH");
|
|
|
|
if (ubsan_log_path == nullptr) {
|
|
|
|
ubsan_log_path = "ubsan.log";
|
|
|
|
}
|
|
|
|
if (ubsan_log_path != nullptr) {
|
|
|
|
fd = open(ubsan_log_path, O_APPEND|O_CREAT|O_WRONLY, 0644);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd != -1) {
|
|
|
|
const char *OutIssueKind = nullptr;
|
|
|
|
const char *OutMessage = nullptr;
|
|
|
|
const char *OutFilename = nullptr;
|
|
|
|
unsigned OutLine=0;
|
|
|
|
unsigned OutCol=0;
|
|
|
|
char *OutMemoryAddr=nullptr;
|
|
|
|
__ubsan_get_current_report_data(&OutIssueKind, &OutMessage, &OutFilename,
|
|
|
|
&OutLine, &OutCol, &OutMemoryAddr);
|
|
|
|
dprintf(fd, "ubsan error: %s:%u:%u %s:%s\n",
|
|
|
|
OutFilename, OutLine, OutCol,
|
|
|
|
OutIssueKind, OutMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::init()
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2018-07-06 23:42:00 -03:00
|
|
|
_main_ctx = pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scheduler::in_main_thread() const
|
|
|
|
{
|
|
|
|
if (!_in_timer_proc && !_in_io_proc && pthread_self() == _main_ctx) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2019-09-02 03:00:16 -03:00
|
|
|
/*
|
|
|
|
* semaphore_wait_hack_required - possibly move time input step
|
|
|
|
* forward even if we are currently pretending to be the IO or timer
|
|
|
|
* threads.
|
|
|
|
*
|
|
|
|
* Without this, if another thread has taken a semaphore (e.g. the
|
|
|
|
* Object Avoidance thread), and an "IO process" tries to take that
|
|
|
|
* semaphore with a timeout specified, then we end up not advancing
|
|
|
|
* time (due to the logic in SITL_State::wait_clock) and thus taking
|
|
|
|
* the semaphore never times out - meaning we essentially deadlock.
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
bool Scheduler::semaphore_wait_hack_required() const
|
2019-09-02 03:00:16 -03:00
|
|
|
{
|
|
|
|
if (pthread_self() != _main_ctx) {
|
|
|
|
// only the main thread ever moves stuff forwards
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _in_semaphore_take_wait;
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::delay_microseconds(uint16_t usec)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2024-02-22 20:33:46 -04:00
|
|
|
if (_sitlState->_sitl == nullptr) {
|
|
|
|
// this allows examples to run
|
|
|
|
hal.scheduler->stop_clock(AP_HAL::micros64()+usec);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t start = AP_HAL::micros64();
|
2016-12-02 06:16:57 -04:00
|
|
|
do {
|
|
|
|
uint64_t dtime = AP_HAL::micros64() - start;
|
|
|
|
if (dtime >= usec) {
|
|
|
|
break;
|
|
|
|
}
|
2017-01-09 08:33:52 -04:00
|
|
|
_sitlState->wait_clock(start + usec);
|
2016-12-02 06:16:57 -04:00
|
|
|
} while (true);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::delay(uint16_t ms)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2018-07-12 02:03:27 -03:00
|
|
|
uint32_t start = AP_HAL::millis();
|
|
|
|
uint32_t now = start;
|
|
|
|
do {
|
2015-03-21 11:27:25 -03:00
|
|
|
delay_microseconds(1000);
|
2018-07-12 02:03:27 -03:00
|
|
|
if (_min_delay_cb_ms <= (ms - (now - start))) {
|
2018-07-06 23:42:00 -03:00
|
|
|
if (in_main_thread()) {
|
|
|
|
call_delay_cb();
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
2018-07-12 02:03:27 -03:00
|
|
|
now = AP_HAL::millis();
|
|
|
|
} while (now - start < ms);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::register_timer_process(AP_HAL::MemberProc proc)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_timer_procs < SITL_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_timer_proc[_num_timer_procs] = proc;
|
|
|
|
_num_timer_procs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::register_io_process(AP_HAL::MemberProc proc)
|
2013-04-17 08:33:50 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_io_procs; i++) {
|
|
|
|
if (_io_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_io_procs < SITL_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_io_proc[_num_io_procs] = proc;
|
|
|
|
_num_io_procs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::register_timer_failsafe(AP_HAL::Proc failsafe, uint32_t period_us)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
_failsafe = failsafe;
|
|
|
|
}
|
|
|
|
|
2020-12-23 07:28:14 -04:00
|
|
|
void Scheduler::set_system_initialized() {
|
2013-01-10 17:50:32 -04:00
|
|
|
if (_initialized) {
|
2015-11-11 09:43:18 -04:00
|
|
|
AP_HAL::panic(
|
2015-10-24 18:45:41 -03:00
|
|
|
"PANIC: scheduler system initialized called more than once");
|
2013-01-10 17:50:32 -04:00
|
|
|
}
|
2015-04-27 00:08:50 -03:00
|
|
|
int exceptions = FE_OVERFLOW | FE_DIVBYZERO;
|
|
|
|
#ifndef __i386__
|
|
|
|
// i386 with gcc doesn't work with FE_INVALID
|
|
|
|
exceptions |= FE_INVALID;
|
|
|
|
#endif
|
2020-09-12 16:04:38 -03:00
|
|
|
#if !defined(HAL_BUILD_AP_PERIPH)
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_sitlState->_sitl == nullptr || _sitlState->_sitl->float_exception) {
|
2015-04-27 00:08:50 -03:00
|
|
|
feenableexcept(exceptions);
|
2014-11-15 20:04:55 -04:00
|
|
|
} else {
|
2015-04-27 00:08:50 -03:00
|
|
|
feclearexcept(exceptions);
|
2014-11-15 20:04:55 -04:00
|
|
|
}
|
2020-09-12 16:04:38 -03:00
|
|
|
#else
|
|
|
|
feclearexcept(exceptions);
|
|
|
|
#endif
|
2013-01-10 17:50:32 -04:00
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::sitl_end_atomic() {
|
2017-01-09 08:33:52 -04:00
|
|
|
if (_nested_atomic_ctr == 0) {
|
2020-12-10 20:18:38 -04:00
|
|
|
hal.serial(0)->printf("NESTED ATOMIC ERROR\n");
|
2017-01-09 08:33:52 -04:00
|
|
|
} else {
|
2013-01-03 21:32:52 -04:00
|
|
|
_nested_atomic_ctr--;
|
2017-01-09 08:33:52 -04:00
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2013-01-03 21:32:52 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::reboot(bool hold_in_bootloader)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2022-09-13 21:18:19 -03:00
|
|
|
HAL_SITL::actually_reboot();
|
|
|
|
abort();
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
void Scheduler::_run_timer_procs()
|
2012-12-13 18:57:01 -04:00
|
|
|
{
|
|
|
|
if (_in_timer_proc) {
|
|
|
|
// the timer calls took longer than the period of the
|
|
|
|
// timer. This is bad, and may indicate a serious
|
|
|
|
// driver failure. We can't just call the drivers
|
|
|
|
// again, as we could run out of stack. So we only
|
|
|
|
// call the _failsafe call. It's job is to detect if
|
|
|
|
// the drivers or the main loop are indeed dead and to
|
|
|
|
// activate whatever failsafe it thinks may help if
|
|
|
|
// need be. We assume the failsafe code can't
|
|
|
|
// block. If it does then we will recurse and die when
|
|
|
|
// we run out of stack
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_failsafe != nullptr) {
|
2013-09-30 07:35:42 -03:00
|
|
|
_failsafe();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_timer_proc = true;
|
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
// now call the timer based drivers
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i]) {
|
|
|
|
_timer_proc[i]();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and the failsafe, if one is setup
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_failsafe != nullptr) {
|
2015-03-24 12:02:47 -03:00
|
|
|
_failsafe();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer_proc = false;
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
void Scheduler::_run_io_procs()
|
2013-04-17 08:33:50 -03:00
|
|
|
{
|
|
|
|
if (_in_io_proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_io_proc = true;
|
|
|
|
|
2018-05-13 19:47:47 -03:00
|
|
|
// now call the IO based drivers
|
|
|
|
for (int i = 0; i < _num_io_procs; i++) {
|
|
|
|
if (_io_proc[i]) {
|
|
|
|
_io_proc[i]();
|
2013-04-17 08:33:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_in_io_proc = false;
|
2016-01-10 02:17:32 -04:00
|
|
|
|
2020-12-10 20:18:38 -04:00
|
|
|
for (uint8_t i=0; i<hal.num_serial; i++) {
|
|
|
|
hal.serial(i)->_timer_tick();
|
|
|
|
}
|
2019-01-21 00:21:42 -04:00
|
|
|
hal.storage->_timer_tick();
|
2018-08-19 23:39:05 -03:00
|
|
|
|
2020-08-03 00:24:26 -03:00
|
|
|
// in lieu of a thread-per-bus:
|
|
|
|
((HALSITL::I2CDeviceManager*)(hal.i2c_mgr))->_timer_tick();
|
|
|
|
|
2020-01-14 23:50:58 -04:00
|
|
|
#if SITL_STACK_CHECKING_ENABLED
|
2018-08-19 23:39:05 -03:00
|
|
|
check_thread_stacks();
|
2020-01-14 23:50:58 -04:00
|
|
|
#endif
|
2019-08-28 19:39:39 -03:00
|
|
|
|
2024-03-07 03:52:58 -04:00
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
2019-08-28 19:39:39 -03:00
|
|
|
AP::RC().update();
|
2020-09-12 16:04:38 -03:00
|
|
|
#endif
|
2013-04-17 08:33:50 -03:00
|
|
|
}
|
|
|
|
|
2015-03-21 11:27:25 -03:00
|
|
|
/*
|
|
|
|
set simulation timestamp
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
void Scheduler::stop_clock(uint64_t time_usec)
|
2015-03-21 11:27:25 -03:00
|
|
|
{
|
2015-11-11 09:43:18 -04:00
|
|
|
_stopped_clock_usec = time_usec;
|
2024-02-22 20:59:06 -04:00
|
|
|
if (_sitlState->_sitl != nullptr && time_usec - _last_io_run > 10000) {
|
2017-10-20 21:57:05 -03:00
|
|
|
_last_io_run = time_usec;
|
2018-05-13 19:47:47 -03:00
|
|
|
_run_io_procs();
|
2017-10-20 21:57:05 -03:00
|
|
|
}
|
2015-03-21 11:27:25 -03:00
|
|
|
}
|
|
|
|
|
2018-07-06 20:30:27 -03:00
|
|
|
/*
|
|
|
|
trampoline for thread create
|
|
|
|
*/
|
|
|
|
void *Scheduler::thread_create_trampoline(void *ctx)
|
|
|
|
{
|
2018-08-19 23:39:05 -03:00
|
|
|
struct thread_attr *a = (struct thread_attr *)ctx;
|
2022-11-08 23:26:34 -04:00
|
|
|
a->thread = pthread_self();
|
2018-08-19 23:39:05 -03:00
|
|
|
a->f[0]();
|
|
|
|
|
|
|
|
WITH_SEMAPHORE(_thread_sem);
|
|
|
|
if (threads == a) {
|
|
|
|
threads = a->next;
|
|
|
|
} else {
|
|
|
|
for (struct thread_attr *p=threads; p->next; p=p->next) {
|
|
|
|
if (p->next == a) {
|
|
|
|
p->next = p->next->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(a->stack);
|
|
|
|
free(a->f);
|
|
|
|
delete a;
|
2018-07-06 20:30:27 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-19 23:39:05 -03:00
|
|
|
#ifndef PTHREAD_STACK_MIN
|
|
|
|
#define PTHREAD_STACK_MIN 16384U
|
|
|
|
#endif
|
2018-07-06 20:30:27 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
create a new thread
|
|
|
|
*/
|
|
|
|
bool Scheduler::thread_create(AP_HAL::MemberProc proc, const char *name, uint32_t stack_size, priority_base base, int8_t priority)
|
|
|
|
{
|
2018-08-19 23:39:05 -03:00
|
|
|
WITH_SEMAPHORE(_thread_sem);
|
|
|
|
|
|
|
|
// even an empty thread takes 2500 bytes on Linux, so always add 2300, giving us 200 bytes
|
|
|
|
// safety margin
|
|
|
|
stack_size += 2300;
|
|
|
|
|
2018-07-06 20:30:27 -03:00
|
|
|
pthread_t thread {};
|
2019-02-08 02:31:27 -04:00
|
|
|
const uint32_t alloc_stack = MAX(size_t(PTHREAD_STACK_MIN),stack_size);
|
|
|
|
|
2024-05-26 22:24:12 -03:00
|
|
|
struct thread_attr *a = NEW_NOTHROW struct thread_attr;
|
2018-08-19 23:39:05 -03:00
|
|
|
if (!a) {
|
2018-07-06 20:30:27 -03:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-19 23:39:05 -03:00
|
|
|
// take a copy of the MemberProc, it is freed after thread exits
|
|
|
|
a->f = (AP_HAL::MemberProc *)malloc(sizeof(proc));
|
|
|
|
if (!a->f) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2018-08-26 23:09:29 -03:00
|
|
|
if (posix_memalign(&a->stack, 4096, alloc_stack) != 0) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2018-08-19 23:39:05 -03:00
|
|
|
if (!a->stack) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
memset(a->stack, stackfill, alloc_stack);
|
|
|
|
a->stack_min = (const uint8_t *)((((uint8_t *)a->stack) + alloc_stack) - stack_size);
|
|
|
|
|
|
|
|
a->stack_size = stack_size;
|
|
|
|
a->f[0] = proc;
|
|
|
|
a->name = name;
|
2020-01-19 07:20:08 -04:00
|
|
|
|
|
|
|
if (pthread_attr_init(&a->attr) != 0) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2020-01-14 23:50:58 -04:00
|
|
|
#if SITL_STACK_CHECKING_ENABLED
|
2018-08-19 23:39:05 -03:00
|
|
|
if (pthread_attr_setstack(&a->attr, a->stack, alloc_stack) != 0) {
|
|
|
|
AP_HAL::panic("Failed to set stack of size %u for thread %s", alloc_stack, name);
|
|
|
|
}
|
2019-08-19 20:36:55 -03:00
|
|
|
#endif
|
2018-08-19 23:39:05 -03:00
|
|
|
if (pthread_create(&thread, &a->attr, thread_create_trampoline, a) != 0) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2023-12-12 04:04:09 -04:00
|
|
|
|
|
|
|
#if !defined(__APPLE__)
|
|
|
|
pthread_setname_np(thread, name);
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 23:39:05 -03:00
|
|
|
a->next = threads;
|
|
|
|
threads = a;
|
2018-07-06 20:30:27 -03:00
|
|
|
return true;
|
2018-08-19 23:39:05 -03:00
|
|
|
|
|
|
|
failed:
|
|
|
|
if (a->stack) {
|
|
|
|
free(a->stack);
|
|
|
|
}
|
|
|
|
if (a->f) {
|
|
|
|
free(a->f);
|
|
|
|
}
|
|
|
|
delete a;
|
|
|
|
return false;
|
2018-07-06 20:30:27 -03:00
|
|
|
}
|
|
|
|
|
2018-08-19 23:39:05 -03:00
|
|
|
/*
|
|
|
|
check for stack overflow
|
|
|
|
*/
|
|
|
|
void Scheduler::check_thread_stacks(void)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_thread_sem);
|
|
|
|
for (struct thread_attr *p=threads; p; p=p->next) {
|
|
|
|
const uint8_t ncheck = 8;
|
|
|
|
for (uint8_t i=0; i<ncheck; i++) {
|
|
|
|
if (p->stack_min[i] != stackfill) {
|
|
|
|
AP_HAL::panic("stack overflow in thread %s\n", p->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 23:26:34 -04:00
|
|
|
|
|
|
|
// get the name of the current thread, or nullptr if not known
|
|
|
|
const char *Scheduler::get_current_thread_name(void) const
|
|
|
|
{
|
|
|
|
const pthread_t self = pthread_self();
|
|
|
|
for (struct thread_attr *a=threads; a; a=a->next) {
|
|
|
|
if (a->thread == self) {
|
|
|
|
return a->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|