1 /*
2  * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _KERNEL_OS_RTTHREAD_OS_THREAD_H_
31 #define _KERNEL_OS_RTTHREAD_OS_THREAD_H_
32 
33 #include "_os_common.h"
34 #include "_os_time.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* thread priority */
41 #define OS_THREAD_PRIO_DRV_BH   OS_PRIORITY_HIGH
42 #define OS_THREAD_PRIO_DRV_WORK OS_PRIORITY_ABOVE_NORMAL
43 #define OS_THREAD_PRIO_NET80211 OS_PRIORITY_ABOVE_NORMAL
44 #define OS_THREAD_PRIO_SYS_CTRL OS_PRIORITY_ABOVE_NORMAL
45 #define OS_THREAD_PRIO_WPAS     OS_PRIORITY_ABOVE_NORMAL
46 #define OS_THREAD_PRIO_HOSTAPD  OS_PRIORITY_ABOVE_NORMAL
47 #define OS_THREAD_PRIO_DRV_RX   OS_PRIORITY_NORMAL
48 #define OS_THREAD_PRIO_LWIP     OS_PRIORITY_NORMAL
49 #define OS_THREAD_PRIO_CONSOLE  OS_PRIORITY_ABOVE_NORMAL
50 #define OS_THREAD_PRIO_APP      OS_PRIORITY_NORMAL
51 
52 /** @brief Thread entry definition, which is a pointer to a function */
53 typedef void (*OS_ThreadEntry_t)(void *arg);
54 
55 /** @brief Thread handle definition */
56 typedef rt_thread_t OS_ThreadHandle_t;
57 
58 /**
59  * @brief Thread object definition
60  */
61 typedef struct OS_Thread {
62     rt_thread_t handle;
63 } OS_Thread_t;
64 
65 OS_Status OS_ThreadCreate(OS_Thread_t *thread, const char *name,
66                           OS_ThreadEntry_t entry, void *arg,
67                           OS_Priority priority, uint32_t stackSize);
68 OS_Status OS_ThreadDelete(OS_Thread_t *thread);
69 
70 /**
71  * @brief Check whether the thread object is valid or not
72  * @param[in] thread Pointer to the thread object
73  * @return 1 on valid, 0 on invalid
74  */
OS_ThreadIsValid(OS_Thread_t * thread)75 static inline int OS_ThreadIsValid(OS_Thread_t *thread)
76 {
77     return (thread->handle != OS_INVALID_HANDLE);
78 }
79 
80 /**
81  * @brief Set the thread object to invalid state
82  * @param[in] thread Pointer to the thread object
83  * @return None
84  */
OS_ThreadSetInvalid(OS_Thread_t * thread)85 static inline void OS_ThreadSetInvalid(OS_Thread_t *thread)
86 {
87     thread->handle = OS_INVALID_HANDLE;
88 }
89 
90 /**
91  * @brief Sleep for the given milliseconds
92  *
93  * This function causes the calling thread to sleep and block for the given
94  * milliseconds.
95  *
96  * @param[in] msec Milliseconds to sleep
97  * @return None
98  */
OS_ThreadSleep(OS_Time_t msec)99 static inline void OS_ThreadSleep(OS_Time_t msec)
100 {
101     rt_thread_delay((rt_tick_t)OS_MSecsToTicks(msec));
102 }
103 
104 /**
105  * @brief Yield to another thread of equal priority
106  *
107  * Yielding is where a thread volunteers to leave the running state, without
108  * being pre-empted, and before its time slice has been fully utilized.
109  *
110  * @return None
111  */
OS_ThreadYield(void)112 static inline void OS_ThreadYield(void)
113 {
114     rt_thread_yield();
115 }
116 
117 /**
118  * @brief Get the handle of the current running thread
119  * @return Handle of the current running thread
120  */
OS_ThreadGetCurrentHandle(void)121 static inline OS_ThreadHandle_t OS_ThreadGetCurrentHandle(void)
122 {
123     return (OS_ThreadHandle_t)rt_thread_self();
124 }
125 
126 /**
127  * @brief Suspend the thread scheduler
128  *
129  * Suspending the scheduler prevents a context switch from occurring but leaves
130  * interrupts enabled. If an interrupt requests a context switch while the
131  * scheduler is suspended, then the request is held pending and is performed
132  * only when the scheduler is resumed (un-suspended).
133  *
134  * @return None
135  */
OS_ThreadSuspendScheduler(void)136 static inline void OS_ThreadSuspendScheduler(void)
137 {
138     rt_enter_critical();
139 }
140 
141 /**
142  * @brief Resume the thread scheduler
143  *
144  * Resume scheduler activity, following a previous call to
145  * OS_ThreadSuspendScheduler(), by transitioning the scheduler into the
146  * active state from the suspended state.
147  *
148  * @return None
149  */
OS_ThreadResumeScheduler(void)150 static inline void OS_ThreadResumeScheduler(void)
151 {
152     rt_exit_critical();
153 }
154 
155 /**
156  * @brief Check whether the thread scheduler is running or not
157  * @return 1 on runing, 0 on not running
158  */
OS_ThreadIsSchedulerRunning(void)159 static inline int OS_ThreadIsSchedulerRunning(void)
160 {
161     // TODO
162     printf("[ERR] %s() NOT SUPPORT!", __func__);
163     return 1;
164 //  return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING);
165 }
166 
167 #ifdef __cplusplus
168 }
169 #endif
170 
171 #endif /* _KERNEL_OS_RTTHREAD_OS_THREAD_H_ */
172