eRPC API Reference  Rev. 1.8.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  * 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 
15 #include <stdint.h>
16 
17 // Exclude the rest of the file if threading is disabled.
18 #if ERPC_THREADS
19 
20 #if ERPC_THREADS_IS(PTHREADS)
21 #include <pthread.h>
22 #elif ERPC_THREADS_IS(FREERTOS)
23 #include "FreeRTOS.h"
24 #include "semphr.h"
25 #include "task.h"
26 #elif ERPC_THREADS_IS(ZEPHYR)
27 #include "kernel.h"
28 #elif ERPC_THREADS_IS(MBED)
29 #if MBED_CONF_RTOS_PRESENT
30 #include "rtos.h"
31 #else
32 #warning mbed-rpc: Threading is enabled but Mbed RTOS is not present!
33 #endif
34 #elif ERPC_THREADS_IS(WIN32)
35 #include "windows.h"
36 
37 #endif // ERPC_THREADS
38 
45 // Types
48 
52 typedef void (*thread_entry_t)(void *arg);
53 
55 // Declarations
57 
58 #if defined(__cplusplus)
59 
60 namespace erpc {
66 class Thread
67 {
68 public:
70  typedef void *thread_id_t;
71 
80  Thread(const char *name = 0);
81 
92  Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0);
93 
97  virtual ~Thread(void);
98 
104  void setName(const char *name) { m_name = name; }
105 
111  const char *getName(void) const { return m_name; }
112 
120  void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0);
121 
127  void start(void *arg = 0);
128 
134  static void sleep(uint32_t usecs);
135 
141  thread_id_t getThreadId(void) const
142  {
143 #if ERPC_THREADS_IS(PTHREADS)
144  return reinterpret_cast<thread_id_t>(m_thread);
145 #elif ERPC_THREADS_IS(FREERTOS)
146  return reinterpret_cast<thread_id_t>(m_task);
147 #elif ERPC_THREADS_IS(ZEPHYR)
148  return reinterpret_cast<thread_id_t>(m_thread);
149 #elif ERPC_THREADS_IS(MBED)
150  return reinterpret_cast<thread_id_t>(m_thread->get_id());
151 #elif ERPC_THREADS_IS(WIN32)
152  return reinterpret_cast<thread_id_t>(m_thread);
153 #endif
154  }
155 
161  static thread_id_t getCurrentThreadId(void)
162  {
163 #if ERPC_THREADS_IS(PTHREADS)
164  return reinterpret_cast<thread_id_t>(pthread_self());
165 #elif ERPC_THREADS_IS(FREERTOS)
166  return reinterpret_cast<thread_id_t>(xTaskGetCurrentTaskHandle());
167 #elif ERPC_THREADS_IS(ZEPHYR)
168  return reinterpret_cast<thread_id_t>(k_current_get());
169 #elif ERPC_THREADS_IS(MBED)
170  return reinterpret_cast<thread_id_t>(rtos::ThisThread::get_id());
171 #elif ERPC_THREADS_IS(WIN32)
172  return reinterpret_cast<thread_id_t>(GetCurrentThread());
173 #endif
174  }
175 
176 #if ERPC_THREADS_IS(ZEPHYR)
177 
182  void setStackPointer(k_thread_stack_t *stack) { m_stack = stack; }
183 #endif
184 
190  static Thread *getCurrentThread(void);
191 
200  bool operator==(Thread &o);
201 
202 protected:
206  virtual void threadEntryPoint(void);
207 
208 private:
209  const char *m_name;
210  thread_entry_t m_entry;
211  void *m_arg;
212  uint32_t m_stackSize;
213  uint32_t m_priority;
214 #if ERPC_THREADS_IS(PTHREADS)
215  static pthread_key_t s_threadObjectKey;
216  pthread_t m_thread;
217 #elif ERPC_THREADS_IS(FREERTOS)
218  TaskHandle_t m_task;
219  Thread *m_next;
220  static Thread *s_first;
221 #elif ERPC_THREADS_IS(ZEPHYR)
222  struct k_thread m_thread;
223  k_thread_stack_t *m_stack;
224 #elif ERPC_THREADS_IS(MBED)
225  rtos::Thread *m_thread;
226  Thread *m_next;
227  static Thread *s_first;
228 #elif ERPC_THREADS_IS(WIN32)
229  HANDLE m_thread;
230  unsigned int m_thrdaddr;
231  Thread *m_next;
232  static Thread *s_first;
233  static CRITICAL_SECTION m_critical_section;
234  static BOOL m_critical_section_inited;
235 #endif
236 
237 #if ERPC_THREADS_IS(PTHREADS)
238 
244  static void *threadEntryPointStub(void *arg);
245 #elif ERPC_THREADS_IS(FREERTOS)
246 
252  static void threadEntryPointStub(void *arg);
253 #elif ERPC_THREADS_IS(ZEPHYR)
254 
262  static void *threadEntryPointStub(void *arg1, void *arg2, void *arg3);
263 
264 #elif ERPC_THREADS_IS(MBED)
265 
271  static void threadEntryPointStub(void *arg);
272 
273 #elif ERPC_THREADS_IS(WIN32)
274 
280  static unsigned WINAPI threadEntryPointStub(void *arg);
281 
282 #endif
283 
284 private:
290  Thread(const Thread &o);
291 
297  Thread &operator=(const Thread &o);
298 };
299 
307 class Mutex
308 {
309 public:
313  class Guard
314  {
315  public:
321  Guard(Mutex &mutex)
322  : m_mutex(mutex)
323  {
324  m_mutex.lock();
325  }
329  ~Guard(void) { m_mutex.unlock(); }
330 
331  private:
332  Mutex &m_mutex;
333  };
334 
338  Mutex(void);
339 
343  ~Mutex(void);
344 
351  bool tryLock(void);
352 
359  bool lock(void);
360 
367  bool unlock(void);
368 
369 #if ERPC_THREADS_IS(PTHREADS)
370 
375  pthread_mutex_t *getPtr(void) { return &m_mutex; }
376 #endif
377 
378 private:
379 #if ERPC_THREADS_IS(PTHREADS)
380  pthread_mutex_t m_mutex;
381 #elif ERPC_THREADS_IS(FREERTOS)
382  SemaphoreHandle_t m_mutex;
383 #elif ERPC_THREADS_IS(ZEPHYR)
384  struct k_mutex m_mutex;
385 #elif ERPC_THREADS_IS(MBED)
386  rtos::Mutex *m_mutex;
387 #elif ERPC_THREADS_IS(WIN32)
388  HANDLE m_mutex;
389 #endif
390 
391 private:
397  Mutex(const Mutex &o);
403  Mutex &operator=(const Mutex &o);
404 };
405 
412 {
413 public:
417  static const uint32_t kWaitForever = 0xffffffff;
418 
424  Semaphore(int count = 0);
425 
429  ~Semaphore(void);
430 
434  void put(void);
435 
436 #if ERPC_THREADS_IS(FREERTOS)
437 
440  void putFromISR(void);
441 #endif // ERPC_HAS_FREERTOS
442 
451  bool get(uint32_t timeout = kWaitForever);
452 
458  int getCount(void) const;
459 
460 private:
461 #if ERPC_THREADS_IS(PTHREADS)
462  int m_count;
463  pthread_cond_t m_cond;
465  Mutex m_mutex;
466 #elif ERPC_THREADS_IS(FREERTOS)
467  SemaphoreHandle_t m_sem;
468 #elif ERPC_THREADS_IS(ZEPHYR)
469  struct k_sem m_sem;
470 #elif ERPC_THREADS_IS(MBED)
471  rtos::Semaphore *m_sem;
472  int m_count;
473 #elif ERPC_THREADS_IS(WIN32)
474  Mutex m_mutex;
475  int m_count;
476  HANDLE m_sem;
477 #endif
478 
479 private:
485  Semaphore(const Semaphore &o);
491  Semaphore &operator=(const Semaphore &o);
492 };
493 
494 } // namespace erpc
495 
496 #endif // defined(__cplusplus)
497 
500 #endif // ERPC_THREADS
501 
502 #endif // defined(__embedded_rpc__thread__)
503 // EOF
bool operator==(Thread &o)
Compare operator compares two threads.
Definition: erpc_threading_pthreads.cpp:73
void init(thread_entry_t entry, uint32_t priority=0, uint32_t stackSize=0)
This function initializes thread.
Definition: erpc_threading_pthreads.cpp:53
void(* thread_entry_t)(void *arg)
Thread function type.
Definition: erpc_threading.h:52
static thread_id_t getCurrentThreadId(void)
This function returns thread id where function is called.
Definition: erpc_threading.h:161
Simple thread class.
Definition: erpc_threading.h:66
~Guard(void)
Destructor.
Definition: erpc_threading.h:329
static void sleep(uint32_t usecs)
This function puts thread to sleep.
Definition: erpc_threading_pthreads.cpp:84
void setName(const char *name)
This function sets name for thread.
Definition: erpc_threading.h:104
const char * getName(void) const
This function returns name of thread.
Definition: erpc_threading.h:111
virtual ~Thread(void)
Destructor.
Definition: erpc_threading_pthreads.cpp:51
Definition: erpc_arbitrated_client_manager.h:25
Definition: erpc_threading.h:313
void * thread_id_t
Unique identifier for a thread.
Definition: erpc_threading.h:70
virtual void threadEntryPoint(void)
This function execute entry function.
Definition: erpc_threading_pthreads.cpp:105
static Thread * getCurrentThread(void)
This function returns Thread instance where functions is called.
Definition: erpc_threading_pthreads.cpp:78
thread_id_t getThreadId(void) const
This function returns current thread id.
Definition: erpc_threading.h:141
void start(void *arg=0)
This function starts thread execution.
Definition: erpc_threading_pthreads.cpp:60
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:321
Mutex.
Definition: erpc_threading.h:307
Simple semaphore class.
Definition: erpc_threading.h:411
Thread(const char *name=0)
Default constructor for use with the init() method.
Definition: erpc_threading_pthreads.cpp:31