tutrcos
読み取り中…
検索中…
一致する文字列を見つけられません
thread.hpp
[詳解]
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <memory>
6#include <type_traits>
7
8#include "cmsis_os2.h"
9
10namespace tutrcos {
11namespace core {
12
13class Thread {
14private:
15 struct Deleter {
16 void operator()(osThreadId_t thread_id) { osThreadTerminate(thread_id); }
17 };
18
19 using ThreadId =
20 std::unique_ptr<std::remove_pointer_t<osThreadId_t>, Deleter>;
21
22public:
23 Thread(std::function<void()> &&func) : func_{std::move(func)} {
24 osThreadAttr_t attr = {};
25 attr.stack_size = STACK_SIZE;
26 attr.priority = PRIORITY;
27 thread_id_ = ThreadId{osThreadNew(func_internal, this, &attr)};
28 }
29
30 static inline void yield() { osThreadYield(); }
31
32 static inline void delay(uint32_t ticks) { osDelay(ticks); }
33
34 static inline void delay_until(uint32_t ticks) { osDelayUntil(ticks); }
35
36 [[noreturn]] static inline void exit() { osThreadExit(); }
37
38private:
39 static constexpr uint32_t STACK_SIZE = 4096;
40 static constexpr osPriority_t PRIORITY = osPriorityNormal;
41
42 ThreadId thread_id_;
43 std::function<void()> func_;
44
45 static inline void func_internal(void *thread) {
46 reinterpret_cast<tutrcos::core::Thread *>(thread)->func_();
47 exit();
48 }
49};
50
51} // namespace core
52} // namespace tutrcos
Definition thread.hpp:13
Thread(std::function< void()> &&func)
Definition thread.hpp:23
static void exit()
Definition thread.hpp:36
static void yield()
Definition thread.hpp:30
static void delay_until(uint32_t ticks)
Definition thread.hpp:34
static void delay(uint32_t ticks)
Definition thread.hpp:32
Definition kernel.hpp:7