-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe.c
64 lines (51 loc) · 1.05 KB
/
pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <assert.h>
#include <err.h>
#include <pthread.h>
#include <stdlib.h>
#include "pipe.h"
#include "producer.h"
struct pipe {
pthread_t p_td;
struct producer *p_producer;
struct processor *p_processor;
struct consumer *p_consumer;
};
static void *pipe_main(void *);
struct pipe *
pipe_start(struct producer *producer, struct processor *processor, struct consumer *consumer)
{
struct pipe *p;
int error;
assert(producer != NULL);
assert(processor != NULL);
assert(consumer != NULL);
p = malloc(sizeof *p);
assert(p != NULL);
p->p_producer = producer;
p->p_processor = processor;
p->p_consumer = consumer;
error = pthread_create(&p->p_td, NULL, pipe_main, p);
if (error != 0) {
warnc(error, "pthread_create");
free(p);
return (NULL);
}
return (p);
}
void
pipe_wait(struct pipe *p)
{
int error;
error = pthread_join(p->p_td, NULL);
if (error != 0)
warnc(error, "pthread_join");
}
static void *
pipe_main(void *arg)
{
struct pipe *p;
p = arg;
for (;;) {
p->p_producer->p_produce(p->p_producer, p->p_processor, p->p_consumer);
}
}