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