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

  1. /*
  2.     --- Version 2.0 89-12-13 17:41 ---
  3.  
  4.    TSKBUF.C - CTask - Buffered 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.    NOTE: None of the Buffer routines my be used from inside an interrupt
  13.          handler. The routines are relatively slow, since they use
  14.          the normal word pipe and resource calls. Optimization would be 
  15.          possible by using block moves and internally handling resources.
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. #include "tsk.h"
  21. #include "tsklocal.h"
  22.  
  23.  
  24. bufferptr far create_buffer (bufferptr buf, farptr pbuf, word bufsize
  25. #if (TSK_NAMEPAR)
  26.                         ,byteptr name
  27. #endif
  28.                         )
  29. {
  30. #if (TSK_DYNAMIC)
  31.    if (buf == NULL)
  32.       {
  33.       if ((buf = tsk_alloc (sizeof (buffer))) == NULL)
  34.          return NULL;
  35.       buf->flags = F_TEMP;
  36.       }
  37.    else
  38.       buf->flags = 0;
  39.    if (pbuf == NULL)
  40.       {
  41.       if ((pbuf = tsk_alloc (bufsize)) == NULL)
  42.          {
  43.          if (buf->flags & F_TEMP)
  44.             tsk_free (buf);
  45.          return NULL;
  46.          }
  47.       buf->flags |= F_STTEMP;
  48.       }
  49. #endif
  50.  
  51.    create_wpipe (&buf->pip, pbuf, bufsize
  52. #if (TSK_NAMEPAR)
  53.                  ,name
  54. #endif
  55.                  );
  56.    create_resource (&buf->buf_read
  57. #if (TSK_NAMEPAR)
  58.                  ,name
  59. #endif
  60.                  );
  61.    create_resource (&buf->buf_write
  62. #if (TSK_NAMEPAR)
  63.                  ,name
  64. #endif
  65.                  );
  66.    buf->msgcnt = 0;
  67.  
  68. #if (TSK_NAMED)
  69.    tsk_add_name (&buf->name, name, TYP_BUFFER, buf);
  70. #endif
  71.  
  72.    return buf;
  73. }
  74.  
  75.  
  76. void far delete_buffer (bufferptr buf)
  77. {
  78.    delete_wpipe (&buf->pip);
  79.    delete_resource (&buf->buf_read);
  80.    delete_resource (&buf->buf_write);
  81.    buf->msgcnt = 0;
  82.  
  83. #if (TSK_NAMED)
  84.    tsk_del_name (&buf->name);
  85. #endif
  86.  
  87. #if (TSK_DYNAMIC)
  88.    if (buf->flags & F_STTEMP)
  89.       tsk_free (buf->pip.wcontents);
  90.    if (buf->flags & F_TEMP)
  91.       tsk_free (buf);
  92. #endif
  93. }
  94.  
  95.  
  96. int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  97. {
  98.    int i, len, l1, l2;
  99.    word w;
  100.  
  101.    if ((i = request_resource (&buf->buf_read, timeout)) < 0)
  102.       return i;
  103.  
  104.    if ((len = read_wpipe (&buf->pip, timeout)) < 0)
  105.       {
  106.       release_resource (&buf->buf_read);
  107.       return len;
  108.       }
  109.  
  110.    l1 = (len < size) ? len : size;
  111.  
  112.    for (i = 0; i < l1; i++)
  113.       {
  114.       if (!(i & 1))
  115.          w = read_wpipe (&buf->pip, 0L);
  116.       else
  117.          w = w >> 8;
  118.       ((byteptr)msg) [i] = (byte)w;
  119.       }
  120.  
  121.    l2 = (len + 1) >> 1;
  122.    for (i = (l1 + 1) >> 1; i < l2; i++)
  123.       read_wpipe (&buf->pip, 0L);
  124.    if (l1 < size)
  125.       ((byteptr)msg) [l1] = 0;
  126.  
  127.    buf->msgcnt--;
  128.  
  129.    release_resource (&buf->buf_read);
  130.    return len;
  131. }
  132.  
  133.  
  134. int far c_read_buffer (bufferptr buf, farptr msg, int size)
  135. {
  136.    int res;
  137.    CRITICAL;
  138.  
  139.    res = -1;
  140.    C_ENTER;
  141.  
  142.    if (buf->pip.filled && !buf->buf_read.count)
  143.       {
  144.       request_resource (&buf->buf_read, 0L);
  145.       C_LEAVE;
  146.       res = read_buffer (buf, msg, size, 0L);
  147.       }
  148.    else
  149.       C_LEAVE;
  150.  
  151.    return res;
  152. }
  153.  
  154.  
  155. local int near tsk_wrbuf (bufferptr buf, word w, dword timeout)
  156. {
  157.    int i;
  158.  
  159.    if ((i = write_wpipe (&buf->pip, w, timeout)) < 0)
  160.       release_resource (&buf->buf_write);
  161.    return i;
  162. }
  163.  
  164.  
  165. int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  166. {
  167.    int i, res, l2;
  168.  
  169.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  170.       return -3;
  171.  
  172.    if ((i = request_resource (&buf->buf_write, timeout)) < 0)
  173.       return i;
  174.  
  175.    if ((i = tsk_wrbuf (buf, (word)size, timeout)) < 0)
  176.       return i;
  177.  
  178.    l2 = (size + 1) >> 1;
  179.  
  180.    for (i = 0; i < l2; i++)
  181.       if ((res = tsk_wrbuf (buf, ((wordptr)msg) [i], timeout)) < 0)
  182.          return res;
  183.  
  184.    buf->msgcnt++;
  185.  
  186.    release_resource (&buf->buf_write);
  187.    return size;
  188. }
  189.  
  190.  
  191. int far c_write_buffer (bufferptr buf, farptr msg, int size)
  192. {
  193.    int res;
  194.    CRITICAL;
  195.  
  196.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  197.       return -3;
  198.  
  199.    C_ENTER;
  200.    res = -1;
  201.  
  202.    if (wpipe_free (&buf->pip) >= ((size + 3) >> 1) && !buf->buf_write.count)
  203.       {
  204.       request_resource (&buf->buf_write, 0L);
  205.       C_LEAVE;
  206.       res = write_buffer (buf, msg, size, 0L);
  207.       }
  208.    else
  209.       C_LEAVE;
  210.  
  211.    return res;
  212. }
  213.  
  214.  
  215. word far check_buffer (bufferptr buf)
  216. {
  217.    return buf->msgcnt;
  218. }
  219.  
  220.