xref: /nrf52832-nimble/rt-thread/components/cplusplus/Thread.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2016/10/1      Bernard      The first version
9  */
10 
11 #pragma once
12 
13 #include <stdint.h>
14 #include <rtthread.h>
15 
16 namespace rtthread
17 {
18 
19 /** The Thread class allow defining, creating, and controlling thread functions in the system. */
20 class Thread
21 {
22 public:
23     typedef void (*thread_func_t) (void *param);
24 
25     /** Allocate a new thread without starting execution
26       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
27       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
28       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
29     */
30     Thread(rt_uint32_t stack_size = 2048,
31            rt_uint8_t  priority = (RT_THREAD_PRIORITY_MAX * 2)/3,
32            rt_uint32_t tick = 20,
33            const char *name = "th");
34 
35     Thread(void (*entry)(void *p),
36            void *p = RT_NULL,
37            rt_uint32_t stack_size = 2048,
38            rt_uint8_t  priority = (RT_THREAD_PRIORITY_MAX * 2)/3,
39            rt_uint32_t tick = 20,
40            const char *name = "th");
41 
42     virtual ~Thread();
43 
44     bool start();
45 
46     static void sleep(int32_t millisec);
47 
48     void wait(int32_t millisec);
49     void join(int32_t millisec = -1);
50 
51 protected:
52     virtual void run();
53 
54 private:
55     static void func(Thread *pThis);
56 
57 private:
58     rt_thread_t _thread;
59 
60     thread_func_t _entry;
61     void *_param;
62 
63     /* event for thread join */
64     struct rt_event _event;
65     bool started;
66 };
67 
68 }
69