tinykernel
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
kernel.hpp
[詳解]
1
// tinykernel: minimal preemptive task scheduler for RP2350 (Pico 2, Cortex-M33)
2
#pragma once
3
4
#include <stdint.h>
5
#include <stddef.h>
6
7
#include "pico/runtime.h"
// PICO_RUNTIME_INIT_FUNC (static task registration)
8
10
#ifndef TK_TICKS_PER_SEC
11
#define TK_TICKS_PER_SEC 1000u
12
#endif
13
15
#ifndef TK_TIME_SLICE_MS
16
#define TK_TIME_SLICE_MS 10u
17
#endif
18
20
#ifndef TK_MAX_TASKS
21
#define TK_MAX_TASKS 32u
22
#endif
23
25
#ifndef TK_DEFAULT_STACK_SIZE
26
#define TK_DEFAULT_STACK_SIZE (4 * 1024)
27
#endif
28
29
namespace
tk
{
30
32
using
TaskFn
= void (*)(
void
* arg);
33
39
enum
Prio
: uint32_t {
40
PRIO_REALTIME
= 1,
41
PRIO_HIGH
= 5,
42
PRIO_NORMAL
= 9,
43
PRIO_LOW
= 13,
44
};
45
49
struct
TaskHandle
{
50
uint8_t
slot
{0xFF};
51
53
bool
is_valid
()
const
{
return
slot
!= 0xFF; }
54
};
55
66
TaskHandle
create
(
TaskFn
fn,
void
* arg =
nullptr
, uint32_t prio =
PRIO_NORMAL
);
67
74
void
start
();
75
81
void
yield
();
82
91
void
sleep_ms
(uint32_t ms);
92
99
void
exit
(
int
code = 0);
100
106
uint64_t
now_ms
();
107
115
bool
lock
();
116
122
void
unlock
(
bool
was_enabled);
123
132
void
preempt_disable
();
133
141
void
preempt_enable
();
142
156
class
PreemptGuard
{
157
public
:
158
PreemptGuard
() {
preempt_disable
(); }
159
~PreemptGuard
() {
preempt_enable
(); }
160
161
PreemptGuard
(
const
PreemptGuard
&) =
delete
;
162
PreemptGuard
&
operator=
(
const
PreemptGuard
&) =
delete
;
163
};
164
165
using
task_mask_t
= uint32_t;
166
167
namespace
detail
{
168
void
static_register
(
const
char
* name,
TaskFn
fn, uint32_t prio);
169
uint32_t
sched_lock
();
170
void
sched_unlock
(uint32_t save);
171
void
block_current_task_locked
();
172
void
wake_task_locked
(
TaskHandle
handle);
173
uint8_t
current_task_slot
();
174
uint8_t
find_highest_prio_waiter
(
task_mask_t
wait_mask);
175
}
// namespace detail
176
177
}
// namespace tk
178
187
#define TK_TASK(fn, prio) \
188
static void tk_register_##fn(void) { \
189
::tk::detail::static_register(#fn, fn, (prio)); \
190
} \
191
PICO_RUNTIME_INIT_FUNC(tk_register_##fn, "ZZ900");
tk::PreemptGuard::~PreemptGuard
~PreemptGuard()
定義
kernel.hpp:159
tk::PreemptGuard::PreemptGuard
PreemptGuard(const PreemptGuard &)=delete
tk::PreemptGuard::PreemptGuard
PreemptGuard()
定義
kernel.hpp:158
tk::PreemptGuard::operator=
PreemptGuard & operator=(const PreemptGuard &)=delete
tk::detail
定義
kernel.hpp:167
tk::detail::block_current_task_locked
void block_current_task_locked()
定義
scheduler.cpp:333
tk::detail::static_register
void static_register(const char *name, TaskFn fn, uint32_t prio)
定義
scheduler.cpp:188
tk::detail::sched_lock
uint32_t sched_lock()
定義
scheduler.cpp:325
tk::detail::find_highest_prio_waiter
uint8_t find_highest_prio_waiter(task_mask_t wait_mask)
定義
scheduler.cpp:355
tk::detail::current_task_slot
uint8_t current_task_slot()
定義
scheduler.cpp:349
tk::detail::sched_unlock
void sched_unlock(uint32_t save)
定義
scheduler.cpp:329
tk::detail::wake_task_locked
void wake_task_locked(TaskHandle handle)
定義
scheduler.cpp:340
tk
定義
actor.hpp:9
tk::preempt_disable
void preempt_disable()
自コアのタスクスイッチ (プリエンプション) を一時的に禁止します
定義
scheduler.cpp:369
tk::Prio
Prio
タスクの優先度レベル
定義
kernel.hpp:39
tk::PRIO_NORMAL
@ PRIO_NORMAL
通常優先度 (デフォルト)
定義
kernel.hpp:42
tk::PRIO_HIGH
@ PRIO_HIGH
高優先度
定義
kernel.hpp:41
tk::PRIO_REALTIME
@ PRIO_REALTIME
リアルタイム最優先
定義
kernel.hpp:40
tk::PRIO_LOW
@ PRIO_LOW
低優先度
定義
kernel.hpp:43
tk::now_ms
uint64_t now_ms()
システム起動時からの経過時間をミリ秒単位で取得します
定義
scheduler.cpp:312
tk::lock
bool lock()
ハードウェアスピンロックと全割り込み禁止によるクリティカルセクションに入ります
定義
scheduler.cpp:316
tk::start
void start()
tinykernel スケジューラを起動します
定義
scheduler.cpp:247
tk::create
TaskHandle create(TaskFn fn, void *arg=nullptr, uint32_t prio=PRIO_NORMAL)
タスクを生成・登録します
定義
scheduler.cpp:204
tk::preempt_enable
void preempt_enable()
自コアのタスクスイッチ (プリエンプション) を再開します
定義
scheduler.cpp:376
tk::unlock
void unlock(bool was_enabled)
tk::lock() で入ったクリティカルセクションを抜けます
定義
scheduler.cpp:321
tk::yield
void yield()
現在実行中のタスクを一時中断し、同優先度の他のタスクへ CPU を譲渡します
定義
scheduler.cpp:276
tk::TaskFn
void(*)(void *arg) TaskFn
定義
kernel.hpp:32
tk::sleep_ms
void sleep_ms(uint32_t ms)
現在のタスクを指定時間スリープさせます
定義
scheduler.cpp:284
tk::exit
void exit(int code=0)
現在のタスクを終了・破棄します
定義
scheduler.cpp:298
tk::task_mask_t
uint32_t task_mask_t
定義
kernel.hpp:165
tk::TaskHandle
タスクの識別ハンドル
定義
kernel.hpp:49
tk::TaskHandle::slot
uint8_t slot
タスクスロット番号 (0xFF は無効/失敗)
定義
kernel.hpp:50
tk::TaskHandle::is_valid
bool is_valid() const
定義
kernel.hpp:53
tinykernel
include
tinykernel
kernel.hpp
構築:
1.18.0