home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MAILBOX.ZIP / MPBOX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-27  |  4.5 KB  |  146 lines

  1. /****************************************************************************/
  2. /* mpbox.c  -- MAIL BOX CLUSTER                (IMPLEMENTATION)    */
  3. /* Created:  12/12/87        Release:  0.7        Version:  06/27/88  */
  4. /****************************************************************************
  5. (c) Copyright 1987, 1988 by Michael Benjamin Parker     (USA SS# 557-49-4130)
  6.  
  7. All Rights Reserved unless specified in the following include files: */
  8. #include "mptsk.cpy" /*
  9.  
  10. DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
  11. ****************************************************************************/
  12. /* OVERVIEW:    See mptsk.h                            */
  13. /****************************************************************************/
  14. /****************************************************************************/
  15. #include "mpbox.h"
  16. /****************************************************************************/
  17. /****************************************************************************/
  18. /****************************************************************************/
  19. MPBOXID        mpbox_init(buf)
  20.     void PTR    buf;
  21. {
  22.     MPBOXID    mpbox=    (MPBOXID)buf;
  23.  
  24.     mprng_init(str2fld(mpbox,mprng));
  25.     mpsem_init(str2fld(mpbox,mpsem));
  26.     mpbox->msgcnt=    0;
  27.     mprng_init(str2fld(mpbox,msgs));
  28.     return(mpbox);
  29. }
  30. /****************************************************************************/
  31. void PTR    mpbox_dinit(mpbox)
  32.     MPBOXID    mpbox;
  33. {
  34.     mprng_dinit(mpbox);
  35.     mpsem_dinit(str2fld(mpbox,mpsem));
  36.     mprng_dinit(str2fld(mpbox,msgs));
  37.     return((void PTR)mpbox);
  38. }
  39. /****************************************************************************/
  40. /****************************************************************************/
  41. MPRNGID    mpbox_xfer(mpbox,next,msg)
  42.     MPBOXID    mpbox;
  43.     BOOL    next;
  44.     MPRNGID    msg;
  45. {
  46.     mpsem_critsect(str2fld(mpbox,mpsem), {});
  47. {
  48.         if (next) {    /* SEND A MESSAGE */
  49.             if (++(mpbox->msgcnt) > 0) {    /* NO RECEIVERS */
  50.                 if (!msg)    (mpbox->msgcnt)--;
  51.                 else     mprng_addn(str2fld(mpbox,msgs),msg);
  52.                 msg=0;
  53.             } else {            /* RECEIVERS WAITING*/
  54.                 msg=    mprng_subn(str2fld(mpbox,msgs));
  55.             }
  56.         } else {    /* RECEIVE A MESSAGE */
  57.             if ((mpbox->msgcnt)-- > 0) {    /* MESSAGES WAITING */
  58.                 msg=    mprng_subp(str2fld(mpbox,msgs));
  59.             } else {            /* NO MESSAGES */
  60.                 if (!msg)    ++(mpbox->msgcnt);
  61.                 else    mprng_addp(str2fld(mpbox,msgs),msg);
  62.                 msg=    0;
  63.             }
  64.         }
  65.     }
  66.     return(msg);
  67. }
  68. /****************************************************************************/
  69. void    mpbox_print(MPBOXID mpbox)
  70. {
  71.     printf("\tmpbox %lX (mpsem %ld, msgcnt %ld, msgs ",
  72.             ptr_2int(mpbox),(INT)(mpbox->mpsem),mpbox->msgcnt);
  73.     mprng_printall(str2fld(mpbox,msgs));
  74.     printf(")");
  75. }
  76.  
  77. void    mpbox_printall(MPBOXID mpbox)
  78. {
  79.     MPBOXID    cur=    mpbox;
  80.     printf("(");
  81.     do {
  82.         printf("\n");
  83.         mpbox_print(cur);
  84.         cur=    mprng_next(cur);
  85.     } while (cur!=mpbox);
  86.     printf("\n)");
  87. }
  88. /*
  89. /****************************************************************************/
  90. MPRNGID    mpbox_xfer0(MPBOXID mpbox, MPRNGID msg, BOOL block)
  91. {
  92. mpsem_critsect(str2fld(mpbox,mpsem), {
  93.     mptsk_me()->msg=    msg;
  94.     do {
  95.         if (msg) {    /* SEND A MESSAGE */
  96.             if ((mpbox->msgcnt) >= 0) {    /* NO RECEIVERS */
  97.                 if (!block)    break;
  98.                 mprng_addp(str2fld(mpbox,msgs),msg);
  99.                 ++(mpbox->msgcnt);
  100.                 /* MAYBE RETURN */
  101.                 if (msg!=mptsk_me()) {msg= 0; break;}
  102.             } else {            /* RECEIVERS WAITING*/
  103.                 MPTSKID    mptsk=
  104.                     mprng_subp(
  105.                         str2fld(mpbox->msgs));
  106.                     ++(mpbox->msgcnt);
  107.                 mptsk->msg=    msg;
  108.                 msg=    0;
  109.                 /* COULD RETURN */
  110.                 mptsk_schedule(mptsk);
  111.                 mpbox_scheduleme()
  112.                 break;
  113.             }
  114.         } else {    /* RECEIVE A MESSAGE */
  115.             if ((mpbox->msgcnt) > 0) {    /* MESSAGES WAITING */
  116.                 msg=    mprng_subn(
  117.                         str2fld(mpbox,msgs));
  118.                     (mpbox->msgcnt)--;
  119.                 break;
  120.             } else {            /* NO MESSAGES */
  121.                 if (!block)    break;
  122.                 mprng_addn(    str2fld(mpbox,msgs),
  123.                         mptsk_me());
  124.                     (mpbox->msgcnt)--;
  125.             }
  126.         }
  127.         {    /* FIND AND SWITCH TO A NEW TASK */
  128.             MPBOXID    cursched=    headsched;
  129.             MPTSKID    mptsk;
  130.             while (!(mptsk=    mpbox_recv(cursched,BOOL_FALSE)))
  131.                 cursched=    mpbox_next(cursched);
  132.             mpthd_switch(str2fld(mptsk,mpthd));
  133.         }
  134.     } while (BOOL_FALSE);
  135.     msg=    mptsk_me()->recvmsg;    mptsk_me()->recvmsg=    0;
  136. });
  137. return(msg);
  138. }
  139. */
  140. /****************************************************************************/
  141. /****************************************************************************/
  142. /****************************************************************************/
  143. /****************************************************************************/
  144.  
  145.  
  146.