1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2019 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * ä¸è¨èä½æ¨©è
|
---|
7 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
8 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
9 | * å¤ã»åé
|
---|
10 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
11 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
12 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
13 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
14 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
15 | * ç¨ã§ããå½¢ã§åé
|
---|
16 | å¸ããå ´åã«ã¯ï¼åé
|
---|
17 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
18 | * è
|
---|
19 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
20 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
21 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
22 | * ç¨ã§ããªãå½¢ã§åé
|
---|
23 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
24 | * ã¨ï¼
|
---|
25 | * (a) åé
|
---|
26 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
27 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
28 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
29 | * (b) åé
|
---|
30 | å¸ã®å½¢æ
|
---|
31 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
32 | * å ±åãããã¨ï¼
|
---|
33 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
34 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
35 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
36 | 責ãããã¨ï¼
|
---|
37 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
38 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
40 | * å
|
---|
41 | 責ãããã¨ï¼
|
---|
42 | *
|
---|
43 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
44 | ã
|
---|
45 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
46 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
47 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
48 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: threadapi_toppers.c 388 2019-05-22 11:25:18Z coas-nagasima $
|
---|
51 | */
|
---|
52 | #include <stdint.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <kernel.h>
|
---|
55 | #include <t_stdlib.h>
|
---|
56 | #include "azure_c_shared_utility/threadapi.h"
|
---|
57 | #include "azure_c_shared_utility/xlogging.h"
|
---|
58 |
|
---|
59 | MU_DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
|
---|
60 |
|
---|
61 | #ifdef TOPPERS_SUPPORT_DYNAMIC_CRE
|
---|
62 | #define MAX_THREADS 4
|
---|
63 | #define TASK_PRIORITY ((TMAX_TPRI + TMIN_TPRI) / 2)
|
---|
64 |
|
---|
65 | typedef struct _thread
|
---|
66 | {
|
---|
67 | ID tskid;
|
---|
68 | int result;
|
---|
69 | } toppersThread;
|
---|
70 | static toppersThread threads[MAX_THREADS] = { 0 };
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
|
---|
74 | {
|
---|
75 | #ifndef TOPPERS_SUPPORT_DYNAMIC_CRE
|
---|
76 | LogError("TOPPERS/ASP3 does not support multi-thread function.");
|
---|
77 | return THREADAPI_ERROR;
|
---|
78 | #else
|
---|
79 | THREADAPI_RESULT result;
|
---|
80 | if ((threadHandle == NULL) ||
|
---|
81 | (func == NULL))
|
---|
82 | {
|
---|
83 | result = THREADAPI_INVALID_ARG;
|
---|
84 | LogError("(result = %s)", MU_ENUM_TO_STRING(THREADAPI_RESULT, result));
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | ER_ID ret;
|
---|
88 | T_CTSK ctsk;
|
---|
89 | toppersThread *thread = NULL;
|
---|
90 |
|
---|
91 | for (int i = 0; i < MAX_THREADS; i++) {
|
---|
92 | if (threads[i].tskid == 0) {
|
---|
93 | thread = &threads[i];
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | memset(&ctsk, 0, sizeof(ctsk));
|
---|
99 |
|
---|
100 | ctsk.exinf = (intptr_t)arg;
|
---|
101 | ctsk.task = (TASK)func;
|
---|
102 | ctsk.itskpri = TASK_PRIORITY;
|
---|
103 |
|
---|
104 | ret = acre_tsk(&ctsk);
|
---|
105 | if (ret > 0) {
|
---|
106 | if (thread == NULL) {
|
---|
107 | for (int i = 0; i < MAX_THREADS; i++) {
|
---|
108 | if (threads[i].tskid == ret) {
|
---|
109 | thread = &threads[i];
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if (thread != NULL) {
|
---|
115 | thread->result = 0;
|
---|
116 | }
|
---|
117 | if (act_tsk(ret) == E_OK) {
|
---|
118 | *threadHandle = (THREAD_HANDLE)(ret);
|
---|
119 | result = THREADAPI_OK;
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | del_tsk(ret);
|
---|
123 | result = THREADAPI_ERROR;
|
---|
124 | LogError("(result = %s)", MU_ENUM_TO_STRING(THREADAPI_RESULT, result));
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | result = THREADAPI_NO_MEMORY;
|
---|
129 | LogError("(result = %s)", MU_ENUM_TO_STRING(THREADAPI_RESULT, result));
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | return result;
|
---|
134 | #endif
|
---|
135 | }
|
---|
136 |
|
---|
137 | THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE threadHandle, int *res)
|
---|
138 | {
|
---|
139 | #ifndef TOPPERS_SUPPORT_DYNAMIC_CRE
|
---|
140 | LogError("TOPPERS/ASP3 does not support multi-thread function.");
|
---|
141 | return THREADAPI_ERROR;
|
---|
142 | #else
|
---|
143 | ID tskid = (ID)threadHandle;
|
---|
144 | T_RTSK rtsk;
|
---|
145 | toppersThread *thread = NULL;
|
---|
146 | ER ret;
|
---|
147 |
|
---|
148 | for (;;) {
|
---|
149 | ret = ref_tsk(tskid, &rtsk);
|
---|
150 | if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
|
---|
151 | break;
|
---|
152 |
|
---|
153 | dly_tsk(10000);
|
---|
154 | }
|
---|
155 |
|
---|
156 | for (int i = 0; i < MAX_THREADS; i++) {
|
---|
157 | if (threads[i].tskid == ret) {
|
---|
158 | thread = &threads[i];
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (thread != NULL) {
|
---|
164 | *res = thread->result;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return THREADAPI_OK;
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 |
|
---|
171 | void ThreadAPI_Exit(int res)
|
---|
172 | {
|
---|
173 | ER ret;
|
---|
174 | #ifdef TOPPERS_SUPPORT_DYNAMIC_CRE
|
---|
175 | ID tskid;
|
---|
176 |
|
---|
177 | ret = get_tid(&tskid);
|
---|
178 | if (ret != E_OK) {
|
---|
179 | LogError("get_tid error. %s", itron_strerror(ret));
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | for (int i = 0; i < MAX_THREADS; i++) {
|
---|
184 | if (threads[i].tskid == tskid) {
|
---|
185 | threads[i].result = res;
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | #endif
|
---|
190 | ret = ext_tsk();
|
---|
191 | if (ret != E_OK) {
|
---|
192 | LogError("ext_tsk error. %s", itron_strerror(ret));
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | void ThreadAPI_Sleep(unsigned int milliseconds)
|
---|
197 | {
|
---|
198 | ER ret;
|
---|
199 |
|
---|
200 | ret = dly_tsk((RELTIM)milliseconds * 1000);
|
---|
201 | if (ret != E_OK) {
|
---|
202 | LogError("dly_tsk error. %s", itron_strerror(ret));
|
---|
203 | }
|
---|
204 | }
|
---|