home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_03 / tskmsg.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  3KB  |  161 lines

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