home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
ctask.zip
/
TSKBUF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-03-01
|
3KB
|
170 lines
/*
TSKBUF.C - CTask - Buffered Message handling routines.
Public Domain Software written by
Thomas Wagner
Patschkauer Weg 31
D-1000 Berlin 33
West Germany
NOTE: None of the Buffer routines my be used from inside an interrupt
handler. The routines are relatively slow, since they use
the normal word pipe and resource calls. Optimization would be
possible by using block moves and internally handling resources.
*/
#include <stdio.h>
#include "tsk.h"
#include "tsklocal.h"
void far create_buffer (bufferptr buf, farptr pbuf, word bufsize)
{
create_wpipe (&buf->pip, pbuf, bufsize);
create_resource (&buf->buf_read);
create_resource (&buf->buf_write);
buf->msgcnt = 0;
}
void far delete_buffer (bufferptr buf)
{
delete_wpipe (&buf->pip);
delete_resource (&buf->buf_read);
delete_resource (&buf->buf_write);
buf->msgcnt = 0;
}
int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout)
{
int i, len, l1;
word w;
CRITICAL;
if ((i = request_resource (&buf->buf_read, timeout)) < 0)
return i;
if ((len = read_wpipe (&buf->pip, timeout)) < 0)
{
release_resource (&buf->buf_read);
return len;
}
l1 = (len < size) ? len : size;
for (i = 0; i < l1; i++)
{
if (!(i & 1))
w = read_wpipe (&buf->pip, 0L);
else
w = w >> 8;
((byteptr)msg) [i] = (byte)w;
}
len = (len + 1) >> 1;
for (i = (l1 + 1) >> 1; i < len; i++)
read_wpipe (&buf->pip, 0L);
if (l1 < size)
((byteptr)msg) [l1] = 0;
C_ENTER;
buf->msgcnt++;
C_LEAVE;
release_resource (&buf->buf_read);
return 0;
}
int far c_read_buffer (bufferptr buf, farptr msg, int size)
{
int res;
CRITICAL;
res = -1;
C_ENTER;
if (buf->pip.filled && buf->buf_read.state)
{
request_resource (&buf->buf_read, 0L);
C_LEAVE;
res = read_buffer (buf, msg, size, 0L);
}
else
C_LEAVE;
return res;
}
local int near tsk_wrbuf (bufferptr buf, word w, dword timeout)
{
int i;
if ((i = write_wpipe (&buf->pip, w, timeout)) < 0)
release_resource (&buf->buf_write);
return i;
}
int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout)
{
int i, res;
CRITICAL;
if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
return -3;
if ((i = request_resource (&buf->buf_write, timeout)) < 0)
return i;
if ((i = tsk_wrbuf (buf, (word)size, timeout)) < 0)
return i;
size = (size + 1) >> 1;
for (i = 0; i < size; i++)
if ((res = tsk_wrbuf (buf, ((wordptr)msg) [i], timeout)) < 0)
return res;
C_ENTER;
buf->msgcnt--;
C_LEAVE;
release_resource (&buf->buf_write);
return 0;
}
int far c_write_buffer (bufferptr buf, farptr msg, int size)
{
int res;
CRITICAL;
if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
return -3;
C_ENTER;
res = -1;
if (wpipe_free (&buf->pip) >= ((size + 3) >> 1) && buf->buf_write.state)
{
request_resource (&buf->buf_write, 0L);
C_LEAVE;
res = write_buffer (buf, msg, size, 0L);
}
else
C_LEAVE;
return res;
}
word far check_buffer (bufferptr buf)
{
return buf->msgcnt;
}