Example of Variable-Size Memory Pool Management APIs

  1 /*
  2  *        main.c 
  3  *        To demonstrate APIs related to mpl
  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 mem_id = -1;
 17 VP buf = 0;
 18 
 19 /******************************************************************************
 20 task1
 21 ******************************************************************************/
 22 IMPORT void
 23 task1 (INT stacd, VP exinf)
 24 {
 25   ER ercd = 0;
 26 
 27   printf ("This is task-1 delaying itself\n");
 28   tk_dly_tsk (5000);
 29 
 30 
 31   ercd = tk_rel_mpl (mem_id, (VP) buf);
 32   if (ercd < 0)
 33     {
 34       printf ("rel mpl error %x\n", ercd);
 35       tk_del_mpl (mem_id);
 36       goto exit;
 37     }
 38   ercd = tk_del_mpl (mem_id);
 39   if (ercd < 0)
 40     {
 41       printf ("del mpl error %x\n", ercd);
 42       goto exit;
 43     }
 44 exit:
 45   printf ("task 1 exit and del task, mbx, mpl now\n");
 46   mem_id = -1;
 47   taskid1 = -1;
 48   tk_exd_tsk ();
 49 }
 50 
 51 
 52 
 53 /******************************************************************************
 54 task2
 55  *****************************************************************************/
 56 IMPORT void
 57 task2 (INT stacd, VP exinf)
 58 {
 59   T_CMPL mempool;
 60   ER ercd = 0;
 61 
 62   mempool.exinf = (VP) 0x00000000;
 63   mempool.mplatr = TA_TFIFO | TA_RNG0;
 64   mempool.mplsz = (INT) 4;
 65   mem_id = tk_cre_mpl (&mempool);
 66   if (mem_id < 0)
 67     {
 68       printf ("\tcre var mem pool fails error %x\n", mem_id);
 69       goto exit;
 70     }
 71   ercd = tk_get_mpl (mem_id, mempool.mplsz, (VP *) & buf, TMO_FEVR);
 72   if (ercd < 0)
 73     {
 74       printf ("\tget var mem pool fails error %x\n", ercd);
 75       tk_del_mpl (mem_id);
 76       goto exit;
 77     }
 78   printf ("\taddr = %x\n", buf);
 79   if (ercd < 0)
 80     {
 81       printf ("\tsnd mbx fails error %x\n", ercd);
 82       tk_rel_mpl (mem_id, (VP) & buf);
 83       tk_del_mpl (mem_id);
 84     }
 85 exit:
 86   printf ("\ttask 2 exit and del task now\n");
 87   taskid2 = -1;
 88   tk_exd_tsk ();
 89 }
 90 
 91 
 92 /******************************************************************************
 93 main
 94 ******************************************************************************/
 95 EXPORT ER
 96 main (INT ac, UB * av[])
 97 {
 98   T_CTSK ctsk;
 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 
120   ctsk.exinf = (VP) 0x74736574;
121   ctsk.tskatr = TA_HLNG | TA_RNG0;
122   ctsk.task = task1;
123   ctsk.itskpri = 80;
124   ctsk.stksz = 1024 * 4;
125   taskid1 = tk_cre_tsk (&ctsk);
126   printf ("tk_cre_tsk: (task1id = %d)\n", taskid1);
127   if (taskid1 < E_OK)
128     {
129       printf ("cre tsk 1 fails = %x\n", taskid1);
130       goto ext;
131     }
132 
133   ctsk.exinf = (VP) 0x74736574;
134   ctsk.tskatr = TA_HLNG | TA_RNG0;
135   ctsk.task = task2;
136   ctsk.itskpri = 80;
137   ctsk.stksz = 1024 * 4;
138   taskid2 = tk_cre_tsk (&ctsk);
139   printf ("tk_cre_tsk: (task2id = %d)\n", taskid2);
140   if (taskid2 < E_OK)
141     {
142       tk_del_tsk (taskid1);
143       printf ("cre tsk 2 fails = %x\n", taskid2);
144       goto ext;
145     }
146 
147   printf ("start tasks now\n");
148   tk_sta_tsk (taskid1, 0);
149 
150   tk_sta_tsk (taskid2, 0);
151 
152   tk_dly_tsk (10000);
153   /*end */
154 ext:
155   printf ("main ended\n\n");
156   return 0;
157 }

Comments

Click here to Post a Comment