Example of basic task management APIs

  1 /*
  2  *  Description: Code to demonstrate API's- cre,sta,ext,exd,ter,del
  3  *  File: main.c
  4  *
  5  *        (c) Viometrix Private Limited and T-Engine Developer Network (TEDN), 2008 - 2009.
  6  *
  7  */
  8 
  9 #include <basic.h>
 10 #include <tk/tkernel.h>
 11 #include <stdio.h>
 12 
 13 IMPORT void task1 (INT, VP);
 14 IMPORT void task2 (INT, VP);
 15 IMPORT void task3 (INT, VP);
 16 
 17 ID taskid1 = -1;
 18 ID taskid2 = -1;
 19 ID taskid3 = -1;
 20 char *hw = "hello world";
 21 
 22 //A simple function to show the details of the task
 23 void
 24 show_task_details (ID task_id)
 25 {
 26   T_RTSK pk_rtsk;
 27   ER ercd;
 28 
 29   ercd = tk_ref_tsk (task_id, &pk_rtsk);
 30   if (ercd < E_OK)
 31     {
 32       printf ("Error in tk_ref_tsk (%d) - %08X\n", task_id, ercd);
 33       return;
 34     }
 35 
 36   //Output the details
 37   printf ("** Task Details for task_id = %d **\n", task_id);
 38   printf (" Priority: Base - %d, Current - %d\n", pk_rtsk.tskpri,
 39           pk_rtsk.tskbpri);
 40   printf (" Task State: %08X\tWait factor: %08X\n", pk_rtsk.tskstat,
 41           pk_rtsk.tskwait);
 42   printf (" Waiting object ID: %d\tQueued Wakeup Requests: %d\n", pk_rtsk.wid,
 43           pk_rtsk.wupcnt);
 44   printf (" Nested suspend Requests: %d\tSliceTime: %d\n", pk_rtsk.suscnt,
 45           pk_rtsk.slicetime);
 46   printf (" Disabled wait factors: %d\tAllowed task exceptions: %X\n",
 47           pk_rtsk.waitmask, pk_rtsk.texmask);
 48   printf (" Task Events: %08x\n", pk_rtsk.tskevent);
 49 
 50   return;
 51 }
 52 
 53 
 54 
 55 /* main: main function to launch the tasks 
 56  * ac >= 0 : launch the application
 57  * ac < 0  : unload the application
 58  */
 59 EXPORT ER
 60 main (INT ac, UB * av[])
 61 {
 62   T_CTSK ctsk;
 63   ER ercd = 0;
 64 
 65   printf
 66     ("This code demonstrates basic task management APIs:\n cre,sta,ext,exd,ter,del and ref\n");
 67 
 68   if (ac < 0)
 69     {
 70       if (taskid1 >= 0)
 71         {
 72           ercd = tk_ter_tsk (taskid1);
 73           if (ercd < 0)
 74             printf ("tk_ter_tsk err = %x\n", ercd);
 75           ercd = tk_del_tsk (taskid1);
 76           if (ercd < 0)
 77             printf ("tk_del_tsk err = %x\n", ercd);
 78         }
 79       goto ext;
 80     }
 81 
 82   /* ---------------- TASK 1 --------------- */
 83   /* Set up the context for the task */
 84   ctsk.exinf = (VP) 0x00;        /* No extended information */
 85   ctsk.tskatr = TA_HLNG | TA_RNG0;        /* Run the task at Ring 0 - doesn't matter without MMU */
 86   ctsk.task = task1;                /* start address of the task */
 87   ctsk.itskpri = 80;                /* task initial priority */
 88   ctsk.stksz = 8192;                /* stack size */
 89 
 90   /* Create the task */
 91   taskid1 = tk_cre_tsk (&ctsk);
 92   if (taskid1 < E_OK)
 93     {                                /* task creation failed */
 94       printf ("Task creation failed.  Error code = %x\n", ercd);
 95       goto exit_point;
 96     }
 97 
 98   printf ("Task 1 created.  Task ID = %d.\n", taskid1);
 99 
100   /* Launch the task */
101   printf ("Launching Task 1 now.\n");
102   tk_sta_tsk (taskid1, 0);
103 
104   /* ---------------- TASK 2 --------------- */
105   /* Set up the context for the task */
106   ctsk.exinf = (VP) (hw);        /* No extended information */
107   ctsk.tskatr = TA_HLNG | TA_RNG0;        /* Run the task at Ring 0 - doesn't matter without MMU */
108   ctsk.task = task2;                /* start address of the task */
109   ctsk.itskpri = 90;                /* task initial priority */
110   ctsk.stksz = 4096;                /* stack size */
111 
112   /* Create the task */
113   taskid2 = tk_cre_tsk (&ctsk);
114   if (taskid2 < E_OK)
115     {                                /* task creation failed */
116       printf ("Task creation failed.  Error code = %x\n", ercd);
117       goto exit_point;
118     }
119 
120   printf ("Task 2 created.  Task ID = %d.\n", taskid2);
121 
122   /* Launch the task */
123   printf ("Launching Task 2 now.\n");
124   tk_sta_tsk (taskid2, 0);
125 
126 /* ---------------- TASK 3 --------------- */
127   /* Set up the context for the task */
128   ctsk.exinf = (VP) 0x00;        /* No extended information */
129   ctsk.tskatr = TA_HLNG | TA_RNG0;        /* Run the task at Ring 0 - doesn't matter without MMU */
130   ctsk.task = task3;                /* start address of the task */
131   ctsk.itskpri = 100;                /* task initial priority */
132   ctsk.stksz = 4096;                /* stack size */
133 
134   /* Create the task */
135   taskid3 = tk_cre_tsk (&ctsk);
136   if (taskid3 < E_OK)
137     {                                /* task creation failed */
138       printf ("Task creation failed.  Error code = %x\n", ercd);
139       goto exit_point;
140     }
141 
142   printf ("Task 3 created.  Task ID = %d.\n", taskid3);
143 
144   /* Launch the task */
145   printf ("Launching Task 3 now.\n");
146   tk_sta_tsk (taskid3, 0);
147 
148   tk_slp_tsk (5000);
149 
150   show_task_details (taskid1);
151   show_task_details (taskid2);
152   show_task_details (taskid3);
153 
154   tk_dly_tsk (40000);
155 
156   // Deleting Task-1
157   ercd = tk_del_tsk (taskid1);
158   if (ercd < 0)
159     {
160       printf ("Cannot delete Task-1");
161       goto exit_point;
162     }
163   else
164     printf ("Task-1 deleted \n");
165 
166   //Terminating and deleting Task-3
167   ercd = tk_ter_tsk (taskid3);
168   if (ercd < 0)
169     {
170       printf ("Cannot terminate Task-3");
171       goto exit_point;
172     }
173   else
174     printf ("Task-3 Terminated \n");
175 
176   ercd = tk_del_tsk (taskid3);
177   if (ercd < 0)
178     {
179       printf ("Cannot delete Task-3");
180       goto exit_point;
181     }
182   else
183     printf ("Task-3 deleted \n");
184 
185 
186 exit_point:
187 ext:
188   printf ("main ended\n");
189   return 0;
190 }
191 
192 /*****************************************************************
193 task1 wakesup every 2 seconds and display print
194 Exits after 5 iterations- uses tk_ext_task
195  ****************************************************************/
196 IMPORT void
197 task1 (INT stacd, VP exinf)
198 {
199   int cnt = 0;
200   printf ("This is task 1, going for 5 loops\n");
201   while (cnt < 5)
202     {
203       tk_slp_tsk (2000);
204       printf ("This is Task-1: cnt = %d\n", cnt++);
205     }
206   printf ("Task-1 exits now\n");
207   tk_ext_tsk ();
208 }
209 
210 /*****************************************************************
211 task2 shows how to use exinf, 
212 Exits and deletes itself- uses tk_exd_tsk
213  ****************************************************************/
214 IMPORT void
215 task2 (INT stacd, VP exinf)
216 {
217   char *p;
218 
219   printf ("This is Task 2.\n");
220   /* Show how to use exinf */
221   if (exinf != NULL)
222     {
223       printf ("Message from exinf.\n");
224       p = (char *) (exinf);
225       printf ("%s\n", p);
226     }
227   printf ("Task-2 sleeping for 20 seconds\n");
228   tk_slp_tsk (20 * 1000);
229   printf ("Task-2 exits and del now\n");
230   taskid2 = -1;
231 
232   //Quit!
233   tk_exd_tsk ();
234 }
235 
236 /*****************************************************************
237 task3 runs continuously till it gets terminated by main
238  ****************************************************************/
239 IMPORT void
240 task3 (INT stacd, VP exinf)
241 {
242   while (1)
243     {
244       printf ("This is Task 3.\n");
245       tk_slp_tsk (2000);
246     }
247 }

Comments

Click here to Post a Comment