1 /*
2 * main.c
3 * To demonstrate the use of semaphore related APIs
4 */
5
6 #include <basic.h>
7 #include <tk/tkernel.h>
8 #include <stdio.h>
9 #ifdef DEBUG
10 #include <util/tmonitor.h>
11 #endif
12
13 IMPORT void task1 (INT, VP);
14 ID taskid1 = -1;
15 ID taskid2 = -1;
16 ID sem_id = -1;
17
18
19 /*****************************************************************
20 task1 and task 2 trying to acquire resource
21 task 1 wait for task 2 before rel sem .
22 ****************************************************************/
23 IMPORT void
24 task1 (INT stacd, VP exinf)
25 {
26 int cnt = 0;
27
28 printf ("This is task %d, going for 3 loop\n", taskid1);
29 while (cnt < 3)
30 {
31 printf ("task %d try get semaphore\n", taskid1);
32 tk_wai_sem (sem_id, 1, TMO_FEVR);
33 printf ("enter critical section task %d: cnt = %d\n", taskid1, cnt++);
34 tk_dly_tsk (3000);
35 printf ("delay task 1 over leave critical section\n");
36 tk_sig_sem (sem_id, 1);
37 }
38 tk_del_sem (sem_id);
39 printf ("task 1 exit and del task, semaphore now\n");
40 taskid1 = -1;
41 sem_id = -1;
42 tk_exd_tsk ();
43 }
44
45
46
47 /*****************************************************************
48 task1 and task 2 trying to acquire resource
49 ****************************************************************/
50 IMPORT void
51 task2 (INT stacd, VP exinf)
52 {
53 int cnt = 0;
54
55 printf ("\tThis is task %d, going for 3 loop\n", taskid2);
56 while (cnt < 3)
57 {
58 printf ("\ttask %d try get semaphore\n", taskid2);
59 tk_wai_sem (sem_id, 1, TMO_FEVR);
60 printf ("\tenter critical section task %d: cnt = %d\n", taskid2, cnt++);
61 tk_dly_tsk (3000);
62 printf ("\tdelay task 2 over leave critical section\n");
63 tk_sig_sem (sem_id, 1);
64 }
65
66 printf ("\ttask 2 exit and del task now\n");
67 taskid2 = -1;
68 tk_exd_tsk ();
69 }
70
71
72 /******************************************************************************
73 main
74 ******************************************************************************/
75 EXPORT ER
76 main (INT ac, UB * av[])
77 {
78 T_CTSK ctsk;
79 T_CSEM sem;
80
81 #ifdef DEBUG
82 tm_monitor ();
83 #endif
84 printf ("main: (ac=%d)\n", ac);
85
86 if (ac < 0)
87 {
88 if (taskid1 >= 0)
89 {
90 tk_ter_tsk (taskid1);
91 tk_del_tsk (taskid1);
92 }
93 if (taskid2 >= 0)
94 {
95 tk_ter_tsk (taskid2);
96 tk_del_tsk (taskid2);
97 }
98 goto ext;
99 }
100 sem.exinf = (VP) 0x00000000;
101 sem.sematr = TA_TFIFO | TA_FIRST;
102 sem.isemcnt = 1;
103 sem.maxsem = 1;
104 sem_id = tk_cre_sem (&sem);
105 printf ("tk_cre_sem: (sem id = %d)\n", sem_id);
106 if (sem_id < E_OK)
107 goto ext;
108
109
110 ctsk.exinf = (VP) 0x74736574;
111 ctsk.tskatr = TA_HLNG | TA_RNG0;
112 ctsk.task = task1;
113 ctsk.itskpri = 80;
114 ctsk.stksz = 1024 * 4;
115 taskid1 = tk_cre_tsk (&ctsk);
116 printf ("tk_cre_tsk: (task1id = %d)\n", taskid1);
117 if (taskid1 < E_OK)
118 {
119 tk_del_sem (sem_id);
120 goto ext;
121 }
122
123 ctsk.exinf = (VP) 0x74736574;
124 ctsk.tskatr = TA_HLNG | TA_RNG0;
125 ctsk.task = task2;
126 ctsk.itskpri = 80;
127 ctsk.stksz = 1024 * 4;
128 taskid2 = tk_cre_tsk (&ctsk);
129 printf ("tk_cre_tsk: (task2id = %d)\n", taskid2);
130 if (taskid2 < E_OK)
131 {
132 tk_del_sem (sem_id);
133 tk_del_tsk (taskid1);
134 goto ext;
135 }
136
137 printf ("start tasks now\n");
138 tk_sta_tsk (taskid1, 0);
139 tk_sta_tsk (taskid2, 0);
140 tk_dly_tsk (20000);
141
142 /*end */
143 ext:
144 printf ("main ended\n\n");
145 return 0;
146 }
Comments
Click here to Post a Comment