1 /*
2 * author : prife (goprife@gmail.com)
3 * date : 2013/01/14 01:18:50
4 * version: v 0.2.0
5 */
6 #include <rtthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <pthread.h>
10 #include <signal.h>
11 #include <unistd.h>
12 #include <semaphore.h>
13 #include <time.h>
14 #include <sys/time.h>
15
16 //#define TRACE printf
17 #define TRACE(...)
18
19 typedef struct _thread
20 {
21 pthread_t pthread;
22 void (*task)(void *);
23 void *para;
24 void (*exit)(void);
25 sem_t sem;
26 rt_thread_t rtthread;
27 int status;
28 void *data;
29 } thread_t;
30
31 #define THREAD_T(thread) ((thread_t *)thread)
32
33 #define MSG_SUSPEND SIGUSR1 /* 10 */
34 #define MSG_RESUME SIGUSR2
35 #define MSG_TICK SIGALRM /* 14 */
36 #define TIMER_TYPE ITIMER_REAL
37 #define MAX_INTERRUPT_NUM ((unsigned int)sizeof(unsigned int) * 8)
38
39 #define INTERRUPT_ENABLE 0
40 #define INTERRUPT_DISABLE 1
41
42 /* 线程挂起状态,共两种取值 */
43 #define SUSPEND_LOCK 0
44 #define SUSPEND_SIGWAIT 1
45 #define THREAD_RUNNING 2
46
47 /* interrupt flag, if 1, disable, if 0, enable */
48 static long interrupt_disable_flag;
49 //static int systick_signal_flag;
50
51 /* flag in interrupt handling */
52 rt_ubase_t rt_interrupt_from_thread, rt_interrupt_to_thread;
53 rt_ubase_t rt_thread_switch_interrupt_flag;
54
55 /* interrupt event mutex */
56 static pthread_mutex_t *ptr_int_mutex;
57 static pthread_cond_t cond_int_hit; /* interrupt occured! */
58 static volatile unsigned int cpu_pending_interrupts;
59 static int (* cpu_isr_table[MAX_INTERRUPT_NUM])(void) = {0};
60
61 static pthread_t mainthread_pid;
62
63 /* function definition */
64 static void start_sys_timer(void);
65 static int tick_interrupt_isr(void);
66 static void mthread_signal_tick(int sig);
67 static int mainthread_scheduler(void);
68
signal_install(int sig,void (* func)(int))69 int signal_install(int sig, void (*func)(int))
70 {
71 struct sigaction act;
72
73 /* set the signal handler */
74 act.sa_handler = func ;
75 sigemptyset(&act.sa_mask);
76 act.sa_flags = 0;
77 sigaction(sig, &act, 0);
78 }
79
signal_mask(void)80 int signal_mask(void)
81 {
82 sigset_t sigmask, oldmask;
83
84 /* set signal mask */
85 sigemptyset(&sigmask);
86 sigaddset(&sigmask, SIGALRM);
87 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
88 }
thread_suspend_signal_handler(int sig)89 static void thread_suspend_signal_handler(int sig)
90 {
91 sigset_t sigmask;
92 pthread_t pid = pthread_self();
93 thread_t *thread_from;
94 thread_t *thread_to;
95 rt_thread_t tid;
96
97 if (sig != MSG_SUSPEND)
98 {
99 printf("get an unexpected signal <%d>, exit\n", sig);
100 exit(EXIT_FAILURE);
101 }
102
103 thread_from = (thread_t *) rt_interrupt_from_thread;
104 thread_to = (thread_t *) rt_interrupt_to_thread;
105
106 /* 注意!此时 rt_thread_self的值是to线程的值! */
107 tid = rt_thread_self();
108 /* FIXME RT_ASSERT(thread_from->pthread == pid); */
109 RT_ASSERT((thread_t *)(tid->sp) == thread_to);
110
111 TRACE("signal: SIGSUSPEND suspend <%s>\n", thread_from->rtthread->name);
112
113 /* 使用sigwait或者sigsuspend来挂起from线程 */
114 //sem_wait(&thread_from->sem);
115 sigemptyset(&sigmask);
116 sigaddset(&sigmask, MSG_RESUME);
117
118 /* Beginnig Linux Programming上说,当信号处理函数运行中,此信号就会被屏蔽,
119 * 以防止重复执行信号处理函数
120 */
121 thread_from->status = SUSPEND_SIGWAIT;
122 if (sigwait(&sigmask, &sig) != 0)
123 {
124 printf("sigwait faild, %d\n", sig);
125 }
126 thread_to = (thread_t *) rt_interrupt_to_thread;
127 RT_ASSERT(thread_to == thread_from);
128 thread_to->status = THREAD_RUNNING;
129 TRACE("signal: SIGSUSPEND resume <%s>\n", thread_from->rtthread->name);
130 }
131
thread_resume_signal_handler(int sig)132 static void thread_resume_signal_handler(int sig)
133 {
134 sigset_t sigmask;
135 pthread_t pid = pthread_self();
136 thread_t *thread_from;
137 thread_t *thread_to;
138 rt_thread_t tid;
139
140 thread_from = (thread_t *) rt_interrupt_from_thread;
141 thread_to = (thread_t *) rt_interrupt_to_thread;
142
143 /* 注意!此时 rt_thread_self的值是to线程的值! */
144 tid = rt_thread_self();
145 RT_ASSERT((thread_t *)(tid->sp) == thread_to);
146
147 TRACE("signal: SIGRESUME resume <%s>\n", thread_to->rtthread->name);
148 }
149
thread_run(void * parameter)150 static void *thread_run(void *parameter)
151 {
152 rt_thread_t tid;
153 thread_t *thread;
154 thread = THREAD_T(parameter);
155 int res;
156
157 /* set signal mask, mask the timer! */
158 signal_mask();
159
160 thread->status = SUSPEND_LOCK;
161 TRACE("pid <%08x> stop on sem...\n", (unsigned int)(thread->pthread));
162 sem_wait(&thread->sem);
163
164 tid = rt_thread_self();
165 TRACE("pid <%08x> tid <%s> starts...\n", (unsigned int)(thread->pthread),
166 tid->parent.name);
167 thread->rtthread = tid;
168 thread->task(thread->para);
169 TRACE("pid <%08x> tid <%s> exit...\n", (unsigned int)(thread->pthread),
170 tid->parent.name);
171 thread->exit();
172
173 /*TODO:
174 * 最后一行的pthread_exit永远没有机会执行,这是因为在threead->exit函数中
175 * 会发生线程切换,并永久将此pthread线程挂起,所以更完美的解决方案是在这
176 * 里发送信号给主线程,主线程中再次唤醒此线程令其自动退出。
177 */
178 //sem_destroy(&thread->sem);
179
180 pthread_exit(NULL);
181 }
thread_create(thread_t * thread,void * task,void * parameter,void * pexit)182 static int thread_create(
183 thread_t *thread, void *task, void *parameter, void *pexit)
184 {
185 int res;
186 pthread_attr_t attr;
187
188 thread->task = task;
189 thread->para = parameter;
190 thread->exit = pexit;
191
192 if (sem_init(&thread->sem, 0, 0) != 0)
193 {
194 printf("init thread->sem failed, exit \n");
195 exit(EXIT_FAILURE);
196 }
197 /* No need to join the threads. */
198 pthread_attr_init(&attr);
199 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
200
201 /* create a posix thread */
202 res = pthread_create(&thread->pthread, &attr, &thread_run, (void *)thread);
203 if (res)
204 {
205 printf("pthread create faild, <%d>\n", res);
206 exit(EXIT_FAILURE);
207 }
208
209 return 0;
210 }
211
212 /* resume the thread */
thread_resume(thread_t * thread)213 static int thread_resume(thread_t *thread)
214 {
215 sem_post(& thread->sem);
216 }
217
218
rt_hw_stack_init(void * pEntry,void * pParam,rt_uint8_t * pStackAddr,void * pExit)219 rt_uint8_t *rt_hw_stack_init(
220 void *pEntry,
221 void *pParam,
222 rt_uint8_t *pStackAddr,
223 void *pExit)
224 {
225 thread_t *thread;
226
227 thread = (thread_t *)(pStackAddr - sizeof(thread_t));
228
229 /* set the filed to zero */
230 memset(thread, 0x00, sizeof(thread_t));
231
232 thread_create(thread, pEntry, pParam, pExit);
233 //TRACE("thread %x created\n", (unsigned int)thread_table[t].pthread);
234
235 return (rt_uint8_t *) thread;
236 }
237
rt_hw_interrupt_disable(void)238 rt_base_t rt_hw_interrupt_disable(void)
239 {
240 long back;
241
242 if (ptr_int_mutex == NULL)
243 {
244 return 0;
245 }
246
247 pthread_mutex_lock(ptr_int_mutex);
248 back = interrupt_disable_flag;
249 interrupt_disable_flag = INTERRUPT_DISABLE;
250
251 /*TODO: It may need to unmask the signal */
252 return back;
253 }
254
rt_hw_interrupt_enable(rt_base_t level)255 void rt_hw_interrupt_enable(rt_base_t level)
256 {
257 struct rt_thread * tid;
258 pthread_t pid;
259 thread_t *thread_from;
260 thread_t *thread_to;
261
262 if (ptr_int_mutex == NULL)
263 return;
264
265 interrupt_disable_flag = level;
266
267 pthread_mutex_unlock(ptr_int_mutex);
268 /* 如果已经中断仍然关闭 */
269 if (interrupt_disable_flag)
270 {
271 return;
272 }
273
274 /* 表示当前中断打开, 检查是否有挂起的中断 */
275 pthread_mutex_lock(ptr_int_mutex);
276 if (!cpu_pending_interrupts)
277 {
278 pthread_mutex_unlock(ptr_int_mutex);
279 return;
280 }
281
282 thread_from = (thread_t *) rt_interrupt_from_thread;
283 thread_to = (thread_t *) rt_interrupt_to_thread;
284 tid = rt_thread_self();
285 pid = pthread_self();
286
287 //pid != mainthread_pid &&
288 if (thread_from->pthread == pid)
289 {
290 /* 注意这段代码是在RTT普通线程函数总函数中执行的,
291 * from线程就是当前rtt线程 */
292 /* 需要检查是否有挂起的中断需要处理 */
293 TRACE("conswitch: P in pid<%x> ,suspend <%s>, resume <%s>!\n",
294 (unsigned int)pid,
295 thread_from->rtthread->name,
296 thread_to->rtthread->name);
297
298 cpu_pending_interrupts --;
299 thread_from->status = SUSPEND_LOCK;
300 pthread_mutex_unlock(ptr_int_mutex);
301 /* 唤醒被挂起的线程 */
302 if (thread_to->status == SUSPEND_SIGWAIT)
303 {
304 pthread_kill(thread_to->pthread, MSG_RESUME);
305 }
306 else if (thread_to->status == SUSPEND_LOCK)
307 {
308 sem_post(& thread_to->sem);
309 }
310 else
311 {
312 printf("conswitch: should not be here! %d\n", __LINE__);
313 exit(EXIT_FAILURE);
314 }
315
316 /* 挂起当前的线程 */
317 sem_wait(& thread_from->sem);
318 pthread_mutex_lock(ptr_int_mutex);
319 thread_from->status = THREAD_RUNNING;
320 pthread_mutex_unlock(ptr_int_mutex);
321 }
322 else
323 {
324 /* 注意这段代码可能在多种情况下运行:
325 * 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行
326 * 2. 其他线程中调用,比如用于获取按键输入的线程中调用
327 */
328 TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n",
329 (unsigned int)pid,
330 thread_from->rtthread->name,
331 thread_to->rtthread->name);
332
333 cpu_pending_interrupts --;
334
335 /* 需要把解锁函数放在前面,以防止死锁?? */
336 pthread_mutex_unlock(ptr_int_mutex);
337
338 /* 挂起from线程 */
339 pthread_kill(thread_from->pthread, MSG_SUSPEND);
340 /* 注意:这里需要确保线程被挂起了, 否则312行就很可能就会报错退出
341 * 因为这里挂起线程是通过信号实现的,所以一定要确保线程挂起才行 */
342 while (thread_from->status != SUSPEND_SIGWAIT)
343 {
344 sched_yield();
345 }
346
347 /* 唤醒to线程 */
348 if (thread_to->status == SUSPEND_SIGWAIT)
349 {
350 pthread_kill(thread_to->pthread, MSG_RESUME);
351 }
352 else if (thread_to->status == SUSPEND_LOCK)
353 {
354 sem_post(& thread_to->sem);
355 }
356 else
357 {
358 printf("conswitch: should not be here! %d\n", __LINE__);
359 exit(EXIT_FAILURE);
360 }
361
362 }
363 /*TODO: It may need to unmask the signal */
364 }
365
rt_hw_context_switch(rt_ubase_t from,rt_ubase_t to)366 void rt_hw_context_switch(rt_ubase_t from,
367 rt_ubase_t to)
368 {
369 struct rt_thread * tid;
370 pthread_t pid;
371 thread_t *thread_from;
372 thread_t *thread_to;
373
374 RT_ASSERT(from != to);
375
376 #if 0
377 //TODO: 可能还需要考虑嵌套切换的情况
378 if (rt_thread_switch_interrupt_flag != 1)
379 {
380 rt_thread_switch_interrupt_flag = 1;
381
382 // set rt_interrupt_from_thread
383 rt_interrupt_from_thread = *((rt_ubase_t *)from);
384 }
385 #endif
386 pthread_mutex_lock(ptr_int_mutex);
387 rt_interrupt_from_thread = *((rt_ubase_t *)from);
388 rt_interrupt_to_thread = *((rt_ubase_t *)to);
389
390 /* 这个函数只是并不会真正执行中断处理函数,而只是简单的
391 * 设置一下中断挂起标志位
392 */
393 cpu_pending_interrupts ++;
394 pthread_mutex_unlock(ptr_int_mutex);
395 }
396
rt_hw_context_switch_interrupt(rt_ubase_t from,rt_ubase_t to,rt_thread_t from_thread,rt_thread_t to_thread)397 void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
398 {
399 rt_hw_context_switch(from, to);
400 }
401
rt_hw_context_switch_to(rt_ubase_t to)402 void rt_hw_context_switch_to(rt_ubase_t to)
403 {
404 //set to thread
405 rt_interrupt_to_thread = *((rt_ubase_t *)(to));
406
407 //clear from thread
408 rt_interrupt_from_thread = 0;
409
410 //set interrupt to 1
411 rt_thread_switch_interrupt_flag = 0; //TODO: 还需要考虑这个嵌套切换的情况
412
413 /* enable interrupt
414 * note: NOW, there are only one interrupt in simposix: system tick */
415 rt_hw_interrupt_enable(0);
416
417 //start the main thread scheduler
418 mainthread_scheduler();
419
420 //never reach here!
421 return;
422 }
423
mainthread_scheduler(void)424 static int mainthread_scheduler(void)
425 {
426 int i, res, sig;
427 thread_t *thread_from;
428 thread_t *thread_to;
429 pthread_mutex_t mutex;
430 pthread_mutexattr_t mutexattr;
431 sigset_t sigmask, oldmask;
432
433 /* save the main thread id */
434 mainthread_pid = pthread_self();
435 TRACE("pid <%08x> mainthread\n", (unsigned int)(mainthread_pid));
436
437 /* 屏蔽suspend信号和resume信号 */
438 sigemptyset(&sigmask);
439 sigaddset(&sigmask, MSG_SUSPEND);
440 sigaddset(&sigmask, MSG_RESUME);
441 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
442
443 sigemptyset(&sigmask);
444 sigaddset(&sigmask, SIGALRM);
445
446 /* install signal handler of system tick */
447 signal_install(SIGALRM, mthread_signal_tick);
448 /* install signal handler used to suspend/resume threads */
449 signal_install(MSG_SUSPEND, thread_suspend_signal_handler);
450 signal_install(MSG_RESUME, thread_resume_signal_handler);
451
452 /* create a mutex and condition val, used to indicate interrupts occrue */
453 ptr_int_mutex = &mutex;
454 pthread_mutexattr_init(&mutexattr);
455 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
456 pthread_mutex_init(ptr_int_mutex, &mutexattr);
457
458 /* start timer */
459 start_sys_timer();
460
461 thread_to = (thread_t *) rt_interrupt_to_thread;
462 thread_resume(thread_to);
463 for (;;)
464 {
465 #if 1
466 if (sigwait(&sigmask, &sig) != 0)
467 {
468 printf("mthread: sigwait get unexpected sig %d\n", sig);
469 }
470 #else
471 pause();
472 #endif
473 TRACE("mthread:got sig %d\n", sig);
474 /* signal mask sigalrm 屏蔽SIGALRM信号 */
475 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
476
477 // if (systick_signal_flag != 0)
478 if (pthread_mutex_trylock(ptr_int_mutex) == 0)
479 {
480 tick_interrupt_isr();
481 // systick_signal_flag = 0;
482 pthread_mutex_unlock(ptr_int_mutex);
483 }
484 else
485 {
486 TRACE("try lock failed.\n");
487 }
488
489 /* 开启SIGALRM信号 */
490 pthread_sigmask(SIG_UNBLOCK, &sigmask, &oldmask);
491 }
492
493 return 0;
494 }
495
496 /*
497 * Setup the systick timer to generate the tick interrupts at the required
498 * frequency.
499 */
start_sys_timer(void)500 static void start_sys_timer(void)
501 {
502 struct itimerval itimer, oitimer;
503 int us;
504
505 us = 1000000 / RT_TICK_PER_SECOND - 1;
506
507 TRACE("start system tick!\n");
508 /* Initialise the structure with the current timer information. */
509 if (0 != getitimer(TIMER_TYPE, &itimer))
510 {
511 TRACE("get timer failed.\n");
512 exit(EXIT_FAILURE);
513 }
514
515 /* Set the interval between timer events. */
516 itimer.it_interval.tv_sec = 0;
517 itimer.it_interval.tv_usec = us;
518 /* Set the current count-down. */
519 itimer.it_value.tv_sec = 0;
520 itimer.it_value.tv_usec = us;
521
522 /* Set-up the timer interrupt. */
523 if (0 != setitimer(TIMER_TYPE, &itimer, &oitimer))
524 {
525 TRACE("set timer failed.\n");
526 exit(EXIT_FAILURE);
527 }
528 }
529
mthread_signal_tick(int sig)530 static void mthread_signal_tick(int sig)
531 {
532 int res;
533 pthread_t pid = pthread_self();
534
535 if (sig == SIGALRM)
536 {
537 TRACE("pid <%x> signal: SIGALRM enter!\n", (unsigned int)pid);
538 //systick_signal_flag = 1;
539 TRACE("pid <%x> signal: SIGALRM leave!\n", (unsigned int)pid);
540 }
541 else
542 {
543 TRACE("got an unexpected signal <%d>\n", sig);
544 exit(EXIT_FAILURE);
545 }
546 }
547
548 /* isr return value: 1, should not be masked, if 0, can be masked */
tick_interrupt_isr(void)549 static int tick_interrupt_isr(void)
550 {
551 TRACE("isr: systick enter!\n");
552 /* enter interrupt */
553 rt_interrupt_enter();
554
555 rt_tick_increase();
556
557 /* leave interrupt */
558 rt_interrupt_leave();
559
560 TRACE("isr: systick leave!\n");
561 return 0;
562 }
563
564