home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07100a < prev    next >
Text File  |  1990-06-19  |  3KB  |  105 lines

  1. /*
  2.  *  monitor.c :   A task monitor for Desqview API
  3.  *  author:       Victor R. Volkman
  4.  */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <malloc.h>
  9. #include "dvapi.h"         /* These two files are found in */
  10. #include "dvapi2.h"        /* the Desqview API C package.  */
  11. #include "monitor.h"
  12.  
  13. #ifndef MAKEFD
  14. #include "monitor.fd"
  15. #endif
  16.  
  17.  
  18. /* The monitor contains two mutually exclusive functions:
  19.    recvl and sendv. The recvl function is used by
  20.    subprocesses to wait for any signal. A signal can
  21.    indicate either a synchronization wait or a communication
  22.    wait.  The sendv function is used by suprocesses to send
  23.    a synchronization signal or obtain exclusive write access
  24.    to the global communication array.
  25.  
  26.    Semafore usage is decoded as follows:
  27.  
  28.    Semafore value    Usage
  29.    --------------    -----
  30.        MASTER        Signal indicating that the master node
  31.                      is executing the critical section.
  32.  
  33.      1..NUMPRC       Communication signal indicates that
  34.                      valid data is waiting for a given node.
  35.  
  36.     NUMPRC+node      Synchronization signal returned by the
  37.                      master node to each individual node.
  38.  
  39.     2*NUMPRC+1       Termination signal returned when the
  40.                      master node finds that no further
  41.                      intervals will be used.
  42. */
  43.  
  44.  
  45. open_monitor(sem_name, new_mon_pp)
  46. char *sem_name;
  47. MONITOR **new_mon_pp;
  48. {
  49.    DESQ_HAN semaphore_han;
  50.  
  51.    semaphore_han = mal_new();
  52.    mal_name(semaphore_han, sem_name, strlen(sem_name));
  53.    *new_mon_pp = (MONITOR *) calloc(sizeof(MONITOR),1);
  54.    (*new_mon_pp)->sem_han = semaphore_han;
  55. }
  56.  
  57.  
  58. close_monitor(old_mon_pp)
  59. MONITOR **old_mon_pp;
  60. {
  61.    mal_close((*old_mon_pp)->sem_han);
  62.    free(*old_mon_pp);
  63.    *old_mon_pp = NULL;
  64. }
  65.  
  66.  
  67. sendv(mon_p,val,to_node)
  68. MONITOR *mon_p;
  69. double val;
  70. int to_node;
  71. {
  72.    DESQ_HAN semaphore_han = mon_p->sem_han;
  73.  
  74.    mal_lock(semaphore_han);      /* begin mutually exclusive
  75.                                     access */
  76.    mon_p->sent[to_node]++;
  77.    mon_p->value[to_node] = val;
  78.    mal_unlock(semaphore_han);    /* end mutually exclusive
  79.                                     access   */
  80. }
  81.  
  82.  
  83. recvl(mon_p,val_p,from_node)
  84. MONITOR *mon_p;
  85. double *val_p;
  86. int from_node;
  87. {
  88.    DESQ_HAN semaphore_han = mon_p->sem_han;
  89.    int *from_ptr;
  90.  
  91.    from_ptr = &(mon_p->sent[from_node]);
  92.  
  93.    mal_lock(semaphore_han);      /* begin mutually exclusive
  94.                                     access */
  95.    while (! *from_ptr) {  /* message hasn't arrived yet */
  96.       mal_unlock(semaphore_han);
  97.       api_pause();
  98.       mal_lock(semaphore_han);
  99.       }
  100.    *from_ptr = *from_ptr - 1;
  101.    *val_p = mon_p->value[from_node];
  102.    mal_unlock(semaphore_han);    /* end mutually exclusive
  103.                                     access   */
  104. }
  105.