1 /*
2 * Project: Task dependent Synchronization APIs
3 * Description: Code to demonstrate API's- slp, wup
4 * File: main.c
5 *
6 * (c) Viometrix Private Limited and T-Engine Developer Network (TEDN), 2008 - 2009.
7 *
8 */
9
10 #include <basic.h>
11 #include <tk/tkernel.h>
12 #include <stdio.h>
13
14 IMPORT void task1 (INT, VP);
15 IMPORT void task2 (INT, VP);
16 IMPORT void task3 (INT, VP);
17
18 ID taskid1 = -1;
19 ID taskid2 = -1;
20
21 /* main: main function to launch the tasks
22 * ac >= 0 : launch the application
23 * ac < 0 : unload the application
24 */
25 EXPORT ER
26 main (INT ac, UB * av[])
27 {
28 T_CTSK ctsk;
29 ER ercd = 0;
30
31 printf ("This code demonstrates basic task sync APIs:\n slp, wup\n");
32
33 if (ac < 0)
34 {
35 if (taskid1 >= 0)
36 {
37 ercd = tk_ter_tsk (taskid1);
38 if (ercd < 0)
39 printf ("tk_ter_tsk err = %x\n", ercd);
40 ercd = tk_del_tsk (taskid1);
41 if (ercd < 0)
42 printf ("tk_del_tsk err = %x\n", ercd);
43 }
44 goto ext;
45 }
46
47 /* ---------------- TASK 1 --------------- */
48 /* Set up the context for the task */
49 ctsk.exinf = (VP) 0x00; /* No extended information */
50 ctsk.tskatr = TA_HLNG | TA_RNG0; /* Run the task at Ring 0 - doesn't matter without MMU */
51 ctsk.task = task1; /* start address of the task */
52 ctsk.itskpri = 80; /* task initial priority */
53 ctsk.stksz = 8192; /* stack size */
54
55 /* Create the task */
56 taskid1 = tk_cre_tsk (&ctsk);
57 if (taskid1 < E_OK)
58 { /* task creation failed */
59 printf ("Task creation failed. Error code = %x\n", ercd);
60 goto exit_point;
61 }
62
63 printf ("Task 1 created. Task ID = %d.\n", taskid1);
64
65 /* Launch the task */
66 printf ("Launching Task 1 now.\n");
67 tk_sta_tsk (taskid1, 0);
68
69 /* ---------------- TASK 2 --------------- */
70 /* Set up the context for the task */
71 ctsk.exinf = (VP) 0x00; /* No extended information */
72 ctsk.tskatr = TA_HLNG | TA_RNG0; /* Run the task at Ring 0 - doesn't matter without MMU */
73 ctsk.task = task2; /* start address of the task */
74 ctsk.itskpri = 90; /* task initial priority */
75 ctsk.stksz = 4096; /* stack size */
76
77 /* Create the task */
78 taskid2 = tk_cre_tsk (&ctsk);
79 if (taskid2 < E_OK)
80 { /* task creation failed */
81 printf ("Task creation failed. Error code = %x\n", ercd);
82 goto exit_point;
83 }
84
85 printf ("Task 2 created. Task ID = %d.\n", taskid2);
86
87 /* Launch the task */
88 printf ("Launching Task 2 now.\n");
89 tk_sta_tsk (taskid2, 0);
90
91 tk_dly_tsk (30000);
92
93
94 //Terminating and deleting Task-1
95 ercd = tk_ter_tsk (taskid1);
96 if (ercd < 0)
97 {
98 printf ("Cannot terminate Task-1");
99 goto exit_point;
100 }
101 else
102 printf ("Task-1 Terminated \n");
103
104 ercd = tk_del_tsk (taskid1);
105 if (ercd < 0)
106 {
107 printf ("Cannot delete Task-1");
108 goto exit_point;
109 }
110 else
111 printf ("Task-1 deleted \n");
112
113
114 exit_point:
115 ext:
116 printf ("main ended\n");
117 return 0;
118 }
119
120 /*****************************************************************
121 task1- Waiting forever-Wokenup by task-2 every 5 seconds
122 ****************************************************************/
123 IMPORT void
124 task1 (INT stacd, VP exinf)
125 {
126
127 printf ("This is task 1-waiting forever\n");
128 while (1)
129 {
130 tk_slp_tsk (-1);
131 printf ("Task-1 Waken up\n");
132 }
133 }
134
135 /*****************************************************************
136 task2 wakes up task-1 every 5 seconds
137 Exits and deletes itself- after 3 iterations
138 ****************************************************************/
139 IMPORT void
140 task2 (INT stacd, VP exinf)
141 {
142 int cnt = 0;
143 while (cnt < 3)
144 {
145 printf
146 ("Task 2-waking up task-1 evry 5 sec, till cnt=3, Now cnt = %d\n",
147 ++cnt);
148 tk_dly_tsk (5000);
149 tk_wup_tsk (taskid1);
150 }
151 printf ("Task-2 exits and del now\n");
152 taskid2 = -1;
153
154 //Quit!
155 tk_exd_tsk ();
156 }
Comments
Click here to Post a Comment