irctk

libircclient binding for scripts
git clone https://a3nm.net/git/irctk/
Log | Files | Refs | README

commit aa4687d695556157dd851c6ed959f9e637108ead
parent 9ddbee573566ad0106d1583a3862e15417480ee6
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 17 Jun 2012 01:00:55 +0200

fix fifos

Diffstat:
irctk.c | 88++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 49 insertions(+), 39 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -464,59 +464,66 @@ typedef struct fifo { pthread_mutex_t mutex; } fifo; -int full(fifo f) { - return f.hd == f.tl; +int size(fifo *f) { + return (f->tl - f->hd + LINE_BUFFER) % LINE_BUFFER; } -int empty(fifo f) { - return ((f.tl - f.hd + LINE_BUFFER) % LINE_BUFFER) == 1; +int full(fifo *f) { + return size(f) == LINE_BUFFER - 1; +} +int empty(fifo *f) { + return size(f) == 0; } -line pop(fifo f) { +line pop(fifo *f) { line result; int was_full; - pthread_mutex_lock(&f.mutex); - while (empty(f)) - pthread_cond_wait(&f.empty, &f.mutex); + pthread_mutex_lock(&f->mutex); + debug("am popping, hd %d tl %d size %d", f->hd, f->tl, size(f)); + while (empty(f)) { + debug("wait nonempty"); + pthread_cond_wait(&f->empty, &f->mutex); + } + debug("nonempty"); // now fifo isn't empty was_full = full(f); - result = f.queue[f.hd]; - f.hd++; - f.hd %= LINE_BUFFER; + result = f->queue[f->hd]; + f->hd++; + f->hd %= LINE_BUFFER; if (was_full) - pthread_cond_signal(&f.full); - pthread_mutex_unlock(&f.mutex); + pthread_cond_signal(&f->full); + pthread_mutex_unlock(&f->mutex); return result; } -void push(fifo f, char* l, char* li, char* lo) { +void push(fifo *f, char* l, char* li, char* lo) { int was_empty; - pthread_mutex_lock(&f.mutex); + pthread_mutex_lock(&f->mutex); while (full(f)) - pthread_cond_wait(&f.full, &f.mutex); + pthread_cond_wait(&f->full, &f->mutex); // now fifo isn't full was_empty = empty(f); - f.queue[f.tl].line = l; - f.queue[f.tl].last_in = li; - f.queue[f.tl].last_out = lo; - f.tl++; - f.tl %= LINE_BUFFER; + f->queue[f->tl].line = l; + f->queue[f->tl].last_in = li; + f->queue[f->tl].last_out = lo; + f->tl++; + f->tl %= LINE_BUFFER; if (was_empty) - pthread_cond_signal(&f.empty); - pthread_mutex_unlock(&f.mutex); + pthread_cond_signal(&f->empty); + pthread_mutex_unlock(&f->mutex); } -void init(fifo f) { - f.hd = 0; - f.tl = 1; - assert(!pthread_cond_init(&f.empty, NULL)); - assert(!pthread_cond_init(&f.full, NULL)); - assert(!pthread_mutex_init(&f.mutex, NULL)); +void init(fifo *f) { + f->hd = 0; + f->tl = 0; + assert(!pthread_cond_init(&f->empty, NULL)); + assert(!pthread_cond_init(&f->full, NULL)); + assert(!pthread_mutex_init(&f->mutex, NULL)); } -void destroy(fifo f) { - pthread_cond_destroy(&f.empty); - pthread_cond_destroy(&f.full); - pthread_mutex_destroy(&f.mutex); +void destroy(fifo *f) { + pthread_cond_destroy(&f->empty); + pthread_cond_destroy(&f->full); + pthread_mutex_destroy(&f->mutex); } // fifos for input and output @@ -746,7 +753,8 @@ void manage_event (irc_session_t *session, const char *event, const char *origin info("Cannot join channel: channel is invite-only."); } - debug("Event \"%s\", origin: \"%s\", params: %d", event, origin ? origin : "NULL", cnt); + debug("Event \"%s\", origin: \"%s\", params: %d", event, + origin ? origin : "NULL", cnt); } // Handle a nick event @@ -994,10 +1002,11 @@ static void* fifo_in_thread (void *arg) { line = (char*) malloc(size+1); while ((res = getline((char**) &line, (size_t*) &size, stdin)) != -1) { - push(fifo_in, line, args.last_chan_in, args.last_chans_out); + debug("i read and push %s\n", line); + push(&fifo_in, line, args.last_chan_in, args.last_chans_out); } // sentinel - push(fifo_in, NULL, NULL, NULL); + push(&fifo_in, NULL, NULL, NULL); free(line); return 0; } @@ -1023,6 +1032,7 @@ int start (int max_wait) debug("Connection request successful!"); + debug("hd %d tl %d\n", fifo_in.hd, fifo_in.tl); debug("Starting threads..."); if (pthread_create (&tid_irc, 0, irc_thread, (void*) s) != 0) die(E_THREAD, "Could not create thread: %s", strerror(errno)); @@ -1058,7 +1068,7 @@ int start (int max_wait) } } - while ((l = pop(fifo_in)).line) { + while ((l = pop(&fifo_in)).line) { debug("startloop : got %s, waiting", l.line); if (!args.ready) { info("Connection lost, reconnecting..."); @@ -1130,8 +1140,8 @@ int main (int argc, char **argv) strncpy(args.last_chans_out, first_chan(), MAX_LEN-1); // initialize the fifos - init(fifo_in); - init(fifo_out); + init(&fifo_in); + init(&fifo_out); // start trying to connet with the initial retry interval return start(args.retry_after);