hip/chan.c

144 lines
3.7 KiB
C
Raw Normal View History

#include "hip-prv.h"
#include <stdlib.h>
#include <string.h>
struct hip_chan_t
{
hip_t* hip;
int closed;
hip_oow_t unit_size;
hip_oow_t buf_size;
hip_uint8_t* buf;
hip_uint8_t* endptr;
hip_uint8_t* readptr;
hip_uint8_t* writeptr;
hip_uctx_link_t sendq; // list of senders waiting on this channel
hip_uctx_link_t recvq; // list of receivers waiting on this channel
};
hip_chan_t* hip_chan_open(hip_t* hip, hip_oow_t buf_size)
{
hip_chan_t* c;
c = (hip_chan_t*)malloc(HIP_SIZEOF(*c) + buf_size);
if (HIP_UNLIKELY(!c)) return HIP_NULL;
memset(c, 0, HIP_SIZEOF(*c));
c->hip = hip;
c->closed = 0;
c->buf_size = buf_size;
c->buf = (hip_uint8_t*)(c + 1);
c->endptr = 0;
c->readptr = 0;
c->writeptr = 0;
HIP_LIST_INIT(&c->recvq);
HIP_LIST_INIT(&c->sendq);
return c;
}
void hip_chan_close(hip_chan_t* c)
{
free(c);
}
int hip_chan_send(hip_chan_t* c, const void* ptr, hip_oow_t len)
{
hip_t* hip;
hip_uctx_t* caller;
int ret;
hip = c->hip;
caller = UCTX_FROM_LINK(hip->running);
// TODO: IS BUFFER EMPTY
if (!HIP_LIST_IS_EMPTY(&c->recvq)) /* there is a receving routine */
{
hip_uctx_link_t* x;
hip_uctx_t* xx;
x = HIP_LIST_HEAD(&c->recvq);
xx = UCTX_FROM_LINK(x);
if (len > xx->chan_data_len) len = xx->chan_data_len;
memcpy(xx->chan_data_ptr, ptr, len);
//printf ("USE SEND CHAT CALLER %p RECEIVER %p RET %p\n", caller, xx, xx->chan_ret_ptr);
*(xx->chan_ret_ptr) = len; /* manipulate the return value of the receiver */
/* remove the receiving routine from the queue and make it runnable */
HIP_LIST_UNCHAIN(x);
HIP_LIST_ADD_BACK(x, &hip->runnables);
hip_yield(hip); /* let other routines go first */
return len;
}
/* remember the data holder and places the current running routine in the send queue */
caller->chan_data_ptr = (hip_uint8_t*)ptr;
caller->chan_data_len = len;
caller->chan_ret_ptr = &ret; /* remember the pointer to the return value holder to be set by the receiver */
//printf ("REMEMBER SEND CHAT caller %p RET %p\n", caller, caller->chan_ret_ptr);
hip_switch(hip, &c->sendq);
//printf ("CALLER RETURNNING SEND caller %p, ret %p/%p => %d\n", caller, caller->chan_ret_ptr, &ret, ret);
return ret;
}
int hip_chan_recv(hip_chan_t* c, void* ptr, hip_oow_t len)
{
hip_t* hip;
hip_uctx_t* caller;
int ret;
hip = c->hip;
caller = UCTX_FROM_LINK(hip->running);
/* TODO: IS THERE DATA IN BUFFER ? */
if (!HIP_LIST_IS_EMPTY(&c->sendq)) /* there is a sending routine */
{
hip_uctx_link_t* x;
hip_uctx_t* xx;
/* remove the sending routine from the queue and make it runnable */
x = HIP_LIST_HEAD(&c->sendq);
/* copy the data from the sending routine's buffer */
xx = UCTX_FROM_LINK(x);
if (len > xx->chan_data_len) len = xx->chan_data_len; /* TODO: do something else instead of simple truncation */
memcpy(ptr, xx->chan_data_ptr, len);
//printf ("USE RECV CHAT caller %p sender %p RET %p\n",caller, xx, xx->chan_ret_ptr);
*(xx->chan_ret_ptr) = len; /* manipulate the return value of the sender */
HIP_LIST_UNCHAIN(x);
HIP_LIST_ADD_BACK(x, &hip->runnables);
hip_yield(hip); /* let other routines go first */
return len;
}
/* remember the data holder and place the calling routine in the receive queue */
caller->chan_data_ptr = ptr;
caller->chan_data_len = len;
caller->chan_ret_ptr = &ret; /* remember the pointer to the return value holder to be set by the sender */
//printf ("REMEMBER RECV caller %p CHAT RET %p\n", caller, caller->chan_ret_ptr);
hip_switch(hip, &c->recvq); /* switch to the scheduler */
//printf ("CALLER RETURNNING RECV caller %p, ret %p/%p => %d\n", caller, caller->chan_ret_ptr, &ret, ret);
return ret;
}
#if 0
int hip_chan_select(hip_chan_t* chans, hip_oow_t count, int nonblock)
{
/* returns the index to the chan that has data to read */
return -1;
}
#endif