SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
ma_osal.h
Go to the documentation of this file.
1 #ifndef _MA_OSAL_H_
2 #define _MA_OSAL_H_
3 
4 #include <cstddef>
5 #include <cstdint>
6 #include <functional>
7 #include <map>
8 #include <string>
9 
10 #include "core/ma_common.h"
11 
12 #if __has_include(<ma_config_board.h>)
13 #include <ma_config_board.h>
14 #else
15 #error "No <ma_config_board.h>"
16 #endif
17 
18 #if MA_OSAL_PTHREAD
19 #include "osal/ma_osal_pthread.h"
20 #elif MA_OSAL_FREERTOS
21 #include "osal/ma_osal_freertos.h"
22 #else
23 #error "No OSAL defined"
24 #endif
25 
26 namespace ma {
27 
28 class Tick {
29 public:
30  static ma_tick_t current();
31  static ma_tick_t fromMicroseconds(uint32_t us);
32  static ma_tick_t fromMilliseconds(uint32_t ms);
33  static ma_tick_t fromSeconds(uint32_t sec);
34  static uint32_t toMicroseconds(ma_tick_t tick);
35  static uint32_t toMilliseconds(ma_tick_t tick);
36  static uint32_t toSeconds(ma_tick_t tick);
38 };
39 
40 class Thread {
41 public:
42  Thread(const char* name, void (*entry)(void*), void* arg = nullptr, uint32_t priority = 0, size_t stacksize = 0, ma_stack_t* stack = nullptr) noexcept;
43  virtual ~Thread() noexcept;
44  operator bool() const;
45  bool start(void* arg = nullptr);
46  bool stop();
47  bool join();
48  bool operator==(const Thread& other) const;
49 
50 public:
51  static void sleep(ma_tick_t tick);
52  static void yield();
53  static ma_thread_t* self();
54  static void enterCritical();
55  static void exitCritical();
56 
57 protected:
58  virtual void threadEntryPoint(void);
59 
60 private:
61  Thread(const Thread&) = delete;
62  Thread& operator=(const Thread&) = delete;
63 
64 private:
65 #if MA_OSAL_PTHREAD
66  static void* threadEntryPointStub(void* arg);
67 #elif MA_OSAL_FREERTOS
68  static void threadEntryPointStub(void* arg);
69 #endif
70  mutable ma_thread_t m_thread;
71  void* m_arg;
72  std::string m_name;
73  void (*m_entry)(void*);
74  uint32_t m_priority;
75  size_t m_stackSize;
76  ma_stack_t* m_stack;
77  bool m_started;
78 };
79 
80 class Mutex {
81 public:
82  Mutex(bool recursive = true) noexcept;
83  ~Mutex() noexcept;
84  operator bool() const;
85  bool tryLock(ma_tick_t timeout = Tick::waitForever);
86  bool lock() const;
87  bool unlock() const;
88  operator ma_mutex_t*() const;
89 
90 private:
91  Mutex(const Mutex&) = delete;
92  Mutex& operator=(const Mutex&) = delete;
93 
94 private:
95  mutable ma_mutex_t m_mutex;
96 };
97 
98 class Guard {
99 public:
100  explicit Guard(const Mutex& mutex) noexcept;
101  ~Guard() noexcept;
102 
103  operator bool() const;
104 
105 private:
106  const Mutex& m_mutex;
107 };
108 
109 class Semaphore {
110 public:
111  explicit Semaphore(size_t count = 0) noexcept;
112  ~Semaphore() noexcept;
113  operator bool() const;
114  bool wait(ma_tick_t timeout = Tick::waitForever);
115  uint32_t getCount() const;
116  void signal();
117 
118 private:
119  Semaphore(const Semaphore&) = delete;
120  Semaphore& operator=(const Semaphore&) = delete;
121 
122 private:
123  Mutex m_mutex;
124  ma_sem_t m_sem;
125 };
126 
127 class Event {
128 public:
129  Event() noexcept;
130  ~Event() noexcept;
131  operator bool() const;
132  bool wait(uint32_t mask, uint32_t* value, ma_tick_t timeout = Tick::waitForever, bool clear = true, bool waitAll = false);
133  void clear(uint32_t value = 0xffffffffu);
134  void set(uint32_t value);
135  uint32_t get() const;
136 
137 private:
138  Event(const Event&) = delete;
139  Event& operator=(const Event&) = delete;
140 
141 private:
142  Mutex m_mutex;
143  ma_event_t m_event;
144 };
145 
146 class MessageBox {
147 public:
148  explicit MessageBox(size_t size = 1) noexcept;
149  ~MessageBox() noexcept;
150  operator bool() const;
151 
152  bool fetch(void** msg, ma_tick_t timeout = Tick::waitForever);
153  bool post(void* msg, ma_tick_t timeout = Tick::waitForever);
154 
155 private:
156  MessageBox(const MessageBox&) = delete;
157  MessageBox& operator=(const MessageBox&) = delete;
158 
159 private:
160  Mutex m_mutex;
161  ma_mbox_t m_mbox;
162 };
163 
164 class Timer {
165 public:
166  Timer(uint32_t ms, void (*fn)(ma_timer_t*, void*), void* arg, bool oneshot = false) noexcept;
167  ~Timer() noexcept;
168  operator bool() const;
169  void set(ma_tick_t ms = Tick::waitForever);
170  void start();
171  void stop();
172 
173 private:
174  Timer(const Timer&) = delete;
175  Timer& operator=(const Timer&) = delete;
176 
177 private:
178  ma_timer_t m_timer;
179 };
180 
181 class Signal {
182 public:
183  static void install(std::vector<int> sigs, std::function<void(int sig)> callback);
184 
185 private:
186  static void sigHandler(int sig);
187 
188 private:
189  static std::map<int, std::vector<std::function<void(int sig)>>> s_callbacks;
190  static Mutex m_mutex;
191 };
192 
193 
194 } // namespace ma
195 
196 #endif // _MA_OSAL_H_
Definition: ma_osal.h:127
Event() noexcept
Definition: ma_osal.h:98
Guard(const Mutex &mutex) noexcept
~Guard() noexcept
Definition: ma_osal.h:146
MessageBox(size_t size=1) noexcept
Definition: ma_osal.h:80
bool lock() const
Mutex(bool recursive=true) noexcept
bool unlock() const
bool tryLock(ma_tick_t timeout=Tick::waitForever)
Definition: ma_osal.h:109
Semaphore(size_t count=0) noexcept
Definition: ma_osal.h:181
static void install(std::vector< int > sigs, std::function< void(int sig)> callback)
Definition: ma_osal.h:40
bool stop()
static void exitCritical()
static void yield()
bool start(void *arg=nullptr)
static void enterCritical()
virtual void threadEntryPoint(void)
static void sleep(ma_tick_t tick)
static ma_thread_t * self()
bool join()
Thread(const char *name, void(*entry)(void *), void *arg=nullptr, uint32_t priority=0, size_t stacksize=0, ma_stack_t *stack=nullptr) noexcept
Definition: ma_osal.h:28
static ma_tick_t fromSeconds(uint32_t sec)
static uint32_t toMilliseconds(ma_tick_t tick)
static uint32_t toMicroseconds(ma_tick_t tick)
static uint32_t toSeconds(ma_tick_t tick)
static ma_tick_t current()
static ma_tick_t fromMilliseconds(uint32_t ms)
static const ma_tick_t waitForever
Definition: ma_osal.h:37
static ma_tick_t fromMicroseconds(uint32_t us)
Definition: ma_osal.h:164
Timer(uint32_t ms, void(*fn)(ma_timer_t *, void *), void *arg, bool oneshot=false) noexcept
#define MA_WAIT_FOREVER
Definition: ma_osal_pthread.h:14
void * ma_stack_t
Definition: ma_osal_pthread.h:19
pthread_t ma_thread_t
Definition: ma_osal_pthread.h:17
pthread_mutex_t ma_mutex_t
Definition: ma_osal_pthread.h:18
uint64_t ma_tick_t
Definition: ma_osal_pthread.h:16
Definition: ma_cv.cpp:7
Definition: ma_osal_pthread.h:26
Definition: ma_osal_pthread.h:31
Definition: ma_osal_pthread.h:21
Definition: ma_osal_pthread.h:40