Skip to content
Snippets Groups Projects
Commit 1e57c865 authored by Dmitry E. Oboukhov's avatar Dmitry E. Oboukhov
Browse files

ipc was realized properly

parent 6445380b
No related branches found
No related tags found
No related merge requests found
...@@ -34,15 +34,11 @@ ...@@ -34,15 +34,11 @@
const ev_tstamp IPC_TIMEOUT_INFINITY = 365*86400*100.0; const ev_tstamp IPC_TIMEOUT_INFINITY = 365*86400*100.0;
struct ipc_channel { struct ipc_channel {
struct rlist readers, writers; struct rlist readers, writers, bcast;
unsigned creaders;
unsigned cwriters;
unsigned size; unsigned size;
unsigned beg; unsigned beg;
unsigned count; unsigned count;
void *bcast_msg; void *bcast_msg;
void *item[0]; void *item[0];
}; };
...@@ -75,8 +71,7 @@ void ...@@ -75,8 +71,7 @@ void
ipc_channel_init(struct ipc_channel *ch) ipc_channel_init(struct ipc_channel *ch)
{ {
ch->beg = ch->count = 0; ch->beg = ch->count = 0;
ch->creaders = 0; rlist_init(&ch->bcast);
ch->cwriters = 0;
rlist_init(&ch->readers); rlist_init(&ch->readers);
rlist_init(&ch->writers); rlist_init(&ch->writers);
} }
...@@ -99,36 +94,48 @@ ipc_channel_cleanup(struct ipc_channel *ch) ...@@ -99,36 +94,48 @@ ipc_channel_cleanup(struct ipc_channel *ch)
void * void *
ipc_channel_get_timeout(struct ipc_channel *ch, ev_tstamp timeout) ipc_channel_get_timeout(struct ipc_channel *ch, ev_tstamp timeout)
{ {
struct fiber *f;
bool first_try = true;
ev_tstamp started = ev_now();
/* channel is empty */ /* channel is empty */
if (ch->count == 0 || ch->creaders >= ch->count) { while (ch->count == 0) {
rlist_add_tail_entry(&ch->readers, fiber, state);
ch->creaders++; /* try to be in FIFO order */
if (first_try) {
rlist_add_tail_entry(&ch->readers, fiber, state);
first_try = false;
} else {
rlist_add_entry(&ch->readers, fiber, state);
}
bool cancellable = fiber_setcancellable(true); bool cancellable = fiber_setcancellable(true);
bool timed_out = fiber_yield_timeout(timeout); fiber_yield_timeout(timeout);
rlist_del_entry(fiber, state); rlist_del_entry(fiber, state);
ch->creaders--;
/* broadcast messsage wakes us up */
if (!rlist_empty(&ch->bcast)) {
f = rlist_first_entry(&ch->bcast, struct fiber, state);
rlist_del_entry(f, state);
fiber_wakeup(f);
fiber_testcancel();
fiber_setcancellable(cancellable);
return ch->bcast_msg;
}
fiber_testcancel(); fiber_testcancel();
fiber_setcancellable(cancellable); fiber_setcancellable(cancellable);
if (timed_out) timeout -= ev_now() - started;
if (timeout <= 0)
return NULL; return NULL;
if (fiber->waiter) {
fiber_wakeup(fiber->waiter);
return ch->bcast_msg;
}
} }
assert(ch->count > 0);
void *res = ch->item[ch->beg]; void *res = ch->item[ch->beg];
if (++ch->beg >= ch->size) if (++ch->beg >= ch->size)
ch->beg -= ch->size; ch->beg -= ch->size;
ch->count--; ch->count--;
if (!rlist_empty(&ch->writers)) { if (!rlist_empty(&ch->writers)) {
struct fiber *f = f = rlist_first_entry(&ch->writers, struct fiber, state);
rlist_first_entry(&ch->writers, struct fiber, state);
rlist_del_entry(f, state); rlist_del_entry(f, state);
fiber_wakeup(f); fiber_wakeup(f);
} }
...@@ -147,28 +154,33 @@ int ...@@ -147,28 +154,33 @@ int
ipc_channel_put_timeout(struct ipc_channel *ch, void *data, ipc_channel_put_timeout(struct ipc_channel *ch, void *data,
ev_tstamp timeout) ev_tstamp timeout)
{ {
bool first_try = true;
ev_tstamp started = ev_now();
/* channel is full */ /* channel is full */
if (ch->count >= ch->size || ch->cwriters >= ch->size - ch->count) { while (ch->count >= ch->size) {
rlist_add_tail_entry(&ch->writers, fiber, state); /* try to be in FIFO order */
ch->cwriters++; if (first_try) {
rlist_add_tail_entry(&ch->writers, fiber, state);
first_try = false;
} else {
rlist_add_entry(&ch->writers, fiber, state);
}
bool cancellable = fiber_setcancellable(true); bool cancellable = fiber_setcancellable(true);
bool timed_out = fiber_yield_timeout(timeout); fiber_yield_timeout(timeout);
rlist_del_entry(fiber, state); rlist_del_entry(fiber, state);
ch->cwriters--;
fiber_testcancel(); fiber_testcancel();
fiber_setcancellable(cancellable); fiber_setcancellable(cancellable);
if (timed_out) { timeout -= ev_now() - started;
if (timeout <= 0) {
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }
} }
assert(ch->count < ch->size);
unsigned i = ch->beg; unsigned i = ch->beg;
i += ch->count; i += ch->count;
ch->count++; ch->count++;
...@@ -177,8 +189,8 @@ ipc_channel_put_timeout(struct ipc_channel *ch, void *data, ...@@ -177,8 +189,8 @@ ipc_channel_put_timeout(struct ipc_channel *ch, void *data,
ch->item[i] = data; ch->item[i] = data;
if (!rlist_empty(&ch->readers)) { if (!rlist_empty(&ch->readers)) {
struct fiber *f = struct fiber *f;
rlist_first_entry(&ch->readers, struct fiber, state); f = rlist_first_entry(&ch->readers, struct fiber, state);
rlist_del_entry(f, state); rlist_del_entry(f, state);
fiber_wakeup(f); fiber_wakeup(f);
} }
...@@ -194,45 +206,50 @@ ipc_channel_put(struct ipc_channel *ch, void *data) ...@@ -194,45 +206,50 @@ ipc_channel_put(struct ipc_channel *ch, void *data)
bool bool
ipc_channel_has_readers(struct ipc_channel *ch) ipc_channel_has_readers(struct ipc_channel *ch)
{ {
return ch->creaders > 0; return !rlist_empty(&ch->readers);
} }
bool bool
ipc_channel_has_writers(struct ipc_channel *ch) ipc_channel_has_writers(struct ipc_channel *ch)
{ {
return ch->cwriters > 0; return !rlist_empty(&ch->writers);
} }
int int
ipc_channel_broadcast(struct ipc_channel *ch, void *data) ipc_channel_broadcast(struct ipc_channel *ch, void *data)
{ {
/* broadcast in broadcast: marasmus */
if (!rlist_empty(&ch->bcast))
return 0;
/* there is no reader on channel */
if (rlist_empty(&ch->readers)) { if (rlist_empty(&ch->readers)) {
ipc_channel_put(ch, data); ipc_channel_put(ch, data);
return 1; return 1;
} }
unsigned readers = 0;
struct fiber *f; struct fiber *f;
int count = 0;
rlist_foreach_entry(f, &ch->readers, state) { rlist_foreach_entry(f, &ch->readers, state) {
count++; readers++;
} }
for (int i = 0; i < count && !rlist_empty(&ch->readers); i++) { unsigned cnt = 0;
struct fiber *f = while(!rlist_empty(&ch->readers)) {
rlist_first_entry(&ch->readers, struct fiber, state); f = rlist_first_entry(&ch->readers, struct fiber, state);
rlist_del_entry(f, state);
assert(f->waiter == NULL);
f->waiter = fiber;
ch->bcast_msg = data; ch->bcast_msg = data;
rlist_add_tail_entry(&ch->bcast, fiber, state);
fiber_wakeup(f); fiber_wakeup(f);
bool cancellable = fiber_setcancellable(true);
fiber_yield(); fiber_yield();
f->waiter = NULL; rlist_del_entry(fiber, state);
fiber_testcancel(); fiber_testcancel();
if (rlist_empty(&ch->readers)) { fiber_setcancellable(cancellable);
count = i; /* if any other reader was added don't wake it up */
if (++cnt >= readers)
break; break;
}
} }
return count; return cnt;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment