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

  1. /* Msg5 and Msg5PPC.elf
  2.  *
  3.  * This test program sends 1000 messages to the PPC and
  4.  * shows how long this takes.
  5.  * The Messages are send asynchron and it`s not waited
  6.  * for the reply...until all messages are send
  7.  * Each Message has the Body "Text sent by M68k processor\n"
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/nodes.h>
  12. #include <exec/lists.h>
  13. #include <exec/memory.h>
  14. #include <utility/tagitem.h>
  15. #include <powerup/ppclib/interface.h>
  16. #include <powerup/ppclib/message.h>
  17. #include <powerup/ppclib/tasks.h>
  18. #include <powerup/proto/ppc.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "time.h"
  24. #include "time_protos.h"
  25.  
  26. #define TEXT            "Text sent by M68k processor\n"
  27. #define    MSGCOUNT    1000
  28.  
  29. struct StartupData
  30. {
  31.     ULONG    MsgCount;
  32. };
  33.  
  34. extern struct Library    *SysBase;
  35. void            *MsgArray[MSGCOUNT];
  36.  
  37. int    main(void)
  38. {
  39. struct Library        *PPCLibBase;
  40. struct TagItem        MyTags[10];
  41. void            *PPCPort;
  42. void            *ReplyPort;
  43. void            *StartupMsg;
  44. void            *M68kMsg;
  45. void            *ElfObject;
  46. void            *Task;
  47. UBYTE            *Body;
  48. struct StartupData    *StartupData;
  49. void            *MyTimerObject;
  50. ULONG            i;
  51.  
  52.   Printf("Opening ppc.library\n");
  53.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  54.   {
  55.     if (MyTimerObject=TimerCreateObject())
  56.     {
  57.       Printf("Loading PPC object\n");
  58.       if (ElfObject=PPCLoadObject("PROGDIR:Msg5PPC.elf"))
  59.       {
  60.         Printf("Creating reply port\n");
  61.         MyTags[0].ti_Tag    =    TAG_DONE;
  62.         if (ReplyPort = PPCCreatePort(MyTags))
  63.         {
  64.           if (StartupMsg = PPCCreateMessage(ReplyPort, 0))
  65.           {
  66.             Printf("Allocating StartupData\n");
  67.             if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  68.             {
  69.               StartupData->MsgCount    =    MSGCOUNT;
  70.  
  71.               Printf("Creating PPC task\n");
  72.               MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  73.               MyTags[0].ti_Data    =(ULONG) StartupMsg;
  74.  
  75.               MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  76.               MyTags[1].ti_Data    =(ULONG) StartupData;
  77.  
  78.               MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  79.               MyTags[2].ti_Data    =    0;
  80.  
  81.               MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  82.               MyTags[3].ti_Data    =    0;
  83.  
  84.               MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  85.               MyTags[4].ti_Data    =    TRUE;
  86.  
  87.               MyTags[5].ti_Tag    =    TAG_DONE;
  88.  
  89.               if (Task = PPCCreateTask(ElfObject, MyTags))
  90.               {
  91.                 if (PPCPort=(void*) PPCGetTaskAttrsTags(Task,
  92.                                                         PPCTASKINFOTAG_MSGPORT,0,
  93.                                                         TAG_END))
  94.                 {
  95.                   Printf("Allocating memory for message body\n");
  96.                   if (Body = PPCAllocVec(sizeof(TEXT), MEMF_PUBLIC))
  97.                   {
  98.                     Printf("Creating all messages...\n");
  99.                     for (i=0;i<MSGCOUNT;i++)
  100.                     {
  101.                       if ((MsgArray[i]=PPCCreateMessage(ReplyPort,
  102.                                                         sizeof(TEXT)))==NULL)
  103.                       {
  104.                         break;
  105.                       }
  106.                     }
  107.  
  108.                     if (i>=MSGCOUNT)
  109.                     {
  110.                       strcpy(Body, TEXT);
  111.                       Printf("Sending 1000 ASYNCHRON messages with a *Body*...");
  112.  
  113.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  114.                       for (i=0;i<MSGCOUNT;i++)
  115.                       {
  116.                         PPCSendMessage(PPCPort,
  117.                                        MsgArray[i],
  118.                                        Body,
  119.                                        sizeof(TEXT),
  120.                                        0x12345678);
  121.                       }
  122.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  123.                       TimerShow(MyTimerObject);
  124.  
  125.                       Printf("Wait for 1000 message replies...");
  126.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  127.  
  128.                       i    =    0;
  129.  
  130.                       while (i<MSGCOUNT)
  131.                       {
  132.                         PPCWaitPort(ReplyPort);
  133.                         while (i<MSGCOUNT && PPCGetMessage(ReplyPort))
  134.                         {
  135.                           i++;
  136.                         }
  137.                       }
  138.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  139.                       TimerShow(MyTimerObject);
  140.  
  141.                       Printf("Waiting for Task Finish Msg...\n");
  142.                       for (;;)
  143.                       {
  144.                         if ((M68kMsg=PPCGetMessage(ReplyPort)) == StartupMsg)
  145.                         {
  146.                           Printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  147.                           break;
  148.                         }
  149.                         else
  150.                         {
  151.                           Printf("Some non replied Msg 0x%lx was found..wait\n",
  152.                                  M68kMsg);
  153.                           PPCWaitPort(ReplyPort);
  154.                         }
  155.                       }
  156.                     }
  157.                     else
  158.                     {
  159.                       Printf("Could not create all msgs\n");
  160.                     }
  161.  
  162.                     Printf("Deleting messages...\n");
  163.                     for (i=0;i<MSGCOUNT;i++)
  164.                     {
  165.                       if (MsgArray[i])
  166.                       {
  167.                         PPCDeleteMessage(MsgArray[i]);
  168.                       }
  169.                     }
  170.                     Printf("Freeing message body memory\n");
  171.                     PPCFreeVec(Body);
  172.                   }
  173.                   else
  174.                   {
  175.                     Printf("Could not alloc mem for msg body\n");
  176.                   }
  177.                   Printf("Deleting reply port...");
  178.                   while (PPCDeletePort(ReplyPort) == FALSE);
  179.                 }
  180.                 else
  181.                 {
  182.                   Printf("Could not find the PPCTask's msgport\n");
  183.                 }
  184.               }
  185.               else
  186.               {
  187.                 Printf("Could not create PPC task\n");
  188.               }
  189.               PPCFreeVec(StartupData);
  190.             }
  191.             else
  192.             {
  193.               Printf("Could not alloc Startup Data\n");
  194.             }
  195.             PPCDeleteMessage(StartupMsg);
  196.           }
  197.           else
  198.           {
  199.             Printf("Could not create Startup message\n");
  200.           }
  201.         }
  202.         else
  203.         {
  204.           Printf("Could not create reply port\n");
  205.         }
  206.         Printf("Unloading PPC object\n");
  207.         PPCUnLoadObject(ElfObject);
  208.       }
  209.       else
  210.       {
  211.         Printf("Could not load the elfobject\n");
  212.       }
  213.       TimerDeleteObject(MyTimerObject);
  214.     }
  215.     Printf("Closing ppc.library\n");
  216.     CloseLibrary(PPCLibBase);
  217.   }
  218.   else
  219.   {
  220.     Printf("Could not open ppc.library v44+\n");
  221.   }
  222.   return(0);
  223. }
  224.  
  225.