-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmf_thread.c
239 lines (191 loc) · 5.13 KB
/
mf_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
*
* Thread encapsulate in MiFi Project
*
* Copyright 2012 Innofidei Inc.
*
* @file
* @brief
* thread create helper
*
* @author: jimmy.li <[email protected]>
* @date: 2012-09-10
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
#include <cyg/kernel/kapi.h>
#include <mf_comn.h>
#include <mf_thread.h>
#define LOCAL_TRACE 0
typedef struct {
cyg_thread obj;
cyg_handle_t hdl;
char name[16];
mf_thread_fn entry;
void *pStack;
unsigned stackSz;
void *priv;
} thread_data_t;
static void trampoline(cyg_addrword_t param)
{
thread_data_t *td = (thread_data_t*)param;
td->entry(td->priv);
dprintf(ALWAYS, "Thread %s Exiting\n", td->name);
cyg_thread_exit();
// free(td);
}
#define MF_STACK_SIZE_MIN (512)
/**
* @brief create a thread
*
* @param[in] fn - thread routine function
* @param[in] priv - private data of thread routine
* @param[in] thread_name - thread name
* @param[in] stack_size - thread stack size in bytes
* @param[in] priority - the lower value, the higher priority
*
* @return - if success return non-zero thread handle, otherwise return NULL.
*/
mf_thread_t mf_thread_createEx(mf_thread_fn fn, void* priv, const char* thread_name, unsigned stack_size, int priority)
{
thread_data_t *td;
unsigned totSz;
if (fn == NULL || stack_size < MF_STACK_SIZE_MIN) {
return (mf_thread_t)NULL;
}
totSz = _ALIGN_UP(sizeof(*td), sizeof(long)) + _ALIGN_UP(stack_size, sizeof(long));
td = (thread_data_t *)malloc(totSz);
memset(td, 0, totSz);
if (thread_name)
strncpy(td->name, thread_name, sizeof(td->name));
td->entry = fn;
td->pStack = (void*)((char*)td + _ALIGN_UP(sizeof(*td), 4));
td->stackSz = _ALIGN_UP(stack_size, sizeof(long));
td->priv = priv;
cyg_thread_create(priority,
trampoline,
(cyg_addrword_t)td,
td->name,
(void *)td->pStack,
td->stackSz,
&td->hdl,
&td->obj);
return (mf_thread_t)td;
}
/**
* @brief create a thread
*
* @param[in] fn - thread routine function
* @param[in] priv - private data of thread routine
*
* @return - if success return valid thread handle, otherwise return NULL
*/
mf_thread_t mf_thread_create(mf_thread_fn fn, void* priv)
{
return mf_thread_createEx(fn, priv, "mf_thread", CYGNUM_HAL_STACK_SIZE_TYPICAL * sizeof(long), 10);
}
/**
* @brief set a thread into running
*
* @param[in] t - the created thread handle
*
* @return - none
*/
void mf_thread_run(mf_thread_t t)
{
thread_data_t *td = (thread_data_t*)t;
if (td != NULL)
cyg_thread_resume(td->hdl);
}
/**
* @brief create a thread and set it running
*
* @param[in] fn - thread routine function
* @param[in] priv - private data of thread routine
* @param[in] thread_name - thread name
* @param[in] stack_size - thread stack size in bytes
* @param[in] priority - the lower value, the higher priority
*
*
* @return - if success return non-zero thread handle, otherwise return NULL.
*/
mf_thread_t mf_thread_create_and_runEx(mf_thread_fn fn, void* priv, const char* thread_name, unsigned stack_size, int priority)
{
mf_thread_t t = mf_thread_createEx(fn, priv, thread_name, stack_size, priority);
mf_thread_run(t);
return t;
}
/**
* @brief create a thread and set it running
*
* @param[in] fn - thread routine function
* @param[in] priv - private data of thread routine
*
* @return - if success return non-zero thread handle, otherwise return NULL.
*/
mf_thread_t mf_thread_create_and_run(mf_thread_fn fn, void* priv)
{
mf_thread_t t = mf_thread_create(fn, priv);
mf_thread_run(t);
return t;
}
/*------------------- work ------------------*/
static struct mf_workqueue *default_wq;
static void workqueue_thread(void *priv)
{
struct mf_workqueue *wq = (struct mf_workqueue *)priv;
while (1) {
struct mf_work *work;
_restart:
cyg_mutex_lock(&wq->lock);
if (list_empty(&wq->list)) {
cyg_mutex_unlock(&wq->lock);
cyg_flag_wait(&wq->wakeup, 0x01, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
goto _restart;
}
work = list_entry(wq->list.next, struct mf_work, entry);
list_del(&work->entry);
cyg_mutex_unlock(&wq->lock);
if (work && work->func) {
work->func(work);
}
}
}
struct mf_workqueue * mf_create_workqueue(const char* name)
{
struct mf_workqueue *wq = (struct mf_workqueue *)malloc(sizeof(*wq));
wq->thread = mf_thread_create(workqueue_thread, wq);
INIT_LIST_HEAD(&wq->list);
cyg_mutex_init(&wq->lock);
cyg_flag_init(&wq->wakeup);
wq->flag = 0;
return wq;
}
void mf_destroy_workqueue(struct mf_workqueue *wq)
{
//TODO
}
int mf_queue_work(struct mf_workqueue *wq, struct mf_work *work)
{
cyg_mutex_lock(&wq->lock);
list_add_tail(&work->entry, &wq->list);
cyg_mutex_unlock(&wq->lock);
if (wq->flag & WQ_RUNNING) {
// wakeup workqueue thread
cyg_flag_setbits(&wq->wakeup, 0x1);
} else {
mf_thread_run(wq->thread);
wq->flag |= WQ_RUNNING;
}
return 0;
}
int mf_schedule_work(struct mf_work *work)
{
if (default_wq == NULL)
default_wq = mf_create_workqueue("default");
return mf_queue_work(default_wq, work);
}