eRPC API Reference  Rev. 1.9.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-2020 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 "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 = 0);
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 = 0);
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);
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_thread_stack_t *m_stack;
243 #elif ERPC_THREADS_IS(MBED)
244  rtos::Thread *m_thread;
245  Thread *m_next;
246  static Thread *s_first;
247 #elif ERPC_THREADS_IS(WIN32)
248  HANDLE m_thread;
249  unsigned int m_thrdaddr;
250  Thread *m_next;
251  static Thread *s_first;
252  static CRITICAL_SECTION m_critical_section;
253  static BOOL m_critical_section_inited;
254 #elif ERPC_THREADS_IS(THREADX)
255  TX_THREAD m_thread;
256  Thread *m_next;
257  static Thread *s_first;
258 #endif
259 
260 #if ERPC_THREADS_IS(PTHREADS)
261 
267  static void *threadEntryPointStub(void *arg);
268 #elif ERPC_THREADS_IS(FREERTOS)
269 
275  static void threadEntryPointStub(void *arg);
276 #elif ERPC_THREADS_IS(ZEPHYR)
277 
285  static void *threadEntryPointStub(void *arg1, void *arg2, void *arg3);
286 
287 #elif ERPC_THREADS_IS(MBED)
288 
294  static void threadEntryPointStub(void *arg);
295 
296 #elif ERPC_THREADS_IS(WIN32)
297 
303  static unsigned WINAPI threadEntryPointStub(void *arg);
304 
305 #elif ERPC_THREADS_IS(THREADX)
306 
312  static void threadEntryPointStub(ULONG arg);
313 #endif
314 
315 private:
321  Thread(const Thread &o);
322 
328  Thread &operator=(const Thread &o);
329 };
330 
338 class Mutex
339 {
340 public:
344  class Guard
345  {
346  public:
352  Guard(Mutex &mutex)
353  : m_mutex(mutex)
354  {
355  m_mutex.lock();
356  }
360  ~Guard(void) { m_mutex.unlock(); }
361 
362  private:
363  Mutex &m_mutex;
364  };
365 
369  Mutex(void);
370 
374  ~Mutex(void);
375 
382  bool tryLock(void);
383 
390  bool lock(void);
391 
398  bool unlock(void);
399 
400 #if ERPC_THREADS_IS(PTHREADS)
401 
406  pthread_mutex_t *getPtr(void) { return &m_mutex; }
407 #endif
408 
409 private:
410 #if ERPC_THREADS_IS(PTHREADS)
411  pthread_mutex_t m_mutex;
412 #elif ERPC_THREADS_IS(FREERTOS)
413  SemaphoreHandle_t m_mutex;
414  StaticSemaphore_t m_staticQueue;
415 #elif ERPC_THREADS_IS(ZEPHYR)
416  struct k_mutex m_mutex;
417 #elif ERPC_THREADS_IS(MBED)
418  rtos::Mutex *m_mutex;
419 #elif ERPC_THREADS_IS(WIN32)
420  HANDLE m_mutex;
421 #elif ERPC_THREADS_IS(THREADX)
422  TX_MUTEX m_mutex;
423 #endif
424 
425 private:
431  Mutex(const Mutex &o);
437  Mutex &operator=(const Mutex &o);
438 };
439 
446 {
447 public:
451  static const uint32_t kWaitForever = 0xffffffff;
452 
458  Semaphore(int count = 0);
459 
463  ~Semaphore(void);
464 
468  void put(void);
469 
470 #if ERPC_THREADS_IS(FREERTOS)
471 
474  void putFromISR(void);
475 #endif // ERPC_HAS_FREERTOS
476 
485  bool get(uint32_t timeout = kWaitForever);
486 
492  int getCount(void) const;
493 
494 private:
495 #if ERPC_THREADS_IS(PTHREADS)
496  int m_count;
497  pthread_cond_t m_cond;
499  Mutex m_mutex;
500 #elif ERPC_THREADS_IS(FREERTOS)
501  SemaphoreHandle_t m_sem;
502  StaticSemaphore_t m_staticQueue;
503 #elif ERPC_THREADS_IS(ZEPHYR)
504  struct k_sem m_sem;
505 #elif ERPC_THREADS_IS(MBED)
506  rtos::Semaphore *m_sem;
507  int m_count;
508 #elif ERPC_THREADS_IS(WIN32)
509  Mutex m_mutex;
510  int m_count;
511  HANDLE m_sem;
512 #elif ERPC_THREADS_IS(THREADX)
513  TX_SEMAPHORE m_sem;
514 #endif
515 
516 private:
522  Semaphore(const Semaphore &o);
528  Semaphore &operator=(const Semaphore &o);
529 };
530 
531 } // namespace erpc
532 
533 #endif // defined(__cplusplus)
534 
537 #endif // ERPC_THREADS
538 
539 #endif // defined(__embedded_rpc__thread__)
540 // EOF
bool operator==(Thread &o)
Compare operator compares two threads.
Definition: erpc_threading_pthreads.cpp:81
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:360
static void sleep(uint32_t usecs)
This function puts thread to sleep.
Definition: erpc_threading_pthreads.cpp:93
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:58
Definition: erpc_arbitrated_client_manager.h:25
Definition: erpc_threading.h:344
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:115
static Thread * getCurrentThread(void)
This function returns Thread instance where functions is called.
Definition: erpc_threading_pthreads.cpp:86
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:68
void init(thread_entry_t entry, uint32_t priority=0, uint32_t stackSize=0, thread_stack_pointer stackPtr=0)
This function initializes thread.
Definition: erpc_threading_pthreads.cpp:60
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:352
Mutex.
Definition: erpc_threading.h:338
Simple semaphore class.
Definition: erpc_threading.h:445
Thread(const char *name=0)
Default constructor for use with the init() method.
Definition: erpc_threading_pthreads.cpp:37