source: azure_iot_hub/trunk/asp3_dcre/kernel/task.c@ 389

Last change on this file since 389 was 389, checked in by coas-nagasima, 5 years ago

ビルドが通るよう更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 11.3 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2005-2014 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 *
11 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
12 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
13 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16 * スコード中に含まれていること.
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20 * の無保証規定を掲載すること.
21 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23 * と.
24 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
26 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27 * 報告すること.
28 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
31 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
32 * 免責すること.
33 *
34 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
35 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
36 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
37 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
38 * の責任を負わない.
39 *
40 * $Id$
41 */
42
43/*
44 * タスク管理モジュール
45 */
46
47#include "kernel_impl.h"
48#include "task.h"
49#include "taskhook.h"
50#include "wait.h"
51
52#ifdef TOPPERS_tskini
53
54/*
55 * 実行状態のタスク
56 */
57TCB *p_runtsk;
58
59/*
60 * 実行すべきタスク
61 */
62TCB *p_schedtsk;
63
64/*
65 * ディスパッチ許可状態
66 */
67bool_t enadsp;
68
69/*
70 * タスクディスパッチ可能状態
71 */
72bool_t dspflg;
73
74/*
75 * レディキュー
76 */
77QUEUE ready_queue[TNUM_TPRI];
78
79/*
80 * レディキューサーチのためのビットマップ
81 */
82uint16_t ready_primap;
83
84/*
85 * 使用していないTCBのリスト
86 */
87QUEUE free_tcb;
88
89/*
90 * タスク管理モジュールの初期化
91 */
92void
93initialize_task(void)
94{
95 uint_t i, j;
96 TCB *p_tcb;
97 TINIB *p_tinib;
98
99 p_runtsk = NULL;
100 p_schedtsk = NULL;
101 enadsp = true;
102 dspflg = true;
103
104 for (i = 0; i < TNUM_TPRI; i++) {
105 queue_initialize(&(ready_queue[i]));
106 }
107 ready_primap = 0U;
108
109 for (i = 0; i < tnum_stsk; i++) {
110 j = INDEX_TSK(torder_table[i]);
111 p_tcb = &(tcb_table[j]);
112 p_tcb->p_tinib = &(tinib_table[j]);
113 p_tcb->actque = false;
114 make_dormant(p_tcb);
115 if ((p_tcb->p_tinib->tskatr & TA_ACT) != 0U) {
116 make_active(p_tcb);
117 }
118 }
119 queue_initialize(&free_tcb);
120 for (j = 0; i < tnum_tsk; i++, j++) {
121 p_tcb = &(tcb_table[i]);
122 p_tinib = &(atinib_table[j]);
123 p_tinib->tskatr = TA_NOEXS;
124 p_tcb->p_tinib = ((const TINIB *) p_tinib);
125 queue_insert_prev(&free_tcb, &(p_tcb->task_queue));
126 }
127}
128
129#endif /* TOPPERS_tskini */
130
131/*
132 * ビットマップサーチ関数
133 *
134 * bitmap内の1のビットの内,最も下位(右)のものをサーチし,そのビッ
135 * ト番号を返す.ビット番号は,最下位ビットを0とする.bitmapに0を指定
136 * してはならない.この関数では,bitmapが16ビットであることを仮定し,
137 * uint16_t型としている.
138 *
139 * ビットサーチ命令を持つプロセッサでは,ビットサーチ命令を使うように
140 * 書き直した方が効率が良い場合がある.このような場合には,ターゲット
141 * 依存部でビットサーチ命令を使ったbitmap_searchを定義し,
142 * OMIT_BITMAP_SEARCHをマクロ定義すればよい.また,ビットサーチ命令の
143 * サーチ方向が逆などの理由で優先度とビットとの対応を変更したい場合に
144 * は,PRIMAP_BITをマクロ定義すればよい.
145 *
146 * また,ライブラリにffsがあるなら,次のように定義してライブラリ関数を
147 * 使った方が効率が良い可能性もある.
148 * #define bitmap_search(bitmap) (ffs(bitmap) - 1)
149 */
150#ifndef PRIMAP_BIT
151#define PRIMAP_BIT(pri) (1U << (pri))
152#endif /* PRIMAP_BIT */
153
154#ifndef OMIT_BITMAP_SEARCH
155
156static const unsigned char bitmap_search_table[] = { 0, 1, 0, 2, 0, 1, 0,
157 3, 0, 1, 0, 2, 0, 1, 0 };
158
159Inline uint_t
160bitmap_search(uint16_t bitmap)
161{
162 uint_t n = 0U;
163
164 assert(bitmap != 0U);
165 if ((bitmap & 0x00ffU) == 0U) {
166 bitmap >>= 8;
167 n += 8;
168 }
169 if ((bitmap & 0x0fU) == 0U) {
170 bitmap >>= 4;
171 n += 4;
172 }
173 return(n + bitmap_search_table[(bitmap & 0x0fU) - 1]);
174}
175
176#endif /* OMIT_BITMAP_SEARCH */
177
178/*
179 * 優先度ビットマップが空かのチェック
180 */
181Inline bool_t
182primap_empty(void)
183{
184 return(ready_primap == 0U);
185}
186
187/*
188 * 優先度ビットマップのサーチ
189 */
190Inline uint_t
191primap_search(void)
192{
193 return(bitmap_search(ready_primap));
194}
195
196/*
197 * 優先度ビットマップのセット
198 */
199Inline void
200primap_set(uint_t pri)
201{
202 ready_primap |= PRIMAP_BIT(pri);
203}
204
205/*
206 * 優先度ビットマップのクリア
207 */
208Inline void
209primap_clear(uint_t pri)
210{
211 ready_primap &= ~PRIMAP_BIT(pri);
212}
213
214/*
215 * 最高優先順位タスクのサーチ
216 */
217#ifdef TOPPERS_tsksched
218
219TCB *
220search_schedtsk(void)
221{
222 uint_t schedpri;
223
224 schedpri = primap_search();
225 return((TCB *)(ready_queue[schedpri].p_next));
226}
227
228#endif /* TOPPERS_tsksched */
229
230/*
231 * 実行できる状態への遷移
232 *
233 * 実行すべきタスクを更新するのは,実行できるタスクがなかった場合と,
234 * p_tcbの優先度が実行すべきタスクの優先度よりも高い場合である.
235 */
236#ifdef TOPPERS_tskrun
237
238void
239make_runnable(TCB *p_tcb)
240{
241 uint_t pri = p_tcb->priority;
242
243 queue_insert_prev(&(ready_queue[pri]), &(p_tcb->task_queue));
244 primap_set(pri);
245
246 if (dspflg) {
247 if (p_schedtsk == (TCB *) NULL || pri < p_schedtsk->priority) {
248 p_schedtsk = p_tcb;
249 }
250 }
251}
252
253#endif /* TOPPERS_tskrun */
254
255/*
256 * 実行できる状態から他の状態への遷移
257 *
258 * 実行すべきタスクを更新するのは,p_tcbが実行すべきタスクであった場合
259 * である.p_tcbと同じ優先度のタスクが他にある場合は,p_tcbの次のタス
260 * クが実行すべきタスクになる.そうでない場合は,レディキューをサーチ
261 * する必要がある.
262 */
263#ifdef TOPPERS_tsknrun
264
265void
266make_non_runnable(TCB *p_tcb)
267{
268 uint_t pri = p_tcb->priority;
269 QUEUE *p_queue = &(ready_queue[pri]);
270
271 queue_delete(&(p_tcb->task_queue));
272 if (queue_empty(p_queue)) {
273 primap_clear(pri);
274 if (p_schedtsk == p_tcb) {
275 assert(dspflg);
276 p_schedtsk = primap_empty() ? (TCB *) NULL : search_schedtsk();
277 }
278 }
279 else {
280 if (p_schedtsk == p_tcb) {
281 assert(dspflg);
282 p_schedtsk = (TCB *)(p_queue->p_next);
283 }
284 }
285}
286
287#endif /* TOPPERS_tsknrun */
288
289/*
290 * 休止状態への遷移
291 */
292#ifdef TOPPERS_tskdmt
293
294void
295make_dormant(TCB *p_tcb)
296{
297 p_tcb->tstat = TS_DORMANT;
298 p_tcb->bpriority = p_tcb->p_tinib->ipriority;
299 p_tcb->priority = p_tcb->p_tinib->ipriority;
300 p_tcb->wupque = false;
301 p_tcb->raster = false;
302 p_tcb->enater = true;
303 LOG_TSKSTAT(p_tcb);
304}
305
306#endif /* TOPPERS_tskdmt */
307
308/*
309 * 休止状態から実行できる状態への遷移
310 */
311#ifdef TOPPERS_tskact
312
313void
314make_active(TCB *p_tcb)
315{
316 activate_context(p_tcb);
317 p_tcb->tstat = TS_RUNNABLE;
318 LOG_TSKSTAT(p_tcb);
319 make_runnable(p_tcb);
320}
321
322#endif /* TOPPERS_tskact */
323
324/*
325 * タスクの優先度の変更
326 *
327 * タスクが実行できる状態の場合には,レディキューの中での位置を変更す
328 * る.オブジェクトの待ちキューの中で待ち状態になっている場合には,待
329 * ちキューの中での位置を変更する.
330 *
331 * 実行すべきタスクを更新するのは,(1) p_tcbが実行すべきタスクであって,
332 * その優先度を下げた場合,(2) p_tcbが実行すべきタスクではなく,変更後
333 * の優先度が実行すべきタスクの優先度よりも高い場合である.(1)の場合に
334 * は,レディキューをサーチする必要がある.
335 */
336#ifdef TOPPERS_tskpri
337
338void
339change_priority(TCB *p_tcb, uint_t newpri, bool_t mtxmode)
340{
341 uint_t oldpri;
342
343 oldpri = p_tcb->priority;
344 p_tcb->priority = newpri;
345
346 if (TSTAT_RUNNABLE(p_tcb->tstat)) {
347 /*
348 * タスクが実行できる状態の場合
349 */
350 queue_delete(&(p_tcb->task_queue));
351 if (queue_empty(&(ready_queue[oldpri]))) {
352 primap_clear(oldpri);
353 }
354 if (mtxmode) {
355 queue_insert_next(&(ready_queue[newpri]), &(p_tcb->task_queue));
356 }
357 else {
358 queue_insert_prev(&(ready_queue[newpri]), &(p_tcb->task_queue));
359 }
360 primap_set(newpri);
361
362 if (dspflg) {
363 if (p_schedtsk == p_tcb) {
364 if (newpri >= oldpri) {
365 p_schedtsk = search_schedtsk();
366 }
367 }
368 else {
369 if (mtxmode ? newpri <= p_schedtsk->priority
370 : newpri < p_schedtsk->priority) {
371 p_schedtsk = p_tcb;
372 }
373 }
374 }
375 }
376 else {
377 if (TSTAT_WAIT_WOBJCB(p_tcb->tstat)) {
378 /*
379 * タスクが,同期・通信オブジェクトの管理ブロックの共通部
380 * 分(WOBJCB)の待ちキューにつながれている場合
381 */
382 wobj_change_priority(((WINFO_WOBJ *)(p_tcb->p_winfo))->p_wobjcb,
383 p_tcb);
384 }
385 }
386}
387
388#endif /* TOPPERS_tskpri */
389
390/*
391 * レディキューの回転
392 *
393 * 実行すべきタスクを更新するのは,実行すべきタスクがタスクキューの末
394 * 尾に移動した場合である.
395 */
396#ifdef TOPPERS_tskrot
397
398void
399rotate_ready_queue(QUEUE *p_queue)
400{
401 QUEUE *p_entry;
402
403 if (!queue_empty(p_queue) && p_queue->p_next->p_next != p_queue) {
404 p_entry = queue_delete_next(p_queue);
405 queue_insert_prev(p_queue, p_entry);
406 if (dspflg) {
407 if (p_schedtsk == (TCB *) p_entry) {
408 p_schedtsk = (TCB *)(p_queue->p_next);
409 }
410 }
411 }
412}
413
414#endif /* TOPPERS_tskrot */
415
416/*
417 * タスクの終了処理
418 */
419#ifdef TOPPERS_tskterm
420
421void
422task_terminate(TCB *p_tcb)
423{
424 if (TSTAT_RUNNABLE(p_tcb->tstat)) {
425 make_non_runnable(p_tcb);
426 }
427 else if (TSTAT_WAITING(p_tcb->tstat)) {
428 wait_dequeue_wobj(p_tcb);
429 wait_dequeue_tmevtb(p_tcb);
430 }
431 if (p_tcb->p_lastmtx != NULL) {
432 (*mtxhook_release_all)(p_tcb);
433 }
434 make_dormant(p_tcb);
435 if (p_tcb->actque) {
436 p_tcb->actque = false;
437 make_active(p_tcb);
438 }
439}
440
441#endif /* TOPPERS_tskterm */
Note: See TracBrowser for help on using the repository browser.