diff options
Diffstat (limited to 'parapat.c')
| -rw-r--r-- | parapat.c | 438 |
1 files changed, 438 insertions, 0 deletions
diff --git a/parapat.c b/parapat.c new file mode 100644 index 0000000..f51c4a4 --- /dev/null +++ b/parapat.c @@ -0,0 +1,438 @@ +#include <stdlib.h> +#include <pthread.h> + +#include "debug.h" +#include "queue.h" + +#include "parapat.h" + +typedef struct { + emitter *efn; + void *arg; +} handle_emitter_info; + +typedef struct { + worker *wfn; + void *arg; +} handle_worker_info; + +typedef struct { + collector *cfn; + void *arg; +} handle_collector_info; + +void *handle_emitter(queue *out, void *v) { + handle_emitter_info *hei = v; + + int keep_going; + void *x; + do { + debug(("running emitter\n")); + keep_going = hei->efn(&x, hei->arg) == 0; + enqueue(out, x); + } while (keep_going); + + debug(("emitter completed\n")); + + pq_close(out); + + return NULL; +} + +void *handle_collector(queue *in, void *v) { + handle_collector_info *hci = v; + + void *out; + + while (1) { + void *res; + void *temp; + + int stop = dequeue(in, &res); + if (stop) { + break; + } + + temp = (void *) out; + debug(("running collector on %p and %p\n", temp, res)); + + out = hci->cfn(temp, res, hci->arg); + } + + debug(("collector completed, final value %p\n", out)); + + return out; +} + +void *handle_worker(queue *in, queue *out, void *v) { + handle_worker_info *hwi = v; + + while (1) { + void *arg; + + int stop = dequeue(in, &arg); + if (stop) { + break; + } + + debug(("running worker on %p\n", arg)); + enqueue(out, hwi->wfn(arg, hwi->arg)); + } + + debug(("worker completed\n")); + + pq_close(out); + + return NULL; +} + +typedef struct pipe_piece { + pthread_t t_id; + pthread_t prev_t_id; + worker *fn; + queue *in; + queue *out; +} pipe_piece; + +typedef struct { + pipe_piece *pieces; + int piece_count; + queue *in; + queue *out; +} pipeline; + +typedef struct { + queue_emitter *efn; + queue *out; + void *arg; +} queue_emitter_void_info; + +void *queue_emitter_void(void *v) { + queue_emitter_void_info *qevi = v; + qevi->efn(qevi->out, qevi->arg); + return 0; +} + +typedef struct { + queue_worker *wfn; + queue *in; + queue *out; + void *arg; +} queue_worker_void_info; + +void *queue_worker_void(void *v) { + queue_worker_void_info *qwvi = v; + qwvi->wfn(qwvi->in, qwvi->out, qwvi->arg); + return 0; +} + +typedef struct { + queue_collector *cfn; + queue *in; + void *arg; +} queue_collector_void_info; + +void *queue_collector_void(void *v) { + queue_collector_void_info *qcvi = v; + return qcvi->cfn(qcvi->in, qcvi->arg); +} + +void *install_farm_queue(queue_worker *wfn, int n, queue *in, queue *out, + void *arg) { + + queue_worker_void_info qwvi; + int i; + + pthread_t *t_ids = malloc(sizeof *t_ids * n); + + qwvi.in = in; + qwvi.out = out; + qwvi.wfn = wfn; + qwvi.arg = arg; + + for (i = 0; i < n; ++i) { + pthread_create(t_ids + i, NULL, queue_worker_void, &qwvi); + debug(("thread %lu created\n", t_ids[i])); + } + + for (i = 0; i < n; ++i) { + pthread_join(t_ids[i], NULL); + debug(("thread %lu joined\n", t_ids[i])); + } + + pq_close(out); + + return NULL; +} + +void *install_farm(worker *wfn, int n, queue *in, queue *out, void *arg) { + handle_worker_info hwi; + hwi.wfn = wfn; + hwi.arg = arg; + return install_farm_queue(handle_worker, n, in, out, &hwi); +} + +typedef struct { + queue_worker *wfn; + int n; + void *arg; +} install_farm_worker_info; + +void *install_farm_worker(queue *in, queue *out, void *arg) { + install_farm_worker_info *ifqwi = arg; + return install_farm_queue(ifqwi->wfn, ifqwi->n, in, out, ifqwi->arg); +} + +typedef struct { + worker *wfn; + int n; + queue *in; + queue *out; + void *arg; +} install_farm_void_info; + +void *install_farm_void(void *v) { + install_farm_void_info *ifvi = v; + return install_farm(ifvi->wfn, ifvi->n, ifvi->in, ifvi->out, ifvi->arg); +} + +void *create_queue_base(queue_emitter *efn, queue_worker *wfn, + queue_collector *cfn, void *efn_arg, void *wfn_arg, void *cfn_arg) { + + pthread_t etid, wtid, ctid; + + queue_emitter_void_info qevi; + queue_worker_void_info qwvi; + queue_collector_void_info qcvi; + + queue in, out; + + void *result; + + qevi.efn = efn; + qevi.out = ∈ + qevi.arg = efn_arg; + + qwvi.wfn = wfn; + qwvi.in = ∈ + qwvi.out = &out; + qwvi.arg = wfn_arg; + + qcvi.cfn = cfn; + qcvi.in = &out; + qcvi.arg = cfn_arg; + + init_queue(&in); + init_queue(&out); + + pthread_create(&etid, NULL, queue_emitter_void, &qevi); + pthread_create(&wtid, NULL, queue_worker_void, &qwvi); + pthread_create(&ctid, NULL, queue_collector_void, &qcvi); + + pthread_join(etid, &result); + if (result) { + return result; + } + + pthread_join(wtid, &result); + if (result) { + return result; + } + + pthread_join(ctid, &result); + return result; +} + +void *create_queue_farm(queue_emitter *efn, queue_worker *wfn, + queue_collector *cfn, int n, void *efn_arg, void *wfn_arg, + void *cfn_arg) { + + install_farm_worker_info ifwi; + ifwi.wfn = wfn; + ifwi.n = n; + ifwi.arg = wfn_arg; + + return create_queue_base(efn, install_farm_worker, cfn, efn_arg, &ifwi, + cfn_arg); +} + +void *create_farm(emitter *efn, worker *wfn, collector *cfn, int n, + void *efn_arg, void *wfn_arg, void *cfn_arg) { + + handle_emitter_info hei; + handle_worker_info hwi; + handle_collector_info hci; + + hei.efn = efn; + hei.arg = efn_arg; + + hwi.wfn = wfn; + hwi.arg = wfn_arg; + + hci.cfn = cfn; + hci.arg = cfn_arg; + + return create_queue_farm(handle_emitter, handle_worker, handle_collector, + n, &hei, &hwi, &hci); +} + +void *install_queue_pipeline(queue_worker **wfns, int n, queue *in, + queue *out, void **args) { + + int i; + queue_worker_void_info *pieces; + queue *queues; + pthread_t *tids; + + void *result; + + if (!n) { + return NULL; + } + + pieces = malloc(sizeof *pieces * n); + queues = malloc(sizeof *queues * (n - 1)); + tids = malloc(sizeof *tids * n); + + /* + * Initialise new queues. + */ + for (i = 0; i < n - 1; ++i) { + init_queue(queues + i); + } + + /* + * Set input queues. + */ + pieces[0].in = in; + for (i = 1; i < n; ++i) { + pieces[i].in = &queues[i - 1]; + } + + /* + * Set output queues. + */ + for (i = 0; i < n - 1; ++i) { + pieces[i].out = &queues[i]; + } + pieces[n - 1].out = out; + + /* + * Initialise a thread for each stage of the pipeline. + */ + for (i = 0; i < n; ++i) { + queue_worker_void_info *qwvi = pieces + i; + qwvi->wfn = wfns[i]; + qwvi->arg = args[i]; + + pthread_create(&tids[i], NULL, queue_worker_void, qwvi); + } + + /* + * Wait for all stages t complete. + */ + for (i = 0; i < n; ++i) { + pthread_join(tids[i], &result); + } + + /* + * Waiting on all previous stages is required to ensure freeing resources is + * safe. + */ + free(pieces); + free(queues); + + /* + * Forward any issue from individual stages. + */ + return result; +} + +void *install_pipeline(worker **wfns, int n, queue *in, queue *out, + void **args) { + + int i; + handle_worker_info *hwis = malloc(sizeof *hwis * n); + handle_worker_info **hwips = malloc(sizeof *hwips * n); + queue_worker **hwfns = malloc(sizeof *hwfns * n); + void *result; + + for (i = 0; i < n; ++i) { + hwfns[i] = handle_worker; + hwis[i].wfn = wfns[i]; + hwis[i].arg = args[i]; + hwips[i] = &hwis[i]; + } + + result = install_queue_pipeline(hwfns, n, in, out, (void **) hwips); + + free(hwis); + free(hwips); + free(hwfns); + + return result; +} + +typedef struct { + queue_worker **wfns; + int n; + void **args; +} install_pipeline_worker_info; + +void *install_pipeline_worker(queue *in, queue *out, void *v) { + install_pipeline_worker_info *ipwi = v; + return install_queue_pipeline(ipwi->wfns, ipwi->n, in, out, ipwi->args); +} + +void *create_queue_pipeline(queue_emitter *efn, queue_worker **wfns, int n, + queue_collector *cfn, void *efn_arg, void **wfn_args, void *cfn_arg) { + + install_pipeline_worker_info ipwi; + ipwi.wfns = wfns; + ipwi.n = n; + ipwi.args = wfn_args; + + return create_queue_base(efn, install_pipeline_worker, cfn, efn_arg, &ipwi, + cfn_arg); +} + +void *create_pipeline(emitter *efn, worker **wfns, int n, collector *cfn, + void *efn_arg, void **wfn_args, void *cfn_arg) { + + handle_emitter_info hei; + handle_worker_info *hwis = malloc(sizeof *hwis * n); + handle_collector_info hci; + + queue_worker **hwfns = malloc(sizeof *hwfns * n); + handle_worker_info **hwips = malloc(sizeof *hwips * n); + + void *result; + + int i; + for (i = 0; i < n; ++i) { + hwfns[i] = handle_worker; + hwis[i].wfn = wfns[i]; + hwips[i] = &hwis[i]; + + if (wfn_args) { + hwis[i].arg = wfn_args[i]; + } else { + hwis[i].arg = NULL; + } + } + + hei.efn = efn; + hei.arg = efn_arg; + + hci.cfn = cfn; + hci.arg = cfn_arg; + + result = create_queue_pipeline(handle_emitter, hwfns, n, handle_collector, + &hei, (void **) hwips, &hci); + + free(hwis); + free(hwips); + free(hwfns); + + return result; +} |
