Example of Event Flags APIs-Synchronization and Communication functions

  1 /*
  2  *        main.c 
  3  *  To demonstrate the use of event flag 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 flg_id = -1;
 17 
 18 /*****************************************************************
 19 task 1 wait for event flg.
 20  ****************************************************************/
 21 IMPORT void
 22 task1 (INT stacd, VP exinf)
 23 {
 24   int cnt = 0;
 25   ER ercd = 0;
 26   UINT p_flg;
 27 
 28   printf ("This is task 1 with TaskID %d \n", taskid1);
 29   printf ("Wait for flag pattern: 1\n");
 30 
 31   ercd = tk_wai_flg (flg_id, 0x0001, TWF_ANDW | TWF_BITCLR, &p_flg, TMO_FEVR);
 32   if (ercd < 0)
 33     {
 34       printf ("wait flag error %x", ercd);
 35     }
 36   else
 37     {
 38       printf ("flag pattern achieved: %x\n", p_flg);
 39     }
 40 
 41   tk_del_flg (flg_id);
 42   printf ("task 1 exit and del task, flg now\n");
 43   taskid1 = -1;
 44 
 45   flg_id = -1;
 46   tk_exd_tsk ();
 47 }
 48 
 49 
 50 
 51 /*****************************************************************
 52 task 2 setting flag
 53  ****************************************************************/
 54 IMPORT void
 55 task2 (INT stacd, VP exinf)
 56 {
 57   ER ercd = 0;
 58 
 59   printf ("\tThis is task 2 with TaskID %d, going for 5 loop\n", taskid2);
 60   while (cnt < 5)
 61     {
 62       printf ("Event flag will be set when cnt=5 ; Now cnt=%d \n", ++cnt);
 63       tk_dly_tsk (2000);
 64     }
 65   ercd = tk_set_flg (flg_id, 0x0001);
 66   if (ercd < 0)
 67     {
 68       printf ("\tset flag error %x", ercd);
 69     }
 70   else
 71     {
 72       printf ("\t Flag Pattern Set\n");
 73     }
 74 
 75   printf ("\ttask 2 exit and del task now\n");
 76   taskid2 = -1;
 77   tk_exd_tsk ();
 78 }
 79 
 80 
 81 /******************************************************************************
 82 main
 83 ******************************************************************************/
 84 EXPORT ER
 85 main (INT ac, UB * av[])
 86 {
 87   T_CTSK ctsk;
 88   T_CFLG flg;
 89   ER ercd = 0;
 90 
 91 #ifdef DEBUG
 92   tm_monitor ();
 93 #endif
 94   printf ("main: (ac=%d)\n", ac);
 95 
 96   if (ac < 0)
 97     {
 98       if (taskid1 >= 0)
 99         {
100           tk_ter_tsk (taskid1);
101           tk_del_tsk (taskid1);
102         }
103       if (taskid2 >= 0)
104         {
105           tk_ter_tsk (taskid2);
106           tk_del_tsk (taskid2);
107         }
108       goto ext;
109     }
110 
111   flg.exinf = (VP) 0x00000000;
112   flg.flgatr = TA_TFIFO | TA_FIRST;
113   flg.iflgptn = 0x0000;
114   flg_id = tk_cre_flg (&flg);
115   printf ("tk_cre_flg: (flg id = %d)\n", flg_id);
116   ercd = tk_clr_flg (flg_id, 0x0000);
117   if (ercd < 0)
118     {
119       goto ext;
120     }
121 
122   ctsk.exinf = (VP) 0x74736574;
123   ctsk.tskatr = TA_HLNG | TA_RNG0;
124   ctsk.task = task1;
125   ctsk.itskpri = 80;
126   ctsk.stksz = 1024 * 4;
127   taskid1 = tk_cre_tsk (&ctsk);
128   printf ("tk_cre_tsk: (task1id = %d)\n", taskid1);
129   if (taskid1 < E_OK)
130     {
131       tk_del_flg (flg_id);
132       goto ext;
133     }
134 
135   ctsk.exinf = (VP) 0x74736574;
136   ctsk.tskatr = TA_HLNG | TA_RNG0;
137   ctsk.task = task2;
138   ctsk.itskpri = 80;
139   ctsk.stksz = 1024 * 4;
140   taskid2 = tk_cre_tsk (&ctsk);
141   printf ("tk_cre_tsk: (task2id = %d)\n", taskid2);
142   if (taskid2 < E_OK)
143     {
144       tk_del_flg (flg_id);
145       tk_del_tsk (taskid1);
146       goto ext;
147     }
148 
149   printf ("start tasks now\n");
150   tk_sta_tsk (taskid1, 0);
151 
152   tk_sta_tsk (taskid2, 0);
153 
154   tk_dly_tsk (15000);
155   /*end */
156 ext:
157   printf ("main ended\n\n");
158   return 0;
159 }

Comments

Click here to Post a Comment