Example of Rendezvous Port APIs-Extended Synchronization and Communication functions

  1 /*
  2  *        main.c 
  3  *        To demonstrate APIs related to the use of rendezvous 
  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 por_id = -1;
 17 
 18 /*****************************************************************
 19 task1 
 20  ****************************************************************/
 21 IMPORT void
 22 task1 (INT stacd, VP exinf)
 23 {
 24   INT cnt = 0;
 25   INT rmsgsz = 0;
 26   ER ercd = 0;
 27 
 28   printf ("This is task %d, going for 5 loop\n", taskid1);
 29   while (cnt < 5)
 30     {
 31       printf ("task %d try call port\n", taskid1);
 32       rmsgsz =
 33         tk_cal_por (por_id, 0x00000003, (VP) & cnt, sizeof (INT), TMO_FEVR);
 34       if (rmsgsz < 0)
 35         {
 36           printf ("tk_cal_por error %x\n", rmsgsz);
 37           break;
 38         }
 39       printf ("reply port received cnt = %d\n", cnt);
 40       tk_dly_tsk (350);
 41     }
 42   ercd = tk_del_por (por_id);
 43   if (ercd < 0)
 44     {
 45       printf ("tk_del_por error %x\n", ercd);
 46     }
 47   printf ("task 1 exit and del task, port now\n");
 48   taskid1 = -1;
 49   por_id = -1;
 50   tk_exd_tsk ();
 51 }
 52 
 53 
 54 /****************************************************************
 55 task 2
 56  ****************************************************************/
 57 IMPORT void
 58 task2 (INT stacd, VP exinf)
 59 {
 60   INT cnt = 0;
 61   ER ercd = 0;
 62   RNO rdvno = 0;
 63   INT cmsgsz = 0;
 64 
 65   printf ("\tThis is task %d, going for 5 loop\n", taskid2);
 66   while (cnt < 5)
 67     {
 68       printf ("\ttask %d try accept port\n", taskid2);
 69       cmsgsz = tk_acp_por (por_id, 0x00000003, &rdvno, (VP) & cnt, TMO_FEVR);
 70       if (cmsgsz < 0)
 71         {
 72           printf ("\ttk_acp_por error = %d\n", cmsgsz);
 73           break;
 74         }
 75       printf ("\tpor accepted cnt = %d\n", cnt++);
 76       tk_dly_tsk (600);
 77       printf ("\ttask 2 delay over reply port cnt = %d\n", cnt);
 78       ercd = tk_rpl_rdv (rdvno, (VP) & cnt, sizeof (INT));
 79       if (ercd < 0)
 80         {
 81           printf ("\ttk_rpl_rdv error %x\n", ercd);
 82           break;
 83         }
 84     }
 85   printf ("\ttask 2 exit and del task now\n");
 86   taskid2 = -1;
 87   tk_exd_tsk ();
 88 }
 89 
 90 
 91 /******************************************************************************
 92 main
 93 ******************************************************************************/
 94 EXPORT ER
 95 main (INT ac, UB * av[])
 96 {
 97   T_CTSK ctsk;
 98   T_CPOR por;
 99 
100 #ifdef DEBUG
101   tm_monitor ();
102 #endif
103   printf ("main: (ac=%d)\n", ac);
104 
105   if (ac < 0)
106     {
107       if (taskid1 >= 0)
108         {
109           tk_ter_tsk (taskid1);
110           tk_del_tsk (taskid1);
111         }
112       if (taskid2 >= 0)
113         {
114           tk_ter_tsk (taskid2);
115           tk_del_tsk (taskid2);
116         }
117       goto ext;
118     }
119   por.exinf = (VP) 0x00000000;
120   por.poratr = TA_TFIFO;
121   por.maxcmsz = sizeof (INT);
122   por.maxrmsz = sizeof (INT);
123   por_id = tk_cre_por (&por);
124   printf ("tk_cre_por: (por id = %d)\n", por_id);
125   if (por_id < E_OK)
126     goto ext;
127 
128   ctsk.exinf = (VP) 0x74736574;
129   ctsk.tskatr = TA_HLNG | TA_RNG0;
130   ctsk.task = task1;
131   ctsk.itskpri = 80;
132   ctsk.stksz = 1024 * 4;
133   taskid1 = tk_cre_tsk (&ctsk);
134   printf ("tk_cre_tsk: (task1id = %d)\n", taskid1);
135   if (taskid1 < E_OK)
136     {
137       tk_del_por (por_id);
138       printf ("main: cre_tsk 1 fails\n");
139       goto ext;
140     }
141 
142   ctsk.exinf = (VP) 0x74736574;
143   ctsk.tskatr = TA_HLNG | TA_RNG0;
144   ctsk.task = task2;
145   ctsk.itskpri = 80;
146   ctsk.stksz = 1024 * 4;
147   taskid2 = tk_cre_tsk (&ctsk);
148   printf ("tk_cre_tsk: (task2id = %d)\n", taskid2);
149   if (taskid2 < E_OK)
150     {
151       tk_del_por (por_id);
152       tk_del_tsk (taskid1);
153       printf ("main: cre_tsk 2 fails\n");
154       goto ext;
155     }
156 
157   printf ("start tasks now\n");
158   tk_sta_tsk (taskid1, 0);
159   tk_sta_tsk (taskid2, 0);
160   tk_dly_tsk (10000);
161 
162   /*end */
163 ext:
164   printf ("main ended\n\n");
165   return 0;
166 }

Comments

Click here to Post a Comment