home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSKPIP.C < prev    next >
C/C++ Source or Header  |  1988-03-01  |  5KB  |  234 lines

  1. /*
  2.    TSKPIP.C - CTask - Pipe 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.    tsk_getcpipe - get a byte from a pipe. For internal use only.
  18.                   Critical section assumed entered.
  19. */
  20.  
  21. local byte near tsk_getcpipe (pipeptr pip)
  22. {
  23.    byte c;
  24.  
  25.    c = pip->contents [pip->outptr++];
  26.    if (pip->outptr >= pip->bufsize)
  27.       pip->outptr = 0;
  28.    pip->filled--;
  29.    return c;
  30. }
  31.  
  32.  
  33. /*
  34.    tsk_putcpipe - put a byte to a pipe. For internal use only.
  35.                   Critical section assumed entered.
  36. */
  37.  
  38. local void near tsk_putcpipe (pipeptr pip, byte c)
  39. {
  40.    pip->contents [pip->inptr++] = c;
  41.    if (pip->inptr >= pip->bufsize)
  42.       pip->inptr = 0;
  43.    pip->filled++;
  44. }
  45.  
  46.  
  47. /* -------------------------------------------------------------------- */
  48.  
  49.  
  50. /*
  51.    create_pipe - initialises pipe.
  52. */
  53.  
  54. void far create_pipe (pipeptr pip, farptr buf, word bufsize)
  55. {
  56.    pip->wait_read = pip->wait_write = pip->wait_clear = NULL;
  57.    pip->outptr = pip->inptr = pip->filled = 0;
  58.    pip->bufsize = bufsize;
  59.    pip->contents = (byteptr)buf;
  60. }
  61.  
  62.  
  63. /*
  64.    delete_pipe - kills all processes waiting for reading from or writing
  65.                  to the pipe.
  66. */
  67.  
  68. void far delete_pipe (pipeptr pip)
  69. {
  70.    CRITICAL;
  71.  
  72.    C_ENTER;
  73.    tsk_kill_queue (&(pip->wait_read));
  74.    tsk_kill_queue (&(pip->wait_write));
  75.    tsk_kill_queue (&(pip->wait_clear));
  76.    pip->outptr = pip->inptr = pip->filled = 0;
  77.    C_LEAVE;
  78. }
  79.  
  80.  
  81. /*
  82.    read_pipe - Wait until a character is written to the pipe. If there 
  83.                is a character in the pipe on entry, it is assigned to 
  84.                the caller, and the task continues to run. If there are
  85.                tasks waiting to write, the first task is made eligible,
  86.                and the character is inserted into the pipe.
  87. */
  88.  
  89. int far read_pipe (pipeptr pip, dword timeout)
  90. {
  91.    tcbptr curr;
  92.    int res;
  93.    CRITICAL;
  94.  
  95.    C_ENTER;
  96.  
  97.    if (pip->filled)
  98.       {
  99.       res = tsk_getcpipe (pip);
  100.  
  101.       if ((curr = pip->wait_write) != NULL)
  102.          {
  103.          tsk_putcpipe (pip, (byte)curr->retsize);
  104.          pip->wait_write = tsk_runable (curr);
  105.          curr->retptr = NULL;
  106.          }
  107.       else if (!pip->filled)
  108.          while (pip->wait_clear != NULL)
  109.             pip->wait_clear = tsk_runable (pip->wait_clear);
  110.  
  111.       C_LEAVE;
  112.       return res;
  113.       }
  114.  
  115.    tsk_wait (&pip->wait_read, timeout);
  116.    return (int)tsk_current->retptr;
  117. }
  118.  
  119.  
  120. /*
  121.    c_read_pipe - If there is a character in the pipe on entry,
  122.                  read_pipe is called, otherwise en error status is returned.
  123. */
  124.  
  125. int far c_read_pipe (pipeptr pip)
  126. {
  127.    CRITICAL, res;
  128.  
  129.    C_ENTER;
  130.    res = (pip->filled) ? read_pipe (pip, 0L) : -1;
  131.    C_LEAVE;
  132.    return res;
  133. }
  134.  
  135.  
  136.  
  137. /*
  138.    write_pipe - Wait until space for the character to be written to the 
  139.                 pipe is available. If there is enough space in the pipe 
  140.                 on entry, the character is inserted into the pipe, and
  141.                 the task continues to run. If there are tasks waiting 
  142.                 to read, the first task is made eligible, and the character
  143.                 is passed to the waiting task.
  144. */
  145.  
  146. int far write_pipe (pipeptr pip, byte ch, dword timeout)
  147. {
  148.    tcbptr curr;
  149.    CRITICAL;
  150.  
  151.    C_ENTER;
  152.  
  153.    if (pip->filled < pip->bufsize)
  154.       {
  155.       if ((curr = pip->wait_read) != NULL)
  156.          {
  157.          pip->wait_read = tsk_runable (curr);
  158.          curr->retptr = (farptr)ch;
  159.          C_LEAVE;
  160.          return 0;
  161.          }
  162.  
  163.       tsk_putcpipe (pip, ch);
  164.       C_LEAVE;
  165.       return 0;
  166.       }
  167.  
  168.    tsk_current->retsize = ch;
  169.    tsk_wait (&pip->wait_write, timeout);
  170.    return (int)tsk_current->retptr;
  171. }
  172.  
  173.  
  174. /*
  175.    c_write_pipe - If there is space for the character in the pipe on entry,
  176.                   write_pipe is called, otherwise en error status is returned.
  177. */
  178.  
  179. int far c_write_pipe (pipeptr pip, byte ch)
  180. {
  181.    int res;
  182.    CRITICAL;
  183.  
  184.    C_ENTER;
  185.    res = (pip->filled < pip->bufsize) ? write_pipe (pip, ch, 0L) : -1;
  186.    C_LEAVE;
  187.    return res;
  188. }
  189.  
  190.  
  191. /*
  192.    wait_pipe_empty - Wait until the pipe is empty. If the pipe is
  193.                      empty on entry, the task continues to run.
  194. */
  195.  
  196. int far wait_pipe_empty (pipeptr pip, dword timeout)
  197. {
  198.    CRITICAL;
  199.  
  200.    C_ENTER;
  201.    if (!pip->filled)
  202.       {
  203.       C_LEAVE;
  204.       return 0;
  205.       }
  206.  
  207.    tsk_current->retptr = NULL;
  208.    tsk_wait (&pip->wait_clear, timeout);
  209.    return (int)tsk_current->retptr;
  210. }
  211.  
  212.  
  213. /*
  214.    check_pipe - returns -1 if there are no characters in the pipe, else
  215.                 the first available character.
  216. */
  217.  
  218. int far check_pipe (pipeptr pip)
  219. {
  220.    return (pip->filled) ? (int)pip->contents [pip->outptr] : -1;
  221. }
  222.  
  223.  
  224. /*
  225.    pipe_free - returns the number of free characters in the pipe.
  226. */
  227.  
  228. word far pipe_free (pipeptr pip)
  229. {
  230.    return pip->bufsize - pip->filled;
  231. }
  232.  
  233.  
  234.