1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef __THREAD_H__
16 #define __THREAD_H__
17 
18 #include "freertos/FreeRTOSConfig.h"
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/queue.h"
21 #include "freertos/task.h"
22 #include "osi/semaphore.h"
23 #include "esp_task.h"
24 #include "bt_common.h"
25 
26 #define portBASE_TYPE int
27 
28 #define OSI_THREAD_MAX_TIMEOUT OSI_SEM_MAX_TIMEOUT
29 
30 struct osi_thread;
31 
32 typedef struct osi_thread osi_thread_t;
33 
34 typedef void (*osi_thread_func_t)(void *context);
35 
36 typedef enum {
37     OSI_THREAD_CORE_0 = 0,
38     OSI_THREAD_CORE_1,
39     OSI_THREAD_CORE_AFFINITY,
40 } osi_thread_core_t;
41 
42 /*
43  * brief: Create a thread or task
44  * param name: thread name
45  * param stack_size: thread stack size
46  * param priority: thread priority
47  * param core: the CPU core which this thread run, OSI_THREAD_CORE_AFFINITY means unspecific CPU core
48  * param work_queue_num: speicify queue number, the queue[0] has highest priority, and the priority is decrease by index
49  * return : if create successfully, return thread handler; otherwise return NULL.
50  */
51 osi_thread_t *osi_thread_create(const char *name, size_t stack_size, int priority, osi_thread_core_t core, uint8_t work_queue_num);
52 
53 /*
54  * brief: Destroy a thread or task
55  * param thread: point of thread handler
56  */
57 void osi_thread_free(osi_thread_t *thread);
58 
59 /*
60  * brief: Post an msg to a thread and told the thread call the function
61  * param thread: point of thread handler
62  * param func: callback function that called by target thread
63  * param context: argument of callback function
64  * param queue_idx: the queue which the msg send to
65  * param timeout: post timeout, OSI_THREAD_MAX_TIMEOUT means blocking forever, 0 means never blocking, others means block millisecond
66  * return : if post successfully, return true, otherwise return false
67  */
68 bool osi_thread_post(osi_thread_t *thread, osi_thread_func_t func, void *context, int queue_idx, uint32_t timeout);
69 
70 /*
71  * brief: Set the priority of thread
72  * param thread: point of thread handler
73  * param priority: priority
74  * return : if set successfully, return true, otherwise return false
75  */
76 bool osi_thread_set_priority(osi_thread_t *thread, int priority);
77 
78 /* brief: Get thread name
79  * param thread: point of thread handler
80  * return: constant point of thread name
81  */
82 const char *osi_thread_name(osi_thread_t *thread);
83 
84 /* brief: Get the size of the specified queue
85  * param thread: point of thread handler
86  * param wq_idx: the queue index of the thread
87  * return: queue size
88  */
89 int osi_thread_queue_wait_size(osi_thread_t *thread, int wq_idx);
90 
91 #endif /* __THREAD_H__ */
92