OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / lib / src / os / mbedrtos / NyLPC_cThread.cpp
1 /*********************************************************************************
2  * PROJECT: MiMic
3  * --------------------------------------------------------------------------------
4  *
5  * This file is part of MiMic
6  * Copyright (C)2011 Ryo Iizuka
7  *
8  * MiMic is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * For further information please contact.
22  *  http://nyatla.jp/
23  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
24  *
25  *********************************************************************************/
26 #include "../NyLPC_cThread.h"
27
28 #if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS
29 #include "mbed.h"
30 #include "rtos.h"
31
32 static osPriority prio_table[]={
33     osPriorityNormal,osPriorityHigh};
34
35
36
37 static void proc(void const *argument)
38 {
39     NyLPC_TcThread_t* t=(NyLPC_TcThread_t*)argument;
40     for(;;){
41         do{
42            Thread::wait(30);// danger wait!
43         }while(NyLPC_TUInt32_isBitOn(t->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED));
44         t->_func(t->_arg);
45         NyLPC_TUInt32_setBit(t->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED);
46     }
47 }
48
49
50 void NyLPC_cThread_initialize(NyLPC_TcThread_t* i_inst,NyLPC_TInt32 i_stack,NyLPC_TInt32 i_prio)
51 {
52     NyLPC_TUInt32_setBit(i_inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED);
53     i_inst->_thread=new Thread(proc,i_inst,prio_table[i_prio],i_stack);
54 }
55 void NyLPC_cThread_finalize(NyLPC_TcThread_t* i_inst)
56 {
57     NyLPC_cThread_join(i_inst);
58     delete (Thread*)(i_inst->_thread);
59 }
60
61
62 void NyLPC_cThread_start(NyLPC_TcThread_t* i_inst,NyLPC_TcThread_ThreadFunc i_func,void* i_param)
63 {
64     NyLPC_ArgAssert(i_inst!=NULL);
65     NyLPC_ArgAssert(i_func!=NULL);
66     i_inst->_sbit=0;
67     i_inst->_func=i_func;
68     i_inst->_arg=i_param;
69     return;
70 }
71 void NyLPC_cThread_join(NyLPC_TcThread_t* i_inst)
72 {
73     NyLPC_TUInt32_setBit(i_inst->_sbit,NyLPC_TcThread_BIT_IS_JOIN_REQ);
74     while(!NyLPC_TUInt32_isBitOn(i_inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED))
75     {
76         Thread::wait(10);
77     }
78     return;
79 }
80 void NyLPC_cThread_sleep(NyLPC_TUInt32 i_time_in_msec)
81 {
82     //ミリ秒単位で待つ
83     Thread::wait(i_time_in_msec);
84 }
85 void NyLPC_cThread_yield(void)
86 {
87     Thread::yield();
88 }
89
90 #endif