eRPC API Reference  Rev. 1.7.2
NXP Semiconductors
erpc_threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 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 #endif // ERPC_THREADS_IS
28 
35 // Types
38 
42 typedef void (*thread_entry_t)(void *arg);
43 
45 // Declarations
47 
48 #if defined(__cplusplus)
49 
50 namespace erpc {
56 class Thread
57 {
58 public:
60  typedef void *thread_id_t;
61 
70  Thread(const char *name = 0);
71 
82  Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0);
83 
87  virtual ~Thread(void);
88 
94  void setName(const char *name) { m_name = name; }
95 
101  const char *getName(void) const { return m_name; }
102 
110  void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0);
111 
117  void start(void *arg = 0);
118 
124  static void sleep(uint32_t usecs);
125 
131  thread_id_t getThreadId(void) const
132  {
133 #if ERPC_THREADS_IS(PTHREADS)
134  return reinterpret_cast<thread_id_t>(m_thread);
135 #elif ERPC_THREADS_IS(FREERTOS)
136  return reinterpret_cast<thread_id_t>(m_task);
137 #elif ERPC_THREADS_IS(ZEPHYR)
138  return reinterpret_cast<thread_id_t>(m_thread);
139 #endif
140  }
141 
147  static thread_id_t getCurrentThreadId(void)
148  {
149 #if ERPC_THREADS_IS(PTHREADS)
150  return reinterpret_cast<thread_id_t>(pthread_self());
151 #elif ERPC_THREADS_IS(FREERTOS)
152  return reinterpret_cast<thread_id_t>(xTaskGetCurrentTaskHandle());
153 #elif ERPC_THREADS_IS(ZEPHYR)
154  return reinterpret_cast<thread_id_t>(k_current_get());
155 #endif
156  }
157 
158 #if ERPC_THREADS_IS(ZEPHYR)
159 
164  void setStackPointer(k_thread_stack_t *stack) { m_stack = stack; }
165 #endif
166 
172  static Thread *getCurrentThread(void);
173 
182  bool operator==(Thread &o);
183 
184 protected:
188  virtual void threadEntryPoint(void);
189 
190 private:
191  const char *m_name;
192  thread_entry_t m_entry;
193  void *m_arg;
194  uint32_t m_stackSize;
195  uint32_t m_priority;
196 #if ERPC_THREADS_IS(PTHREADS)
197  static pthread_key_t s_threadObjectKey;
198  pthread_t m_thread;
199 #elif ERPC_THREADS_IS(FREERTOS)
200  TaskHandle_t m_task;
201  Thread *m_next;
202  static Thread *s_first;
203 #elif ERPC_THREADS_IS(ZEPHYR)
204  struct k_thread m_thread;
205  k_thread_stack_t *m_stack;
206 #endif
207 
208 #if ERPC_THREADS_IS(PTHREADS)
209 
215  static void *threadEntryPointStub(void *arg);
216 #elif ERPC_THREADS_IS(FREERTOS)
217 
223  static void threadEntryPointStub(void *arg);
224 #elif ERPC_THREADS_IS(ZEPHYR)
225 
233  static void *threadEntryPointStub(void *arg1, void *arg2, void *arg3);
234 #endif
235 
236 private:
242  Thread(const Thread &o);
243 
249  Thread &operator=(const Thread &o);
250 };
251 
259 class Mutex
260 {
261 public:
265  class Guard
266  {
267  public:
273  Guard(Mutex &mutex)
274  : m_mutex(mutex)
275  {
276  m_mutex.lock();
277  }
281  ~Guard(void) { m_mutex.unlock(); }
282 
283  private:
284  Mutex &m_mutex;
285  };
286 
290  Mutex(void);
291 
295  ~Mutex(void);
296 
303  bool tryLock(void);
304 
311  bool lock(void);
312 
319  bool unlock(void);
320 
321 #if ERPC_THREADS_IS(PTHREADS)
322 
327  pthread_mutex_t *getPtr(void) { return &m_mutex; }
328 #endif
329 
330 private:
331 #if ERPC_THREADS_IS(PTHREADS)
332  pthread_mutex_t m_mutex;
333 #elif ERPC_THREADS_IS(FREERTOS)
334  SemaphoreHandle_t m_mutex;
335 #elif ERPC_THREADS_IS(ZEPHYR)
336  struct k_mutex m_mutex;
337 #endif
338 
339 private:
345  Mutex(const Mutex &o);
351  Mutex &operator=(const Mutex &o);
352 };
353 
360 {
361 public:
365  static const uint32_t kWaitForever = 0xffffffff;
366 
372  Semaphore(int count = 0);
373 
377  ~Semaphore(void);
378 
382  void put(void);
383 
384 #if ERPC_THREADS_IS(FREERTOS)
385 
388  void putFromISR(void);
389 #endif // ERPC_HAS_FREERTOS
390 
399  bool get(uint32_t timeout = kWaitForever);
400 
406  int getCount(void) const;
407 
408 private:
409 #if ERPC_THREADS_IS(PTHREADS)
410  int m_count;
411  pthread_cond_t m_cond;
413  Mutex m_mutex;
414 #elif ERPC_THREADS_IS(FREERTOS)
415  SemaphoreHandle_t m_sem;
416 #elif ERPC_THREADS_IS(ZEPHYR)
417  struct k_sem m_sem;
418 #endif
419 
420 private:
426  Semaphore(const Semaphore &o);
432  Semaphore &operator=(const Semaphore &o);
433 };
434 
435 } // namespace erpc
436 
437 #endif // defined(__cplusplus)
438 
441 #endif // ERPC_THREADS
442 
443 #endif // defined(__embedded_rpc__thread__)
444 // 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:42
static thread_id_t getCurrentThreadId(void)
This function returns thread id where function is called.
Definition: erpc_threading.h:147
Simple thread class.
Definition: erpc_threading.h:56
~Guard(void)
Destructor.
Definition: erpc_threading.h:281
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:94
const char * getName(void) const
This function returns name of thread.
Definition: erpc_threading.h:101
virtual ~Thread(void)
Destructor.
Definition: erpc_threading_pthreads.cpp:50
Definition: erpc_arbitrated_client_manager.h:25
Definition: erpc_threading.h:265
void * thread_id_t
Unique identifier for a thread.
Definition: erpc_threading.h:60
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:131
void start(void *arg=0)
This function starts thread execution.
Definition: erpc_threading_pthreads.cpp:59
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:273
Mutex.
Definition: erpc_threading.h:259
Simple semaphore class.
Definition: erpc_threading.h:359
Thread(const char *name=0)
Default constructor for use with the init() method.
Definition: erpc_threading_pthreads.cpp:30