Example of Mutex APIs-Extended Synchronization and Communication functions

  1 /*
  2  *        main.c 
  3  *
  4  *        To demonstrate the use of mutex APIs 
  5  */
  6 
  7 #include <basic.h>
  8 #include <tk/tkernel.h>
  9 #include <stdio.h>
 10 #ifdef DEBUG
 11 #include <util/tmonitor.h>
 12 #endif
 13 
 14 IMPORT void task1 (INT, VP);
 15 ID taskid1 = -1;
 16 ID taskid2 = -1;
 17 ID mtx_id = -1;
 18 ID flg_id = -1;
 19 
 20 /*****************************************************************
 21 task1 and task 2 trying to acquire resource
 22  ****************************************************************/
 23 IMPORT void
 24 task1 (INT stacd, VP exinf)
 25 {
 26   int cnt = 0;
 27   ER ercd = 0;
 28   T_RTSK tsk_info;
 29 
 30   printf ("This is task %d, going for 3 loop\n", taskid1);
 31   while (cnt < 3)
 32     {
 33       printf ("task %d try lock mtx\n", taskid1);
 34       ercd = tk_loc_mtx (mtx_id, TMO_FEVR);
 35       if (ercd < 0 && ercd != E_ILUSE)
 36         {
 37           printf ("loc mtx fails %x\n", ercd);
 38           tk_dly_tsk (100);
 39           continue;
 40         }
 41       printf ("enter critical section task %d: cnt = %d\n", taskid1, cnt++);
 42       tk_dly_tsk (2000);
 43       tk_ref_tsk (taskid1, &tsk_info);
 44       printf ("task id = %d pri = %d base pri = %d\n",
 45               taskid1, tsk_info.tskpri, tsk_info.tskbpri);
 46       tk_ref_tsk (taskid2, &tsk_info);
 47       printf ("task id = %d pri = %d base pri = %d\n",
 48               taskid2, tsk_info.tskpri, tsk_info.tskbpri);
 49       printf ("delay task 1 over leave critical section\n");
 50       tk_unl_mtx (mtx_id);
 51     }
 52   tk_del_mtx (mtx_id);
 53 
 54   printf ("task 1 exit and del task, mutex now\n");
 55   taskid1 = -1;
 56   mtx_id = -1;
 57 
 58   tk_exd_tsk ();
 59 }
 60 
 61 
 62 /*****************************************************************
 63 task1 and task 2 trying to acquire resource
 64  ****************************************************************/
 65 IMPORT void
 66 task2 (INT stacd, VP exinf)
 67 {
 68   int cnt = 0;
 69   ER ercd = 0;
 70   T_RTSK tsk_info;
 71 
 72   printf ("\tThis is task %d, going for 3 loop\n", taskid2);
 73   while (cnt < 3)
 74     {
 75       printf ("\ttask %d try loc mtx\n", taskid2);
 76       ercd = tk_loc_mtx (mtx_id, TMO_FEVR);
 77       if (ercd < 0 && ercd != E_ILUSE)
 78         {
 79           printf ("\tloc mtx fails %x\n", ercd);
 80           tk_dly_tsk (150);
 81           continue;
 82         }
 83       printf ("\tenter critical section task %d: cnt = %d\n", taskid2, cnt++);
 84       tk_dly_tsk (2000);
 85       tk_ref_tsk (taskid1, &tsk_info);
 86       printf ("\ttask id = %d pri = %d base pri = %d\n",
 87               taskid1, tsk_info.tskpri, tsk_info.tskbpri);
 88       tk_ref_tsk (taskid2, &tsk_info);
 89       printf ("\ttask id = %d pri = %d base pri = %d\n",
 90               taskid2, tsk_info.tskpri, tsk_info.tskbpri);
 91       printf ("\tdelay task 2 over leave critical section\n");
 92       tk_unl_mtx (mtx_id);
 93     }
 94 
 95   if (ercd < 0)
 96     {
 97       printf ("\tset flag error %x", ercd);
 98     }
 99   printf ("\ttask 2 exit and del task now\n");
100   taskid2 = -1;
101   tk_exd_tsk ();
102 }
103 
104 
105 /******************************************************************************
106 main
107 ******************************************************************************/
108 EXPORT ER
109 main (INT ac, UB * av[])
110 {
111   T_CTSK ctsk;
112   T_CMTX mtx;
113 
114 #ifdef DEBUG
115   tm_monitor ();
116 #endif
117   printf ("main: (ac=%d)\n", ac);
118 
119   if (ac < 0)
120     {
121       if (taskid1 >= 0)
122         {
123           tk_ter_tsk (taskid1);
124           tk_del_tsk (taskid1);
125         }
126       if (taskid2 >= 0)
127         {
128           tk_ter_tsk (taskid2);
129           tk_del_tsk (taskid2);
130         }
131       goto ext;
132     }
133   mtx.exinf = (VP) 0x00000000;
134 #if 0
135   mtx.mtxatr = TA_TFIFO | TA_CEILING;
136 #else
137   mtx.mtxatr = TA_TFIFO | TA_INHERIT;
138 #endif
139   mtx.ceilpri = 70;
140   mtx_id = tk_cre_mtx (&mtx);
141   printf ("tk_cre_mtx: (mtx id = %d)\n", mtx_id);
142   if (mtx_id < E_OK)
143     goto ext;
144 
145   ctsk.exinf = (VP) 0x74736574;
146   ctsk.tskatr = TA_HLNG | TA_RNG0;
147   ctsk.task = task1;
148   ctsk.itskpri = 81;
149   ctsk.stksz = 1024 * 4;
150   taskid1 = tk_cre_tsk (&ctsk);
151   printf ("tk_cre_tsk: (task1id = %d)\n", taskid1);
152   if (taskid1 < E_OK)
153     {
154       tk_del_mtx (mtx_id);
155       goto ext;
156     }
157 
158   ctsk.exinf = (VP) 0x74736574;
159   ctsk.tskatr = TA_HLNG | TA_RNG0;
160   ctsk.task = task2;
161   ctsk.itskpri = 80;
162   ctsk.stksz = 1024 * 4;
163   taskid2 = tk_cre_tsk (&ctsk);
164   printf ("tk_cre_tsk: (task2id = %d)\n", taskid2);
165   if (taskid2 < E_OK)
166     {
167       tk_del_mtx (mtx_id);
168       tk_del_tsk (taskid1);
169       goto ext;
170     }
171 
172   printf ("start tasks now\n");
173   tk_sta_tsk (taskid1, 0);
174 
175   tk_sta_tsk (taskid2, 0);
176 
177   tk_dly_tsk (15000);
178   /*end */
179 ext:
180   printf ("main ended\n\n");
181   return 0;
182 }

Comments

Click here to Post a Comment