home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTASK11.ZIP / TSKWPIP.C < prev   
Encoding:
C/C++ Source or Header  |  1988-07-01  |  5.8 KB  |  276 lines

  1. /*
  2.    TSKWPIP.C - CTask - Word 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_getwpipe - get a word from a pipe. For internal use only.
  18.                   Critical section assumed entered.
  19. */
  20.  
  21. local word near tsk_getwpipe (wpipeptr pip)
  22. {
  23.    word c;
  24.  
  25.    c = pip->wcontents [pip->outptr++];
  26.    if (pip->outptr >= pip->bufsize)
  27.       pip->outptr = 0;
  28.    pip->filled--;
  29.    return c;
  30. }
  31.  
  32.  
  33. /*
  34.    tsk_putwpipe - put a word to a pipe. For internal use only.
  35.                   Critical section assumed entered.
  36. */
  37.  
  38. local void near tsk_putwpipe (wpipeptr pip, word c)
  39. {
  40.    pip->wcontents [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_wpipe - initialises wpipe.
  52. */
  53.  
  54. wpipeptr far create_wpipe (wpipeptr pip, farptr buf, word bufsize
  55. #if (TSK_NAMEPAR)
  56.                            ,byteptr name
  57. #endif
  58.                            )
  59. {
  60. #if (TSK_DYNAMIC)
  61.    if (pip == NULL)
  62.       {
  63.       if ((pip = tsk_alloc (sizeof (wpipe))) == NULL)
  64.          return NULL;
  65.       pip->flags = F_TEMP;
  66.       }
  67.    else
  68.       pip->flags = 0;
  69.    if (buf == NULL)
  70.       {
  71.       if ((buf = tsk_alloc (bufsize)) == NULL)
  72.          {
  73.          if (pip->flags & F_TEMP)
  74.             tsk_free (pip);
  75.          return NULL;
  76.          }
  77.       pip->flags |= F_STTEMP;
  78.       }
  79. #endif
  80.  
  81.    pip->wait_read = pip->wait_write = pip->wait_clear = NULL;
  82.    pip->outptr = pip->inptr = pip->filled = 0;
  83.    pip->bufsize = bufsize >> 1;
  84.    pip->wcontents = (wordptr)buf;
  85.  
  86. #if (TSK_NAMED)
  87.    tsk_add_name (&pip->name, name, TYP_WPIPE, pip);
  88. #endif
  89.  
  90.    return pip;
  91. }
  92.  
  93.  
  94. /*
  95.    delete_wpipe - kills all processes waiting for reading from or writing
  96.                   to the wpipe.
  97. */
  98.  
  99. void far delete_wpipe (wpipeptr pip)
  100. {
  101.    CRITICAL;
  102.  
  103.    C_ENTER;
  104.    tsk_kill_queue (&(pip->wait_read));
  105.    tsk_kill_queue (&(pip->wait_write));
  106.    tsk_kill_queue (&(pip->wait_clear));
  107.    pip->outptr = pip->inptr = pip->filled = 0;
  108.    C_LEAVE;
  109.  
  110. #if (TSK_NAMED)
  111.    tsk_del_name (&pip->name);
  112. #endif
  113.  
  114. #if (TSK_DYNAMIC)
  115.    if (pip->flags & F_STTEMP)
  116.       tsk_free (pip->wcontents);
  117.    if (pip->flags & F_TEMP)
  118.       tsk_free (pip);
  119. #endif
  120. }
  121.  
  122.  
  123. /*
  124.    read_wpipe - Wait until a word is written to the wpipe. If there 
  125.                 is a word in the wpipe on entry, it is assigned to 
  126.                 the caller, and the task continues to run. If there are
  127.                 tasks waiting to write, the first task is made eligible,
  128.                 and the word is inserted into the wpipe.
  129. */
  130.  
  131. word far read_wpipe (wpipeptr pip, dword timeout)
  132. {
  133.    tcbptr curr;
  134.    word res;
  135.    CRITICAL;
  136.  
  137.    C_ENTER;
  138.  
  139.    if (pip->filled)
  140.       {
  141.       res = tsk_getwpipe (pip);
  142.  
  143.       if ((curr = pip->wait_write) != NULL)
  144.          {
  145.          tsk_putwpipe (pip, curr->retsize);
  146.          pip->wait_write = tsk_runable (curr);
  147.          curr->retptr = NULL;
  148.          }
  149.       else if (!pip->filled)
  150.          while (pip->wait_clear != NULL)
  151.             pip->wait_clear = tsk_runable (pip->wait_clear);
  152.  
  153.       C_LEAVE;
  154.       return res;
  155.       }
  156.  
  157.    tsk_wait (&pip->wait_read, timeout);
  158.    return (word)tsk_current->retptr;
  159. }
  160.  
  161.  
  162. /*
  163.    c_read_wpipe - If there is a word in the wpipe on entry,
  164.                   read_wpipe is called, otherwise an error status is returned.
  165. */
  166.  
  167. word far c_read_wpipe (wpipeptr pip)
  168. {
  169.    CRITICAL, res;
  170.  
  171.    C_ENTER;
  172.    res = (pip->filled) ? read_wpipe (pip, 0L) : (word)-1;
  173.    C_LEAVE;
  174.    return res;
  175. }
  176.  
  177.  
  178.  
  179. /*
  180.    write_wpipe - Wait until space for the word to be written to the 
  181.                  wpipe is available. If there is enough space in the wpipe 
  182.                  on entry, the word is inserted into the wpipe, and
  183.                  the task continues to run. If there are tasks waiting 
  184.                  to read, the first task is made eligible, and the word
  185.                  is passed to the waiting task.
  186. */
  187.  
  188. int far write_wpipe (wpipeptr pip, word ch, dword timeout)
  189. {
  190.    tcbptr curr;
  191.    CRITICAL;
  192.  
  193.    C_ENTER;
  194.  
  195.    if (pip->filled < pip->bufsize)
  196.       {
  197.       if ((curr = pip->wait_read) != NULL)
  198.          {
  199.          pip->wait_read = tsk_runable (curr);
  200.          curr->retptr = (farptr)ch;
  201.          C_LEAVE;
  202.          return 0;
  203.          }
  204.  
  205.       tsk_putwpipe (pip, ch);
  206.       C_LEAVE;
  207.       return 0;
  208.       }
  209.  
  210.    tsk_current->retsize = ch;
  211.    tsk_wait (&pip->wait_write, timeout);
  212.    return (int)tsk_current->retptr;
  213. }
  214.  
  215.  
  216. /*
  217.    c_write_wpipe - If there is space for the word in the wpipe on entry,
  218.                    write_wpipe is called, otherwise an error status is returned.
  219. */
  220.  
  221. int far c_write_wpipe (wpipeptr pip, word ch)
  222. {
  223.    int res;
  224.    CRITICAL;
  225.  
  226.    C_ENTER;
  227.    res = (pip->filled < pip->bufsize) ? write_wpipe (pip, ch, 0L) : -1;
  228.    C_LEAVE;
  229.    return res;
  230. }
  231.  
  232.  
  233. /*
  234.    wait_wpipe_empty - Wait until the pipe is empty. If the pipe is
  235.                       empty on entry, the task continues to run.
  236. */
  237.  
  238. int far wait_wpipe_empty (wpipeptr pip, dword timeout)
  239. {
  240.    CRITICAL;
  241.  
  242.    C_ENTER;
  243.    if (!pip->filled)
  244.       {
  245.       C_LEAVE;
  246.       return 0;
  247.       }
  248.  
  249.    tsk_current->retptr = NULL;
  250.    tsk_wait (&pip->wait_clear, timeout);
  251.    return (int)tsk_current->retptr;
  252. }
  253.  
  254.  
  255. /*
  256.    check_wpipe - returns -1 if there are no words in the wpipe, else
  257.                  the first available word.
  258. */
  259.  
  260. word far check_wpipe (wpipeptr pip)
  261. {
  262.    return (pip->filled) ? pip->wcontents [pip->outptr] : (word)-1;
  263. }
  264.  
  265.  
  266. /*
  267.    wpipe_free - returns the number of free words in the pipe.
  268. */
  269.  
  270. word far wpipe_free (wpipeptr pip)
  271. {
  272.    return pip->bufsize - pip->filled;
  273. }
  274.  
  275.  
  276.