OSDN Git Service

dc27a470d6001e265579a02da9f7f46f065a06c2
[hos/hos-v4a.git] / sample / h8 / h83069 / sample.c
1 /** 
2  *  Sample program for Hyper Operating System V4 Advance
3  *
4  * @file  sample.c
5  * @brief %jp{サンプルプログラム}%en{Sample program}
6  *
7  * Copyright (C) 1998-2006 by Project HOS
8  * http://sourceforge.jp/projects/hos/
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include "kernel.h"
14 #include "kernel_id.h"
15 #include "sci1.h"
16
17
18 #define LEFT(num)       ((num) <= 1 ? 5 : (num) - 1)
19 #define RIGHT(num)      ((num) >= 5 ? 1 : (num) + 1)
20
21
22 ID mbxid;
23 ID mpfid;
24
25
26 /** %jp{メッセージ構造体} */
27 typedef struct t_print_msg
28 {
29         T_MSG msg;
30         char  text[32];
31 } T_PRINT_MSG;
32
33
34 /** %jp{初期化ハンドラ} */
35 void Sample_Initialize(VP_INT exinf)
36 {
37         T_CMPF cmpf;
38         T_CMBX cmbx;
39         
40         /* %jp{固定長メモリプール生成} */
41         cmpf.mpfatr = TA_TFIFO;                                 
42         cmpf.blkcnt = 3;                                                
43         cmpf.blksz  = sizeof(T_PRINT_MSG);              
44         cmpf.mpf    = NULL;                                             
45         mpfid = acre_mpf(&cmpf);
46
47         /* %jp{メールボックス生成} */
48         cmbx.mbxatr  = TA_TFIFO | TA_TFIFO;             
49         cmbx.maxmpri = 1;                                               
50         cmbx.mprihd  = NULL;                                    
51         mbxid = acre_mbx(&cmbx);
52
53         /* %jp{タスク起動} */
54         act_tsk(TSKID_PRINT);
55         act_tsk(TSKID_SAMPLE1);
56         act_tsk(TSKID_SAMPLE2);
57         act_tsk(TSKID_SAMPLE3);
58         act_tsk(TSKID_SAMPLE4);
59         act_tsk(TSKID_SAMPLE5);
60 }
61
62
63 /** %jp{適当な時間待つ} */
64 void rand_wait(void)
65 {
66         dly_tsk(rand() % 1000 + 1);
67 }
68
69
70 /** %jp{状態表示} */
71 void print_state(int num, char *text)
72 {
73         T_PRINT_MSG *msg;
74         VP  mem;
75         
76         /* %jp{メモリ取得} */
77         get_mpf(mpfid, &mem);
78         msg = (T_PRINT_MSG *)mem;
79
80         /* %jp{文字列生成} */
81         msg->text[0] = '0' + num;
82         msg->text[1] = ' ';
83         msg->text[2] = ':';
84         msg->text[3] = ' ';
85         strcpy(&msg->text[4], text);
86         strcat(msg->text, "\n");
87         
88         /* %jp{表示タスクに送信} */
89         snd_mbx(mbxid, (T_MSG *)msg);
90 }
91
92
93 /** %jp{サンプルタスク} */
94 void Sample_Task(VP_INT exinf)
95 {
96         int num;
97         
98         num = (int)exinf;
99         
100         /* %jp{いわゆる哲学者の食事の問題} */
101         for ( ; ; )
102         {
103                 /* %jp{適当な時間考える} */
104                 print_state(num, "thinking");
105                 rand_wait();
106                 
107                 /* %jp{左右のフォークを取るまでループ} */
108                 for ( ; ; )
109                 {
110                         /* %jp{左から順に取る} */
111                         wai_sem(LEFT(num));
112                         if ( pol_sem(RIGHT(num)) == E_OK )
113                         {
114                                 break;  /* %jp{両方取れた} */
115                         }
116                         sig_sem(LEFT(num));     /* %jp{取れなければ離す} */
117                         
118                         /* %jp{適当な時間待つ} */
119                         print_state(num, "hungry");
120                         rand_wait();
121
122                         /* %jp{右から順に取る} */
123                         wai_sem(RIGHT(num));
124                         if ( pol_sem(LEFT(num)) == E_OK )
125                         {
126                                 break;  /* %jp{両方取れた} */
127                         }
128                         sig_sem(RIGHT(num));    /* %jp{取れなければ離す} */
129
130                         /* %jp{適当な時間待つ} */
131                         print_state(num, "hungry");
132                         rand_wait();
133                 }
134                 
135                 /* %jp{適当な時間、食べる} */
136                 print_state(num, "eating");
137                 rand_wait();
138                 
139                 /* %jp{フォークを置く} */
140                 sig_sem(LEFT(num));
141                 sig_sem(RIGHT(num));
142         }
143 }
144
145
146 /** %jp{表示タスク} */
147 void Sample_Print(VP_INT exinf)
148 {
149         T_PRINT_MSG *msg;
150         
151         for ( ; ; )
152         {
153                 rcv_mbx(mbxid, (T_MSG **)&msg);
154                 Sci1_PutString(msg->text);
155                 rel_mpf(mpfid, msg);
156         }
157 }
158
159
160
161 /* end of file */