/* $Id: element.c,v 1.7 2004/03/25 00:10:21 jmuelmen Exp $ */ #include "scheduler.h" #include "queue.h" #include "parse.h" #include #include #include #include queue_el_t *queue_el_create (func_t f, value_t *argv, int argc, value_t *ret) { int i; queue_el_t *l = malloc(sizeof(queue_el_t)); l->f = f; /* copy the arguments */ for (i = 0; i < argc; ++i) l->argv[i] = argv[i]; l->argc = argc; l->ret = ret; return l; } void queue_el_destroy (queue_el_t *l) { free(l); } /* thread function that does the actual evaluating */ void *queue_el_execute (void *v) { queue_el_t *l = (queue_el_t *)v; value_t ret = l->f(l->argv, l->argc); /* copy the return value into the right place */ pthread_mutex_lock(&l->ret->mut); value_copy(l->ret, &ret); l->ret->returned = 1; pthread_mutex_unlock(&l->ret->mut); /* then signal that the function call has returned */ pthread_cond_signal(&l->ret->has_returned); queue_el_destroy(l); return NULL; } void queue_el_process (queue_el_t *l) { /* detach a thread that does the actual evaluating; return immediately */ int err; pthread_t proc_thr; err = pthread_create(&proc_thr, NULL, queue_el_execute, l); if (err) perror("creating queue element evaluation thread"); err = pthread_detach(proc_thr); if (err) perror("detaching queue element evaluation thread"); }