eRPC API Reference  Rev. 1.7.4
NXP Semiconductors
erpc_threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef __embedded_rpc__thread__
11 #define __embedded_rpc__thread__
12 
13 #include "erpc_config_internal.h"
14 #include <stdint.h>
15 
16 // Exclude the rest of the file if threading is disabled.
17 #if ERPC_THREADS
18 
19 #if ERPC_THREADS_IS(PTHREADS)
20 #include <pthread.h>
21 #elif ERPC_THREADS_IS(FREERTOS)
22 #include "FreeRTOS.h"
23 #include "semphr.h"
24 #include "task.h"
25 #elif ERPC_THREADS_IS(ZEPHYR)
26 #include "kernel.h"
27 #elif ERPC_THREADS_IS(MBED)
28 #if MBED_CONF_RTOS_PRESENT
29 #include "rtos.h"
30 #else
31 #warning mbed-rpc: Threading is enabled but Mbed RTOS is not present!
32 #endif
33 #endif // ERPC_THREADS_IS
34 
41 // Types
44 
48 typedef void (*thread_entry_t)(void *arg);
49 
51 // Declarations
53 
54 #if defined(__cplusplus)
55 
56 namespace erpc {
62 class Thread
63 {
64 public:
66  typedef void *thread_id_t;
67 
76  Thread(const char *name = 0);
77 
88  Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0);
89 
93  virtual ~Thread(void);
94 
100  void setName(const char *name) { m_name = name; }
101 
107  const char *getName(void) const { return m_name; }
108 
116  void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0);
117 
123  void start(void *arg = 0);
124 
130  static void sleep(uint32_t usecs);
131 
137  thread_id_t getThreadId(void) const
138  {
139 #if ERPC_THREADS_IS(PTHREADS)
140  return reinterpret_cast<thread_id_t>(m_thread);
141 #elif ERPC_THREADS_IS(FREERTOS)
142  return reinterpret_cast<thread_id_t>(m_task);
143 #elif ERPC_THREADS_IS(ZEPHYR)
144  return reinterpret_cast<thread_id_t>(m_thread);
145 #elif ERPC_THREADS_IS(MBED)
146  return reinterpret_cast<thread_id_t>(m_thread->get_id());
147 #endif
148  }
149 
155  static thread_id_t getCurrentThreadId(void)
156  {
157 #if ERPC_THREADS_IS(PTHREADS)
158  return reinterpret_cast<thread_id_t>(pthread_self());
159 #elif ERPC_THREADS_IS(FREERTOS)
160  return reinterpret_cast<thread_id_t>(xTaskGetCurrentTaskHandle());
161 #elif ERPC_THREADS_IS(ZEPHYR)
162  return reinterpret_cast<thread_id_t>(k_current_get());
163 #elif ERPC_THREADS_IS(MBED)
164  return reinterpret_cast<thread_id_t>(rtos::ThisThread::get_id());
165 #endif
166  }
167 
168 #if ERPC_THREADS_IS(ZEPHYR)
169 
174  void setStackPointer(k_thread_stack_t *stack) { m_stack = stack; }
175 #endif
176 
182  static Thread *getCurrentThread(void);
183 
192  bool operator==(Thread &o);
193 
194 protected:
198  virtual void threadEntryPoint(void);
199 
200 private:
201  const char *m_name;
202  thread_entry_t m_entry;
203  void *m_arg;
204  uint32_t m_stackSize;
205  uint32_t m_priority;
206 #if ERPC_THREADS_IS(PTHREADS)
207  static pthread_key_t s_threadObjectKey;
208  pthread_t m_thread;
209 #elif ERPC_THREADS_IS(FREERTOS)
210  TaskHandle_t m_task;
211  Thread *m_next;
212  static Thread *s_first;
213 #elif ERPC_THREADS_IS(ZEPHYR)
214  struct k_thread m_thread;
215  k_thread_stack_t *m_stack;
216 #elif ERPC_THREADS_IS(MBED)
217  rtos::Thread *m_thread;
218  Thread *m_next;
219  static Thread *s_first;
220 #endif
221 
222 #if ERPC_THREADS_IS(PTHREADS)
223 
229  static void *threadEntryPointStub(void *arg);
230 #elif ERPC_THREADS_IS(FREERTOS)
231 
237  static void threadEntryPointStub(void *arg);
238 #elif ERPC_THREADS_IS(ZEPHYR)
239 
247  static void *threadEntryPointStub(void *arg1, void *arg2, void *arg3);
248 
249 #elif ERPC_THREADS_IS(MBED)
250 
256  static void threadEntryPointStub(void *arg);
257 
258 #endif
259 
260 private:
266  Thread(const Thread &o);
267 
273  Thread &operator=(const Thread &o);
274 };
275 
283 class Mutex
284 {
285 public:
289  class Guard
290  {
291  public:
297  Guard(Mutex &mutex)
298  : m_mutex(mutex)
299  {
300  m_mutex.lock();
301  }
305  ~Guard(void) { m_mutex.unlock(); }
306 
307  private:
308  Mutex &m_mutex;
309  };
310 
314  Mutex(void);
315 
319  ~Mutex(void);
320 
327  bool tryLock(void);
328 
335  bool lock(void);
336 
343  bool unlock(void);
344 
345 #if ERPC_THREADS_IS(PTHREADS)
346 
351  pthread_mutex_t *getPtr(void) { return &m_mutex; }
352 #endif
353 
354 private:
355 #if ERPC_THREADS_IS(PTHREADS)
356  pthread_mutex_t m_mutex;
357 #elif ERPC_THREADS_IS(FREERTOS)
358  SemaphoreHandle_t m_mutex;
359 #elif ERPC_THREADS_IS(ZEPHYR)
360  struct k_mutex m_mutex;
361 #elif ERPC_THREADS_IS(MBED)
362  rtos::Mutex *m_mutex;
363 #endif
364 
365 private:
371  Mutex(const Mutex &o);
377  Mutex &operator=(const Mutex &o);
378 };
379 
386 {
387 public:
391  static const uint32_t kWaitForever = 0xffffffff;
392 
398  Semaphore(int count = 0);
399 
403  ~Semaphore(void);
404 
408  void put(void);
409 
410 #if ERPC_THREADS_IS(FREERTOS)
411 
414  void putFromISR(void);
415 #endif // ERPC_HAS_FREERTOS
416 
425  bool get(uint32_t timeout = kWaitForever);
426 
432  int getCount(void) const;
433 
434 private:
435 #if ERPC_THREADS_IS(PTHREADS)
436  int m_count;
437  pthread_cond_t m_cond;
439  Mutex m_mutex;
440 #elif ERPC_THREADS_IS(FREERTOS)
441  SemaphoreHandle_t m_sem;
442 #elif ERPC_THREADS_IS(ZEPHYR)
443  struct k_sem m_sem;
444 #elif ERPC_THREADS_IS(MBED)
445  rtos::Semaphore *m_sem;
446  int m_count;
447 #endif
448 
449 private:
455  Semaphore(const Semaphore &o);
461  Semaphore &operator=(const Semaphore &o);
462 };
463 
464 } // namespace erpc
465 
466 #endif // defined(__cplusplus)
467 
470 #endif // ERPC_THREADS
471 
472 #endif // defined(__embedded_rpc__thread__)
473 // EOF
bool operator==(Thread &o)
Compare operator compares two threads.
Definition: erpc_threading_pthreads.cpp:72
void init(thread_entry_t entry, uint32_t priority=0, uint32_t stackSize=0)
This function initializes thread.
Definition: erpc_threading_pthreads.cpp:52
void(* thread_entry_t)(void *arg)
Thread function type.
Definition: erpc_threading.h:48
static thread_id_t getCurrentThreadId(void)
This function returns thread id where function is called.
Definition: erpc_threading.h:155
Simple thread class.
Definition: erpc_threading.h:62
~Guard(void)
Destructor.
Definition: erpc_threading.h:305
static void sleep(uint32_t usecs)
This function puts thread to sleep.
Definition: erpc_threading_pthreads.cpp:83
void setName(const char *name)
This function sets name for thread.
Definition: erpc_threading.h:100
const char * getName(void) const
This function returns name of thread.
Definition: erpc_threading.h:107
virtual ~Thread(void)
Destructor.
Definition: erpc_threading_pthreads.cpp:50
Definition: erpc_arbitrated_client_manager.h:25
Definition: erpc_threading.h:289
void * thread_id_t
Unique identifier for a thread.
Definition: erpc_threading.h:66
virtual void threadEntryPoint(void)
This function execute entry function.
Definition: erpc_threading_pthreads.cpp:102
static Thread * getCurrentThread(void)
This function returns Thread instance where functions is called.
Definition: erpc_threading_pthreads.cpp:77
thread_id_t getThreadId(void) const
This function returns current thread id.
Definition: erpc_threading.h:137
void start(void *arg=0)
This function starts thread execution.
Definition: erpc_threading_pthreads.cpp:59
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:297
Mutex.
Definition: erpc_threading.h:283
Simple semaphore class.
Definition: erpc_threading.h:385
Thread(const char *name=0)
Default constructor for use with the init() method.
Definition: erpc_threading_pthreads.cpp:30