Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created July 10, 2018 21:48
Show Gist options
  • Save lucasdemarchi/22f86f7443bb65a901ca51da6d823c09 to your computer and use it in GitHub Desktop.
Save lucasdemarchi/22f86f7443bb65a901ca51da6d823c09 to your computer and use it in GitHub Desktop.
diff --git a/libraries/AP_HAL_Linux/Scheduler.cpp b/libraries/AP_HAL_Linux/Scheduler.cpp
index a8b3b9c038..320b45d050 100644
--- a/libraries/AP_HAL_Linux/Scheduler.cpp
+++ b/libraries/AP_HAL_Linux/Scheduler.cpp
@@ -354,28 +354,15 @@ void Scheduler::teardown()
_tonealarm_thread.join();
}
-/*
- trampoline for thread create
-*/
-void *Scheduler::thread_create_trampoline(void *ctx)
-{
- AP_HAL::MemberProc *t = (AP_HAL::MemberProc *)ctx;
- (*t)();
- free(t);
- return nullptr;
-}
-
/*
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)
{
- // take a copy of the MemberProc, it is freed after thread exits
- AP_HAL::MemberProc *tproc = (AP_HAL::MemberProc *)malloc(sizeof(proc));
- if (!tproc) {
+ Thread *thread = new Thread{(Thread::task_t)proc};
+ if (!thread) {
return false;
}
- *tproc = proc;
uint8_t thread_priority = APM_LINUX_IO_PRIORITY;
static const struct {
@@ -399,21 +386,12 @@ bool Scheduler::thread_create(AP_HAL::MemberProc proc, const char *name, uint32_
break;
}
}
- pthread_t thread;
- pthread_attr_t thread_attr;
- struct sched_param param;
-
- pthread_attr_init(&thread_attr);
- pthread_attr_setstacksize(&thread_attr, stack_size);
- param.sched_priority = thread_priority;
- (void)pthread_attr_setschedparam(&thread_attr, &param);
- pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
-
- if (pthread_create(&thread, &thread_attr, thread_create_trampoline, tproc) != 0) {
- free(tproc);
+ thread->set_stack_size(stack_size);
+ if (!thread->start(name, SCHED_FIFO, thread_priority)) {
+ delete thread;
return false;
}
- pthread_setname_np(thread, name);
+
return true;
}
diff --git a/libraries/AP_HAL_Linux/Scheduler.h b/libraries/AP_HAL_Linux/Scheduler.h
index 3614c36815..b8c9290144 100644
--- a/libraries/AP_HAL_Linux/Scheduler.h
+++ b/libraries/AP_HAL_Linux/Scheduler.h
@@ -102,8 +102,6 @@ private:
pthread_t _main_ctx;
Semaphore _io_semaphore;
-
- static void *thread_create_trampoline(void *ctx);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment