home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / Tasks / Tasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-07  |  9.0 KB  |  332 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/memory.h>
  5. #include <powerup/ppclib/interface.h>
  6. #include <powerup/ppclib/semaphore.h>
  7. #include <powerup/ppclib/message.h>
  8. #include <powerup/ppclib/tasks.h>
  9. #include <powerup/gcclib/powerup_protos.h>
  10. #include <stdio.h>
  11.  
  12. /* This Taskcounter isn`t that smart
  13.  */
  14. ULONG    TaskCounter=0;
  15. void    *Semaphore;
  16.  
  17. #define    TASKA_MSG    "TaskA Good Morning"
  18. #define    TASKB_MSG    "TaskB Good Morning"
  19. #define    TASKC_MSG    "TaskC Good Morning"
  20. #define    TASKD_MSG    "TaskD Good Morning"
  21.  
  22.  
  23. void    TaskA(void);
  24. void    TaskB(void);
  25. void    TaskC(void);
  26. void    TaskD(void);
  27.  
  28.  
  29. int    main(void)
  30. {
  31. void        *Msg;
  32. void        *ReplyPort;
  33. void        *StartUpMsgA;
  34. void        *StartUpMsgB;
  35. void        *StartUpMsgC;
  36. void        *StartUpMsgD;
  37. void        *TaskObjectA;
  38. void        *TaskObjectB;
  39. void        *TaskObjectC;
  40. void        *TaskObjectD;
  41. struct TagItem    MyTags[10];
  42. ULONG        MsgCounter;
  43. ULONG        TaskStartCounter;
  44. int        Status;
  45.  
  46.   Status        =    20;
  47.  
  48.   MyTags[0].ti_Tag    =    TAG_DONE;
  49.   if ((Semaphore=PPCCreateSemaphore(MyTags)))
  50.   {
  51.     MyTags[0].ti_Tag    =    TAG_DONE;
  52.     if (ReplyPort = PPCCreatePort(MyTags))
  53.     {
  54.       if (StartUpMsgA = PPCCreateMessage(ReplyPort, sizeof(TASKA_MSG)))
  55.       {
  56.         if (StartUpMsgB = PPCCreateMessage(ReplyPort, sizeof(TASKB_MSG)))
  57.         {
  58.           if (StartUpMsgC = PPCCreateMessage(ReplyPort, sizeof(TASKC_MSG)))
  59.           {
  60.             if (StartUpMsgD = PPCCreateMessage(ReplyPort, sizeof(TASKD_MSG)))
  61.             {
  62.               /* Wait for 4 Startup Msg replies */
  63.               MsgCounter    =    4;
  64.               TaskStartCounter=    0;
  65.  
  66.               MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  67.               MyTags[0].ti_Data    =(ULONG) StartUpMsgA;
  68.  
  69.               MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  70.               MyTags[1].ti_Data    =(ULONG) TASKA_MSG;
  71.  
  72.               MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  73.               MyTags[2].ti_Data    =    sizeof(TASKD_MSG);
  74.  
  75.               MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  76.               MyTags[3].ti_Data    =    0;
  77.  
  78.               MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  79.               MyTags[4].ti_Data    =    TRUE;
  80.  
  81.               MyTags[5].ti_Tag    =    TAG_DONE;
  82.  
  83.               if (TaskObjectA=PPCCreateTask(NULL,
  84.                                             &TaskA,
  85.                                             MyTags))
  86.               {
  87.                 TaskStartCounter++;
  88.                 PPCprintf("TaskObjectA 0x%lx created...\n",
  89.                           TaskObjectA);
  90.  
  91.                 MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  92.                 MyTags[0].ti_Data    =(ULONG) StartUpMsgB;
  93.  
  94.                 MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  95.                 MyTags[1].ti_Data    =(ULONG) TASKB_MSG;
  96.  
  97.                 MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  98.                 MyTags[2].ti_Data    =    sizeof(TASKD_MSG);
  99.  
  100.                 MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  101.                 MyTags[3].ti_Data    =    0;
  102.  
  103.                 MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  104.                 MyTags[4].ti_Data    =    TRUE;
  105.  
  106.                 MyTags[5].ti_Tag    =    TAG_DONE;
  107.  
  108.  
  109.                 if (TaskObjectB=PPCCreateTask(NULL,
  110.                                               &TaskB,
  111.                                               MyTags))
  112.                 {
  113.                   TaskStartCounter++;
  114.                   PPCprintf("TaskObjectB 0x%lx created...\n",
  115.                             TaskObjectB);
  116.  
  117.  
  118.                   MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  119.                   MyTags[0].ti_Data    =(ULONG) StartUpMsgC;
  120.  
  121.                   MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  122.                   MyTags[1].ti_Data    =(ULONG) TASKC_MSG;
  123.  
  124.                   MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  125.                   MyTags[2].ti_Data    =    sizeof(TASKD_MSG);
  126.  
  127.                   MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  128.                   MyTags[3].ti_Data    =    0;
  129.  
  130.                   MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  131.                   MyTags[4].ti_Data    =    TRUE;
  132.  
  133.                   MyTags[5].ti_Tag    =    TAG_DONE;
  134.  
  135.                   if (TaskObjectC=PPCCreateTask(NULL,
  136.                                                 &TaskC,
  137.                                                 MyTags))
  138.                   {
  139.                     TaskStartCounter++;
  140.                     PPCprintf("TaskObjectC 0x%lx created...\n",
  141.                               TaskObjectC);
  142.  
  143.                     MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  144.                     MyTags[0].ti_Data    =(ULONG) StartUpMsgD;
  145.  
  146.                     MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  147.                     MyTags[1].ti_Data    =(ULONG) TASKD_MSG;
  148.  
  149.                     MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  150.                     MyTags[2].ti_Data    =    sizeof(TASKD_MSG);
  151.  
  152.                     MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  153.                     MyTags[3].ti_Data    =    0;
  154.  
  155.                     MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  156.                     MyTags[4].ti_Data    =    TRUE;
  157.  
  158.                     MyTags[5].ti_Tag    =    TAG_DONE;
  159.  
  160.                     if (TaskObjectD=PPCCreateTask(NULL,
  161.                                                   &TaskD,
  162.                                                   MyTags))
  163.                     {
  164.                       TaskStartCounter++;
  165.                       PPCprintf("TaskObjectD 0x%lx created...\n",
  166.                                 TaskObjectD);
  167.  
  168.                       while (TaskCounter<TaskStartCounter);
  169.  
  170.  
  171.                       PPCprintf("Waiting for Task Finish Msg...\n");
  172.                       for (;;)
  173.                       {
  174.                         Msg    =    PPCGetMessage(ReplyPort);
  175.                         if ((Msg == StartUpMsgA) ||
  176.                             (Msg == StartUpMsgB) ||
  177.                             (Msg == StartUpMsgC) ||
  178.                             (Msg == StartUpMsgD))
  179.                         {
  180.                           PPCprintf("Got StartUp Msg 0x%lx..\n",
  181.                                     Msg);
  182.                           if (--MsgCounter <= 0)
  183.                           {
  184.                             break;
  185.                           }
  186.                         }
  187.                         else
  188.                         {
  189.                           PPCprintf("Some non replied or empty Msg 0x%lx was found..wait\n",
  190.                                     Msg);
  191.                           PPCWaitPort(ReplyPort);
  192.                         }
  193.                       }
  194.  
  195.                       Status    =    0;
  196.                     }
  197.                     else
  198.                     {
  199.                       PPCprintf("Couldn`t Start Task D\n");
  200.                     }
  201.                   }
  202.                   else
  203.                   {
  204.                     PPCprintf("Couldn`t Start Task C\n");
  205.                   }
  206.                 }
  207.                 else
  208.                 {
  209.                   PPCprintf("Couldn`t Start Task B\n");
  210.                 }
  211.               }
  212.               else
  213.               {
  214.                 PPCprintf("Couldn`t Start Task A\n");
  215.               }
  216.  
  217.               /* Some trick to make sure the PPC tasks have been gone
  218.                * This could be an alternative way to check for the exit
  219.                * if you don`t wanna use the startup msg reply.
  220.                */
  221.               PPCObtainSemaphore(Semaphore);
  222.               PPCReleaseSemaphore(Semaphore);
  223.  
  224.               PPCDeleteMessage(StartUpMsgD);
  225.             }
  226.             else
  227.             {
  228.               PPCprintf("Couldn`t create StartUpMsgD\n");
  229.             }
  230.             PPCDeleteMessage(StartUpMsgC);
  231.           }
  232.           else
  233.           {
  234.             PPCprintf("Couldn`t create StartUpMsgC\n");
  235.           }
  236.           PPCDeleteMessage(StartUpMsgB);
  237.         }
  238.         else
  239.         {
  240.           PPCprintf("Couldn`t create StartUpMsgB\n");
  241.         }
  242.         PPCDeleteMessage(StartUpMsgA);
  243.       }
  244.       else
  245.       {
  246.         PPCprintf("Couldn`t create StartUpMsgA\n");
  247.       }
  248.  
  249.       while (PPCDeletePort(ReplyPort) == FALSE);
  250.     }
  251.     PPCDeleteSemaphore(Semaphore);
  252.   }
  253.   else
  254.   {
  255.     PPCprintf("Couldn`t create Semaphore\n");
  256.   }
  257.   return(Status);
  258. }
  259.  
  260.  
  261. void    TaskA(void)
  262. {
  263. int    i;
  264.  
  265.   PPCObtainSemaphoreShared(Semaphore);
  266.   TaskCounter++;
  267.  
  268.   PPCprintf("TaskA StartUp Msg %s\n",
  269.             PPCGetTaskAttr(PPCTASKTAG_STARTUP_MSGDATA));
  270.  
  271.   for (i=0;i<200;i++)
  272.   {
  273.     PPCprintf("TaskA: %ld\n",
  274.               i);
  275.   }
  276.   PPCReleaseSemaphore(Semaphore);
  277. }
  278.  
  279. void    TaskB(void)
  280. {
  281. int    i;
  282.  
  283.   PPCObtainSemaphoreShared(Semaphore);
  284.   TaskCounter++;
  285.  
  286.   PPCprintf("TaskB StartUp Msg %s\n",
  287.             PPCGetTaskAttr(PPCTASKTAG_STARTUP_MSGDATA));
  288.  
  289.   for (i=0;i<200;i++)
  290.   {
  291.     PPCprintf("TaskB: %ld\n",
  292.               i);
  293.   }
  294.   PPCReleaseSemaphore(Semaphore);
  295. }
  296.  
  297. void    TaskC(void)
  298. {
  299. int    i;
  300.  
  301.   PPCObtainSemaphoreShared(Semaphore);
  302.   TaskCounter++;
  303.  
  304.   PPCprintf("TaskC StartUp Msg %s\n",
  305.             PPCGetTaskAttr(PPCTASKTAG_STARTUP_MSGDATA));
  306.  
  307.   for (i=0;i<200;i++)
  308.   {
  309.     PPCprintf("TaskC: %ld\n",
  310.               i);
  311.   }
  312.   PPCReleaseSemaphore(Semaphore);
  313. }
  314.  
  315. void    TaskD(void)
  316. {
  317. int    i;
  318.  
  319.   PPCObtainSemaphoreShared(Semaphore);
  320.   TaskCounter++;
  321.  
  322.   PPCprintf("TaskD StartUp Msg %s\n",
  323.             PPCGetTaskAttr(PPCTASKTAG_STARTUP_MSGDATA));
  324.  
  325.   for (i=0;i<200;i++)
  326.   {
  327.     PPCprintf("TaskD: %ld\n",
  328.               i);
  329.   }
  330.   PPCReleaseSemaphore(Semaphore);
  331. }
  332.