home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / Multiprocessing SDK / Sample Code / MPHelloWorld / sources / MPHelloWorld.c
Encoding:
C/C++ Source or Header  |  1996-05-03  |  3.8 KB  |  136 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <Types.h>
  3. #include <SIOUX.h>
  4. #include <MP.h>
  5.  
  6.  
  7. static void
  8. sendString(MPQueueID queue, char *string) {
  9. /*
  10.   Send the string through the queue one character at a time.
  11. */
  12.   while (*string) {
  13.     (void) MPNotifyQueue(queue, (void *) *string, 0, 0);
  14.     string++;
  15.   }
  16.  
  17.   (void) MPNotifyQueue(queue, (void *) '\0', 0, 0);            /* Send the final null character. */
  18. }
  19.  
  20.  
  21. static void
  22. receiveString(MPQueueID queue, char *string) {
  23. /*
  24.   Reassemble a string being transferred over the queue and return it to the caller.
  25. */
  26.   void
  27.     *first,            /* The first word of the message. */
  28.     *second,        /* The second word of the message. */
  29.     *third;            /* The third word of the message. */
  30.  
  31.   for (;;) {
  32.     (void) MPWaitOnQueue(queue, &first, &second, &third, kDurationForever);
  33.     *string = (char) first;
  34.  
  35.     if (*string == '\0') {
  36.       return;
  37.     }
  38.  
  39.     string++;
  40.   }
  41. }
  42.  
  43.  
  44. static OSStatus
  45. HelloWorld(void *parameter) {
  46.   MPQueueID
  47.     queue = parameter;
  48.  
  49.   sendString(queue, "Hello, World!");
  50.  
  51.   return noErr;
  52. }
  53.  
  54.  
  55. static int
  56. failure(char *annotation, char *routine, OSStatus status) {
  57.   printf("Uh oh.  %s%s%s returned %s [%d].\n",
  58.          (annotation) ? annotation : "",
  59.          (routine) ? routine : "",
  60.          (routine) ? "()" : "A routine",
  61.          _MPStatusCString(status),
  62.          status);
  63.  
  64.   return 1;
  65. }
  66.  
  67.  
  68. int
  69. main(void) {
  70.   OSStatus
  71.     status;                    /* We'll use this to test the outcome of each MP function. */
  72.   MPQueueID
  73.     terminationQueue,        /* This queue will report the completion of the task. */
  74.     communicationQueue;        /* This queue will be used to communicate between the app and the task. */
  75.   MPTaskID
  76.     task;                    /* This will be the ID of the task we create. */
  77.   char
  78.     myString[100];
  79.  
  80.   SIOUXSettings.asktosaveonclose = 0;
  81.  
  82.   if (!MPLibraryIsLoaded()) {
  83.     printf("Can't run without the \"%s\" shared library.\n", MPLibraryName);
  84.  
  85.     return 1;
  86.   }
  87.  
  88.   status = MPCreateQueue(&terminationQueue);    /* Create the queue which will report the completion of the task. */
  89.   if (status != noErr) {
  90.     return failure("Cannot create the termination queue:\n    ", "MPCreateQueue", status);
  91.   }
  92.  
  93.   status = MPCreateQueue(&communicationQueue);    /* Create the queue we'll use to communicate with. */
  94.   if (status != noErr) {
  95.     (void) MPDeleteQueue(terminationQueue);
  96.  
  97.     return failure("Cannot create the communication queue:\n    ", "MPCreateQueue", status);
  98.   }
  99.  
  100.   status = MPCreateTask(HelloWorld,                /* This is the task function. */
  101.                         communicationQueue,        /* This is the parameter to the task function. */
  102.                         kMPUseDefaultStackSize,    /* We'll use whatever the MP system software gives us. */
  103.                         terminationQueue,        /* We'll use this to sense task completion. */
  104.                         0,                        /* We won't use the first part of the termination message. */
  105.                         0,                        /* We won't use the second part of the termination message. */
  106.                         kMPNormalTaskOptions,    /* Use the normal task options. (Currently this *must* be zero!) */
  107.                         &task);                    /* Here's where the ID of the new task will go. */
  108.   if (status != noErr) {
  109.     (void) MPDeleteQueue(terminationQueue);
  110.     (void) MPDeleteQueue(communicationQueue);
  111.  
  112.     return failure(0, "MPCreateTask", status);
  113.   }
  114.  
  115.   receiveString(communicationQueue, myString);
  116.   printf("%s\n", myString);
  117.  
  118.   /* Wait for the task to complete: */
  119.   status = MPWaitOnQueue(terminationQueue, 0, 0, 0, kDurationForever);
  120.   if (status != noErr) {
  121.     (void) failure("While waiting for task completion:\n    ", "MPWaitOnQueue", status);
  122.   }
  123.  
  124.   status = MPDeleteQueue(terminationQueue);
  125.   if (status != noErr) {
  126.     (void) failure("Can't delete the termination queue:\n    ", "MPDeleteQueue", status);
  127.   }
  128.  
  129.   status = MPDeleteQueue(communicationQueue);
  130.   if (status != noErr) {
  131.     (void) failure("Can't delete the communication queue:\n    ", "MPDeleteQueue", status);
  132.   }
  133.  
  134.   return 0;
  135. }
  136.