eRPC API Reference  Rev. 1.12.0
NXP Semiconductors
erpc_threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2023 NXP
4  * Copyright 2021 ACRIOS Systems s.r.o.
5  * All rights reserved.
6  *
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
9  */
10 
11 #ifndef __embedded_rpc__thread__
12 #define __embedded_rpc__thread__
13 
14 #include "erpc_config_internal.h"
15 
16 #include <stdint.h>
17 
18 // Exclude the rest of the file if threading is disabled.
19 #if !ERPC_THREADS_IS(NONE)
20 
21 #if ERPC_THREADS_IS(PTHREADS)
22 #include <pthread.h>
23 #elif ERPC_THREADS_IS(FREERTOS)
24 #include "FreeRTOS.h"
25 #include "semphr.h"
26 #include "task.h"
27 #elif ERPC_THREADS_IS(ZEPHYR)
28 #include <zephyr/kernel.h>
29 #elif ERPC_THREADS_IS(MBED)
30 #if MBED_CONF_RTOS_PRESENT
31 #include "rtos.h"
32 #else
33 #warning mbed-rpc: Threading is enabled but Mbed RTOS is not present!
34 #endif
35 #elif ERPC_THREADS_IS(WIN32)
36 #include "windows.h"
37 #elif ERPC_THREADS_IS(THREADX)
38 #include "tx_api.h"
39 
40 #endif // ERPC_THREADS
41 
48 // Types
51 
55 typedef void (*thread_entry_t)(void *arg);
56 
58 // Declarations
60 
61 #if defined(__cplusplus)
62 
63 namespace erpc {
69 class Thread
70 {
71 public:
73  typedef void *thread_id_t;
74 #if ERPC_THREADS_IS(FREERTOS)
75  typedef StackType_t *thread_stack_pointer;
76 #else
77  typedef void *thread_stack_pointer;
78 #endif
79 
88  Thread(const char *name = 0);
89 
101  Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0,
102  thread_stack_pointer stackPtr = NULL);
103 
107  virtual ~Thread(void);
108 
114  void setName(const char *name) { m_name = name; }
115 
121  const char *getName(void) const { return m_name; }
122 
131  void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, thread_stack_pointer stackPtr = NULL);
132 
138  void start(void *arg = 0);
139 
145  static void sleep(uint32_t usecs);
146 
152  thread_id_t getThreadId(void) const
153  {
154 #if ERPC_THREADS_IS(PTHREADS)
155  return reinterpret_cast<thread_id_t>(m_thread);
156 #elif ERPC_THREADS_IS(FREERTOS)
157  return reinterpret_cast<thread_id_t>(m_task);
158 #elif ERPC_THREADS_IS(ZEPHYR)
159  return reinterpret_cast<thread_id_t>(m_thread_id);
160 #elif ERPC_THREADS_IS(MBED)
161  return reinterpret_cast<thread_id_t>(m_thread->get_id());
162 #elif ERPC_THREADS_IS(WIN32)
163  return reinterpret_cast<thread_id_t>(m_thread);
164 #elif ERPC_THREADS_IS(THREADX)
165  return reinterpret_cast<thread_id_t>(m_thread.tx_thread_id);
166 #endif
167  }
168 
174  static thread_id_t getCurrentThreadId(void)
175  {
176 #if ERPC_THREADS_IS(PTHREADS)
177  return reinterpret_cast<thread_id_t>(pthread_self());
178 #elif ERPC_THREADS_IS(FREERTOS)
179  return reinterpret_cast<thread_id_t>(xTaskGetCurrentTaskHandle());
180 #elif ERPC_THREADS_IS(ZEPHYR)
181  return reinterpret_cast<thread_id_t>(k_current_get());
182 #elif ERPC_THREADS_IS(MBED)
183  return reinterpret_cast<thread_id_t>(rtos::ThisThread::get_id());
184 #elif ERPC_THREADS_IS(WIN32)
185  return reinterpret_cast<thread_id_t>(GetCurrentThread());
186 #elif ERPC_THREADS_IS(THREADX)
187  return reinterpret_cast<thread_id_t>(tx_thread_identify());
188 #endif
189  }
190 
191 #if ERPC_THREADS_IS(ZEPHYR)
192 
197  void setStackPointer(k_thread_stack_t *stack) { m_stack = stack; }
198 #endif
199 
205  static Thread *getCurrentThread(void);
206 
215  bool operator==(Thread &o);
216 
217 protected:
221  virtual void threadEntryPoint(void);
222 
223 private:
224  const char *m_name;
225  thread_entry_t m_entry;
226  void *m_arg;
227  uint32_t m_stackSize;
228  uint32_t m_priority;
229  thread_stack_pointer m_stackPtr;
230 #if ERPC_THREADS_IS(PTHREADS)
231  static pthread_key_t s_threadObjectKey;
232  pthread_t m_thread;
233 #elif ERPC_THREADS_IS(FREERTOS)
234  TaskHandle_t m_task;
235  Thread *m_next;
236  static Thread *s_first;
237 #if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
238  StaticTask_t m_staticTask;
239 #endif
240 #elif ERPC_THREADS_IS(ZEPHYR)
241  struct k_thread m_thread;
242  k_tid_t m_thread_id;
243  k_thread_stack_t *m_stack;
244 #elif ERPC_THREADS_IS(MBED)
245  rtos::Thread *m_thread;
246  Thread *m_next;
247  static Thread *s_first;
248 #elif ERPC_THREADS_IS(WIN32)
249  HANDLE m_thread;
250  unsigned int m_thrdaddr;
251  Thread *m_next;
252  static Thread *s_first;
253  static CRITICAL_SECTION m_critical_section;
254  static BOOL m_critical_section_inited;
255 #elif ERPC_THREADS_IS(THREADX)
256  TX_THREAD m_thread;
257  Thread *m_next;
258  static Thread *s_first;
259 #endif
260 
261 #if ERPC_THREADS_IS(PTHREADS)
262 
268  static void *threadEntryPointStub(void *arg);
269 #elif ERPC_THREADS_IS(FREERTOS)
270 
276  static void threadEntryPointStub(void *arg);
277 #elif ERPC_THREADS_IS(ZEPHYR)
278 
286  static void threadEntryPointStub(void *arg1, void *arg2, void *arg3);
287 
288 #elif ERPC_THREADS_IS(MBED)
289 
295  static void threadEntryPointStub(void *arg);
296 
297 #elif ERPC_THREADS_IS(WIN32)
298 
304  static unsigned WINAPI threadEntryPointStub(void *arg);
305 
306 #elif ERPC_THREADS_IS(THREADX)
307 
313  static void threadEntryPointStub(ULONG arg);
314 #endif
315 
316 private:
322  Thread(const Thread &o);
323 
329  Thread &operator=(const Thread &o);
330 };
331 
339 class Mutex
340 {
341 public:
345  class Guard
346  {
347  public:
353  Guard(Mutex &mutex) : m_mutex(mutex) { (void)m_mutex.lock(); }
357  ~Guard(void) { (void)m_mutex.unlock(); }
358 
359  private:
360  Mutex &m_mutex;
361  };
362 
366  Mutex(void);
367 
371  ~Mutex(void);
372 
379  bool tryLock(void);
380 
387  bool lock(void);
388 
395  bool unlock(void);
396 
397 #if ERPC_THREADS_IS(PTHREADS)
398 
403  pthread_mutex_t *getPtr(void) { return &m_mutex; }
404 #endif
405 
406 private:
407 #if ERPC_THREADS_IS(PTHREADS)
408  pthread_mutex_t m_mutex;
409 #elif ERPC_THREADS_IS(FREERTOS)
410  SemaphoreHandle_t m_mutex;
411  StaticSemaphore_t m_staticQueue;
412 #elif ERPC_THREADS_IS(ZEPHYR)
413  struct k_mutex m_mutex;
414 #elif ERPC_THREADS_IS(MBED)
415  rtos::Mutex *m_mutex;
416 #elif ERPC_THREADS_IS(WIN32)
417  HANDLE m_mutex;
418 #elif ERPC_THREADS_IS(THREADX)
419  TX_MUTEX m_mutex;
420 #endif
421 
422 private:
428  Mutex(const Mutex &o);
434  Mutex &operator=(const Mutex &o);
435 };
436 
443 {
444 public:
448  static const uint32_t kWaitForever = 0xffffffffu;
449 
455  Semaphore(int count = 0);
456 
460  ~Semaphore(void);
461 
465  void put(void);
466 
467 #if ERPC_THREADS_IS(FREERTOS)
468 
471  void putFromISR(void);
472 #endif // ERPC_HAS_FREERTOS
473 
482  bool get(uint32_t timeoutUsecs = kWaitForever);
483 
489  int getCount(void) const;
490 
491 private:
492 #if ERPC_THREADS_IS(PTHREADS)
493  int m_count;
494  pthread_cond_t m_cond;
496  Mutex m_mutex;
497 #elif ERPC_THREADS_IS(FREERTOS)
498  SemaphoreHandle_t m_sem;
499  StaticSemaphore_t m_staticQueue;
500 #elif ERPC_THREADS_IS(ZEPHYR)
501  struct k_sem m_sem;
502 #elif ERPC_THREADS_IS(MBED)
503  rtos::Semaphore *m_sem;
504  int m_count;
505 #elif ERPC_THREADS_IS(WIN32)
506  Mutex m_mutex;
507  int m_count;
508  HANDLE m_sem;
509 #elif ERPC_THREADS_IS(THREADX)
510  TX_SEMAPHORE m_sem;
511 #endif
512 
513 private:
519  Semaphore(const Semaphore &o);
525  Semaphore &operator=(const Semaphore &o);
526 };
527 
528 } // namespace erpc
529 
530 #endif // defined(__cplusplus)
531 
534 #endif // ERPC_THREADS
535 
536 #endif // defined(__embedded_rpc__thread__)
537 // EOF
void init(thread_entry_t entry, uint32_t priority=0, uint32_t stackSize=0, thread_stack_pointer stackPtr=NULL)
This function initializes thread.
Definition: erpc_threading_pthreads.cpp:48
bool operator==(Thread &o)
Compare operator compares two threads.
Definition: erpc_threading_pthreads.cpp:69
void(* thread_entry_t)(void *arg)
Thread function type.
Definition: erpc_threading.h:55
static thread_id_t getCurrentThreadId(void)
This function returns thread id where function is called.
Definition: erpc_threading.h:174
Simple thread class.
Definition: erpc_threading.h:69
~Guard(void)
Destructor.
Definition: erpc_threading.h:357
static void sleep(uint32_t usecs)
This function puts thread to sleep.
Definition: erpc_threading_pthreads.cpp:81
void setName(const char *name)
This function sets name for thread.
Definition: erpc_threading.h:114
const char * getName(void) const
This function returns name of thread.
Definition: erpc_threading.h:121
virtual ~Thread(void)
Destructor.
Definition: erpc_threading_pthreads.cpp:46
Definition: erpc_arbitrated_client_manager.hpp:25
Definition: erpc_threading.h:345
void * thread_id_t
Unique identifier for a thread.
Definition: erpc_threading.h:73
virtual void threadEntryPoint(void)
This function execute entry function.
Definition: erpc_threading_pthreads.cpp:103
static Thread * getCurrentThread(void)
This function returns Thread instance where functions is called.
Definition: erpc_threading_pthreads.cpp:74
thread_id_t getThreadId(void) const
This function returns current thread id.
Definition: erpc_threading.h:152
void start(void *arg=0)
This function starts thread execution.
Definition: erpc_threading_pthreads.cpp:56
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:353
Mutex.
Definition: erpc_threading.h:339
Simple semaphore class.
Definition: erpc_threading.h:442
Thread(const char *name=0)
Default constructor for use with the init() method.
Definition: erpc_threading_pthreads.cpp:37