halx
読み取り中…
検索中…
一致する文字列を見つけられません
notifier.hpp
[詳解]
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5
6#include "common.hpp"
7#include "timeout.hpp"
8
9namespace halx::core {
10
11class Notifier {
12public:
13 void reset() {
14#if __has_include(<cmsis_os2.h>)
15 thread_id_ = osThreadGetId();
16 osThreadFlagsClear(0xFFFFFFFF);
17#else
18 flags_.store(0, std::memory_order_relaxed);
19#endif
20 }
21
22 void set(uint32_t flags) {
23#if __has_include(<cmsis_os2.h>)
24 osThreadFlagsSet(thread_id_, flags);
25#else
26 flags_.fetch_or(flags, std::memory_order_relaxed);
27#endif
28 }
29
30 uint32_t wait(uint32_t flags, uint32_t timeout) {
31#if __has_include(<cmsis_os2.h>)
32 uint32_t raised =
33 osThreadFlagsWait(flags, osFlagsWaitAny | osFlagsNoClear, timeout);
34 if ((raised & 0x80000000) != 0) {
35 return 0;
36 }
37 return raised;
38#else
39 TimeoutHelper timeout_helper{timeout};
40 uint32_t raised;
41 while ((raised = flags_.load(std::memory_order_relaxed) & flags) == 0) {
42 if (timeout_helper.is_timeout()) {
43 return 0;
44 }
45 yield();
46 }
47 return raised;
48#endif
49 }
50
51private:
52#if __has_include(<cmsis_os2.h>)
53 osThreadId_t thread_id_ = nullptr;
54#else
55 std::atomic<uint32_t> flags_{0};
56#endif
57};
58
59}; // namespace halx::core
Definition notifier.hpp:11
void set(uint32_t flags)
Definition notifier.hpp:22
uint32_t wait(uint32_t flags, uint32_t timeout)
Definition notifier.hpp:30
void reset()
Definition notifier.hpp:13
Definition timeout.hpp:9
Definition common.hpp:11
void yield()
Definition common.hpp:46