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

  1. /*
  2.    TSKBUF.C - CTask - Buffered Message 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.    NOTE: None of the Buffer routines my be used from inside an interrupt
  11.          handler. The routines are relatively slow, since they use
  12.          the normal word pipe and resource calls. Optimization would be 
  13.          possible by using block moves and internally handling resources.
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. #include "tsk.h"
  19. #include "tsklocal.h"
  20.  
  21.  
  22. void far create_buffer (bufferptr buf, farptr pbuf, word bufsize)
  23. {
  24.    create_wpipe (&buf->pip, pbuf, bufsize);
  25.    create_resource (&buf->buf_read);
  26.    create_resource (&buf->buf_write);
  27.    buf->msgcnt = 0;
  28. }
  29.  
  30.  
  31. void far delete_buffer (bufferptr buf)
  32. {
  33.    delete_wpipe (&buf->pip);
  34.    delete_resource (&buf->buf_read);
  35.    delete_resource (&buf->buf_write);
  36.    buf->msgcnt = 0;
  37. }
  38.  
  39.  
  40. int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  41. {
  42.    int i, len, l1;
  43.    word w;
  44.    CRITICAL;
  45.  
  46.    if ((i = request_resource (&buf->buf_read, timeout)) < 0)
  47.       return i;
  48.  
  49.    if ((len = read_wpipe (&buf->pip, timeout)) < 0)
  50.       {
  51.       release_resource (&buf->buf_read);
  52.       return len;
  53.       }
  54.  
  55.    l1 = (len < size) ? len : size;
  56.  
  57.    for (i = 0; i < l1; i++)
  58.       {
  59.       if (!(i & 1))
  60.          w = read_wpipe (&buf->pip, 0L);
  61.       else
  62.          w = w >> 8;
  63.       ((byteptr)msg) [i] = (byte)w;
  64.       }
  65.  
  66.    len = (len + 1) >> 1;
  67.    for (i = (l1 + 1) >> 1; i < len; i++)
  68.       read_wpipe (&buf->pip, 0L);
  69.    if (l1 < size)
  70.       ((byteptr)msg) [l1] = 0;
  71.  
  72.    C_ENTER;
  73.    buf->msgcnt++;
  74.    C_LEAVE;
  75.  
  76.    release_resource (&buf->buf_read);
  77.    return 0;
  78. }
  79.  
  80.  
  81. int far c_read_buffer (bufferptr buf, farptr msg, int size)
  82. {
  83.    int res;
  84.    CRITICAL;
  85.  
  86.    res = -1;
  87.    C_ENTER;
  88.  
  89.    if (buf->pip.filled && buf->buf_read.state)
  90.       {
  91.       request_resource (&buf->buf_read, 0L);
  92.       C_LEAVE;
  93.       res = read_buffer (buf, msg, size, 0L);
  94.       }
  95.    else
  96.       C_LEAVE;
  97.  
  98.    return res;
  99. }
  100.  
  101.  
  102. local int near tsk_wrbuf (bufferptr buf, word w, dword timeout)
  103. {
  104.    int i;
  105.  
  106.    if ((i = write_wpipe (&buf->pip, w, timeout)) < 0)
  107.       release_resource (&buf->buf_write);
  108.    return i;
  109. }
  110.  
  111.  
  112. int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  113. {
  114.    int i, res;
  115.    CRITICAL;
  116.  
  117.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  118.       return -3;
  119.  
  120.    if ((i = request_resource (&buf->buf_write, timeout)) < 0)
  121.       return i;
  122.  
  123.    if ((i = tsk_wrbuf (buf, (word)size, timeout)) < 0)
  124.       return i;
  125.  
  126.    size = (size + 1) >> 1;
  127.  
  128.    for (i = 0; i < size; i++)
  129.       if ((res = tsk_wrbuf (buf, ((wordptr)msg) [i], timeout)) < 0)
  130.          return res;
  131.  
  132.    C_ENTER;
  133.    buf->msgcnt--;
  134.    C_LEAVE;
  135.  
  136.    release_resource (&buf->buf_write);
  137.    return 0;
  138. }
  139.  
  140.  
  141. int far c_write_buffer (bufferptr buf, farptr msg, int size)
  142. {
  143.    int res;
  144.    CRITICAL;
  145.  
  146.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  147.       return -3;
  148.  
  149.    C_ENTER;
  150.    res = -1;
  151.  
  152.    if (wpipe_free (&buf->pip) >= ((size + 3) >> 1) && buf->buf_write.state)
  153.       {
  154.       request_resource (&buf->buf_write, 0L);
  155.       C_LEAVE;
  156.       res = write_buffer (buf, msg, size, 0L);
  157.       }
  158.    else
  159.       C_LEAVE;
  160.  
  161.    return res;
  162. }
  163.  
  164.  
  165. word far check_buffer (bufferptr buf)
  166. {
  167.    return buf->msgcnt;
  168. }
  169.  
  170.