Example of Task-Dependent synchronization-Suspend,Resume

  1 /*
  2  *        Project: Task dependent synchronization 
  3  *  Description: Code to demonstrate APIs- sus, rsm
  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 sus, rsm\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 (10000);
 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- delays itself-Suspended and resumed by task-2 every 2 seconds
122  ****************************************************************/
123 IMPORT void
124 task1 (INT stacd, VP exinf)
125 {
126   while (1)
127     {
128       printf ("This is Task-1\n");
129       tk_dly_tsk (1000);
130     }
131 }
132 
133 /*****************************************************************
134 task2 
135 Exits and deletes itself- after 3 iterations
136  ****************************************************************/
137 IMPORT void
138 task2 (INT stacd, VP exinf)
139 {
140   ER ercd = 0;
141   int cnt = 0;
142   while (cnt < 3)
143     {
144       printf
145         ("Task 2-Suspending and resuming task-1 evry  sec, till cnt=3, Now cnt = %d\n",
146          ++cnt);
147       ercd = tk_sus_tsk (taskid1);
148       if (ercd == E_OK)
149         printf ("Task 1 suspended\n");
150       ercd = tk_rsm_tsk (taskid1);
151       if (ercd == E_OK)
152         printf ("Task 1 Resumed\n");
153       tk_dly_tsk (1000);
154     }
155   printf ("Task-2 exits and del now\n");
156   taskid2 = -1;
157 
158   //Quit!
159   tk_exd_tsk ();
160 }

Comments

Click here to Post a Comment