home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSKMSG.C < prev    next >
Text File  |  1988-03-01  |  2KB  |  125 lines

  1. /*
  2.    TSKMSG.C - CTask - Message handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16.  
  17. /*
  18.    create_mailbox - initialises mailbox.
  19. */
  20.  
  21. void far create_mailbox (mailboxptr box)
  22. {
  23.    box->waiting = NULL;
  24.    box->mail_first = box->mail_last = NULL;
  25. }
  26.  
  27.  
  28. /*
  29.    delete_mailbox - kills all processes waiting for mail
  30. */
  31.  
  32. void far delete_mailbox (mailboxptr box)
  33. {
  34.    CRITICAL;
  35.  
  36.    C_ENTER;
  37.    tsk_kill_queue (&(box->waiting));
  38.    box->mail_first = box->mail_last = NULL;
  39.    C_LEAVE;
  40. }
  41.  
  42.  
  43. /*
  44.    wait_mail - Wait until mail arrives. If there is mail in the box on
  45.                entry, the first mail block is assigned to the caller,
  46.                and the task continues to run.
  47. */
  48.  
  49. farptr far wait_mail (mailboxptr box, dword timeout)
  50. {
  51.    msgptr msg;
  52.    CRITICAL;
  53.  
  54.    C_ENTER;
  55.    if ((msg = box->mail_first) != NULL)
  56.       {
  57.       if ((box->mail_first = msg->next) == NULL)
  58.          box->mail_last = NULL;
  59.       C_LEAVE;
  60.       return msg;
  61.       }
  62.  
  63.    tsk_wait (&box->waiting, timeout);
  64.    return tsk_current->retptr;
  65. }
  66.  
  67. /*
  68.    c_wait_mail - If there is mail in the box on entry, the first mail 
  69.                  block is assigned to the caller, else an error is returned.
  70. */
  71.  
  72. farptr far c_wait_mail (mailboxptr box)
  73. {
  74.    msgptr msg;
  75.    CRITICAL;
  76.  
  77.    C_ENTER;
  78.    if ((msg = box->mail_first) != NULL)
  79.       if ((box->mail_first = msg->next) == NULL)
  80.          box->mail_last = NULL;
  81.    C_LEAVE;
  82.    return msg;
  83. }
  84.  
  85.  
  86. /*
  87.    send_mail - Send a mail block to a mailbox. If there are tasks waiting
  88.                for mail, the first waiting task is assigned the block and
  89.                is made eligible.
  90. */
  91.  
  92. void far send_mail (mailboxptr box, farptr msg)
  93. {
  94.    tcbptr curr;
  95.    CRITICAL;
  96.  
  97.    C_ENTER;
  98.    if ((curr = box->waiting) == NULL)
  99.       {
  100.       if (box->mail_first == NULL)
  101.          box->mail_first = (msgptr)msg;
  102.       else
  103.          box->mail_last->next = (msgptr)msg;
  104.       ((msgptr)msg)->next = NULL;
  105.       box->mail_last = (msgptr)msg;
  106.       C_LEAVE;
  107.       return;
  108.       }
  109.    box->waiting = tsk_runable (curr);
  110.    curr->retptr = msg;
  111.    C_LEAVE;
  112. }
  113.  
  114.  
  115. /*
  116.    check_mailbox - returns TRUE if there is mail in the box.
  117. */
  118.  
  119. int far check_mailbox (mailboxptr box)
  120. {
  121.    return box->mail_first != NULL;
  122. }
  123.  
  124.  
  125.