/* $Id: ifbuf.c,v 1.15 2004/04/03 04:17:37 jmuelmen Exp $ */ #include "scheduler.h" #include "buf.h" #include "parse.h" #include "shell.h" #include #include #include #include static buf_t _ifbuf; inline void ifbuf_init () { buf_init(&_ifbuf); } void ifbuf_lock () { pthread_mutex_lock(&_ifbuf.mut); } void ifbuf_unlock () { pthread_mutex_unlock(&_ifbuf.mut); } int ifbuf_buf (parse_action_t *a) { int retval; retval = buf_buf(&_ifbuf, a); return retval; } inline void ifbuf_dump () { printf("If buffer:\n"); buf_dump(&_ifbuf); } int ifbuf_process_segment (parse_action_t ***start, parse_action_t *end) { return buf_process_segment(&_ifbuf, start, end, 0); } int ifbuf_process () { parse_action_t **i; /* lock up */ pthread_mutex_lock(&_ifbuf.mut); /* do our thing */ i = _ifbuf.actions; for (; i < _ifbuf.actions + _ifbuf.n_actions; ++i) { /* remember where we are */ parse_action_t *a = *i; value_t *v; /* process the predicate first */ ++i; ifbuf_process_segment(&i, a->statement.statement.eop); /* *a->args[0] holds the return value of the predicate now */ v = a->args[0]; /* wait until the predicate has been evaluated */ pthread_mutex_lock(&v->mut); while (!v->returned) { #ifdef WINDOWS_IS_DOGSHIT if (usleep(WINDOWS_IS_DOGSHIT)) perror("Sleeping on predicate eval"); #endif pthread_cond_wait(&v->has_returned, &v->mut); } /* if the predicate comes out true, process the consequent */ if ((v->val_type == val_int && v->value.v_int) || (v->val_type == val_float && v->value.v_float) || (v->val_type == val_string && strlen(v->value.v_string))) { pthread_mutex_unlock(&v->mut); ++i; ifbuf_process_segment(&i, a->statement.statement.eos); } else pthread_mutex_unlock(&v->mut); /* advance the pointer to the end of the statement */ while (*i != a->statement.statement.eos) ++i; } /* unlock */ pthread_mutex_unlock(&_ifbuf.mut); return 0; } void *ifbuf (void *arg) { /* run every so often and process the ifbuf */ while (1) { long sleep_time; int ret; struct timeval tv; /* printf("Processing ifbuf\n"); */ ifbuf_process(); /* figure out how long it is to the next full 10 ms */ gettimeofday(&tv, NULL); sleep_time = IFBUF_INT - tv.tv_usec % IFBUF_INT; ret = usleep(sleep_time); if (ret) { perror("Sleeping ifbuf"); } } }