eRPC API Reference  Rev. 1.7.0
NXP Semiconductors
erpc_threading.h
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef __embedded_rpc__thread__
37 #define __embedded_rpc__thread__
38 
39 #include "erpc_config_internal.h"
40 #include <stdint.h>
41 
42 // Exclude the rest of the file if threading is disabled.
43 #if ERPC_THREADS
44 
45 #if ERPC_THREADS_IS(PTHREADS)
46 #include <pthread.h>
47 #elif ERPC_THREADS_IS(FREERTOS)
48 #include "FreeRTOS.h"
49 #include "semphr.h"
50 #include "task.h"
51 #elif ERPC_THREADS_IS(ZEPHYR)
52 #include "kernel.h"
53 #endif // ERPC_THREADS_IS
54 
61 // Types
64 
68 typedef void (*thread_entry_t)(void *arg);
69 
71 // Declarations
73 
74 #if defined(__cplusplus)
75 
76 namespace erpc {
82 class Thread
83 {
84 public:
86  typedef void *thread_id_t;
87 
96  Thread(const char *name = 0);
97 
108  Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0);
109 
113  virtual ~Thread(void);
114 
120  void setName(const char *name) { m_name = name; }
121 
127  const char *getName(void) const { return m_name; }
128 
136  void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0);
137 
143  void start(void *arg = 0);
144 
150  static void sleep(uint32_t usecs);
151 
157  thread_id_t getThreadId(void) const
158  {
159 #if ERPC_THREADS_IS(PTHREADS)
160  return reinterpret_cast<thread_id_t>(m_thread);
161 #elif ERPC_THREADS_IS(FREERTOS)
162  return reinterpret_cast<thread_id_t>(m_task);
163 #elif ERPC_THREADS_IS(ZEPHYR)
164  return reinterpret_cast<thread_id_t>(m_thread);
165 #endif
166  }
167 
173  static thread_id_t getCurrentThreadId(void)
174  {
175 #if ERPC_THREADS_IS(PTHREADS)
176  return reinterpret_cast<thread_id_t>(pthread_self());
177 #elif ERPC_THREADS_IS(FREERTOS)
178  return reinterpret_cast<thread_id_t>(xTaskGetCurrentTaskHandle());
179 #elif ERPC_THREADS_IS(ZEPHYR)
180  return reinterpret_cast<thread_id_t>(k_current_get());
181 #endif
182  }
183 
184 #if ERPC_THREADS_IS(ZEPHYR)
185 
190  void setStackPointer(k_thread_stack_t *stack) { m_stack = stack; }
191 #endif
192 
198  static Thread *getCurrentThread(void);
199 
208  bool operator==(Thread &o);
209 
210 protected:
214  virtual void threadEntryPoint(void);
215 
216 private:
217  const char *m_name;
218  thread_entry_t m_entry;
219  void *m_arg;
220  uint32_t m_stackSize;
221  uint32_t m_priority;
222 #if ERPC_THREADS_IS(PTHREADS)
223  static pthread_key_t s_threadObjectKey;
224  pthread_t m_thread;
225 #elif ERPC_THREADS_IS(FREERTOS)
226  TaskHandle_t m_task;
227  Thread *m_next;
228  static Thread *s_first;
229 #elif ERPC_THREADS_IS(ZEPHYR)
230  struct k_thread m_thread;
231  k_thread_stack_t *m_stack;
232 #endif
233 
234 #if ERPC_THREADS_IS(PTHREADS)
235 
241  static void *threadEntryPointStub(void *arg);
242 #elif ERPC_THREADS_IS(FREERTOS)
243 
249  static void threadEntryPointStub(void *arg);
250 #elif ERPC_THREADS_IS(ZEPHYR)
251 
259  static void *threadEntryPointStub(void *arg1, void *arg2, void *arg3);
260 #endif
261 
262 private:
268  Thread(const Thread &o);
269 
275  Thread &operator=(const Thread &o);
276 };
277 
285 class Mutex
286 {
287 public:
291  class Guard
292  {
293  public:
299  Guard(Mutex &mutex)
300  : m_mutex(mutex)
301  {
302  m_mutex.lock();
303  }
307  ~Guard(void) { m_mutex.unlock(); }
308 
309  private:
310  Mutex &m_mutex;
311  };
312 
316  Mutex(void);
317 
321  ~Mutex(void);
322 
329  bool tryLock(void);
330 
337  bool lock(void);
338 
345  bool unlock(void);
346 
347 #if ERPC_THREADS_IS(PTHREADS)
348 
353  pthread_mutex_t *getPtr(void) { return &m_mutex; }
354 #endif
355 
356 private:
357 #if ERPC_THREADS_IS(PTHREADS)
358  pthread_mutex_t m_mutex;
359 #elif ERPC_THREADS_IS(FREERTOS)
360  SemaphoreHandle_t m_mutex;
361 #elif ERPC_THREADS_IS(ZEPHYR)
362  struct k_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 #endif
445 
446 private:
452  Semaphore(const Semaphore &o);
458  Semaphore &operator=(const Semaphore &o);
459 };
460 
461 } // namespace erpc
462 
463 #endif // defined(__cplusplus)
464 
467 #endif // ERPC_THREADS
468 
469 #endif // defined(__embedded_rpc__thread__)
470 // EOF
bool operator==(Thread &o)
Compare operator compares two threads.
Definition: erpc_threading_pthreads.cpp:98
void init(thread_entry_t entry, uint32_t priority=0, uint32_t stackSize=0)
This function initializes thread.
Definition: erpc_threading_pthreads.cpp:78
void(* thread_entry_t)(void *arg)
Thread function type.
Definition: erpc_threading.h:68
static thread_id_t getCurrentThreadId(void)
This function returns thread id where function is called.
Definition: erpc_threading.h:173
Simple thread class.
Definition: erpc_threading.h:82
~Guard(void)
Destructor.
Definition: erpc_threading.h:307
static void sleep(uint32_t usecs)
This function puts thread to sleep.
Definition: erpc_threading_pthreads.cpp:109
void setName(const char *name)
This function sets name for thread.
Definition: erpc_threading.h:120
const char * getName(void) const
This function returns name of thread.
Definition: erpc_threading.h:127
virtual ~Thread(void)
Destructor.
Definition: erpc_threading_pthreads.cpp:76
Definition: erpc_arbitrated_client_manager.h:51
Definition: erpc_threading.h:291
void * thread_id_t
Unique identifier for a thread.
Definition: erpc_threading.h:86
virtual void threadEntryPoint(void)
This function execute entry function.
Definition: erpc_threading_pthreads.cpp:128
static Thread * getCurrentThread(void)
This function returns Thread instance where functions is called.
Definition: erpc_threading_pthreads.cpp:103
thread_id_t getThreadId(void) const
This function returns current thread id.
Definition: erpc_threading.h:157
void start(void *arg=0)
This function starts thread execution.
Definition: erpc_threading_pthreads.cpp:85
Guard(Mutex &mutex)
Constructor.
Definition: erpc_threading.h:299
Mutex.
Definition: erpc_threading.h:285
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:56