OSDN Git Service

git-svn-id: http://svn.osdn.jp/svnroot/mimic/trunk@28 47198e57-cb75-475f-84c4-a814cd6...
[mimic/MiMicSDK.git] / lib / src / NyLPC_cThread.c
1 /*********************************************************************************\r
2  * PROJECT: MiMic\r
3  * --------------------------------------------------------------------------------\r
4  *\r
5  * This file is part of MiMic\r
6  * Copyright (C)2011 Ryo Iizuka\r
7  *\r
8  * MiMic is free software: you can redistribute it and/or modify\r
9  * it under the terms of the GNU Lesser General Public License as published\r
10  * by the Free Software Foundation, either version 3 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU Lesser General Public License\r
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
20  *\r
21  * For further information please contact.\r
22  *      http://nyatla.jp/\r
23  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
24  *\r
25  *********************************************************************************/\r
26 #include "NyLPC_cThread.h"\r
27 #include "FreeRTOS.h"\r
28 \r
29 #include "task.h"\r
30 #include "semphr.h"\r
31 \r
32 \r
33 \r
34 \r
35 static void _task(void *pvParameters);\r
36 \r
37 void NyLPC_cThread_initialize(NyLPC_TcThread_t* i_inst,NyLPC_TInt32 i_stack)\r
38 {\r
39         i_inst->_sbit=NyLPC_TcThread_BIT_IS_TERMINATED;\r
40         NyLPC_AbortIfNot(pdPASS==xTaskCreate(\r
41                 _task,\r
42                 (const portCHAR*)"th",\r
43                 i_stack<1?NyLPC_TcThread_DEFAULT_STACKSIZE:i_stack,\r
44                 ( void * ) i_inst,\r
45                 tskIDLE_PRIORITY,\r
46                 &(i_inst->_taskid)));\r
47 }\r
48 \r
49 void NyLPC_cThread_start(NyLPC_TcThread_t* i_inst,NyLPC_TcThread_ThreadFunc i_func,void* i_param)\r
50 {\r
51         NyLPC_ArgAssert(i_inst!=NULL);\r
52         NyLPC_ArgAssert(i_func!=NULL);\r
53         i_inst->_func=i_func;\r
54         i_inst->_param=i_param;\r
55         i_inst->_sbit=0;\r
56         vTaskResume(i_inst->_taskid);\r
57         return;\r
58 }\r
59 void NyLPC_cThread_join(NyLPC_TcThread_t* i_inst)\r
60 {\r
61         NyLPC_TUInt32_setBit(i_inst->_sbit,NyLPC_TcThread_BIT_IS_JOIN_REQ);\r
62         while(!NyLPC_TUInt32_isBitOn(i_inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED))\r
63         {\r
64                 vTaskDelay(2);//少し待つ。\r
65         }\r
66         //終了\r
67         return;\r
68 }\r
69 void NyLPC_cThread_sleep(NyLPC_TUInt32 i_time_in_msec)\r
70 {\r
71         //ミリ秒単位で待つ\r
72         vTaskDelay(i_time_in_msec/portTICK_RATE_MS);\r
73 }\r
74 \r
75 static void _task(void *pvParameters)\r
76 {\r
77         NyLPC_TcThread_t* inst=(NyLPC_TcThread_t*)pvParameters;\r
78         vTaskSuspend(inst->_taskid);\r
79         for(;;){\r
80                 inst->_func(inst->_param);\r
81                 NyLPC_TUInt32_setBit(inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED);\r
82                 vTaskSuspend(inst->_taskid);\r
83         }\r
84 }\r
85 \r