home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CTASK.ZIP / TSKMSG.C < prev    next >
C/C++ Source or Header  |  1989-12-20  |  3KB  |  157 lines

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