-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchan.c
65 lines (54 loc) · 1.21 KB
/
chan.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
65
#include <stdlib.h>
#include <unistd.h>
#include "pipe_util.h"
#include "chan.h"
int mk_chan(Channel *c, size_t elsize)
{
if (mk_pipe(&(c->data_chan)) != 0)
goto BAD_DATA_PIPE;
if (mk_pipe(&(c->barrier)) != 0)
goto BAD_SYNCH_PIPE;
c->elsize = elsize;
return 1;
BAD_SYNCH_PIPE:
close_pipe(&(c->data_chan));
BAD_DATA_PIPE:
return -1;
}
void free_chan(Channel *c)
{
close_pipe(&(c->data_chan));
close_pipe(&(c->barrier));
}
int send(Channel *c, const void *data)
{
ssize_t ret;
size_t sent=0;
char d = 0;
do {
ret = write(c->data_chan.writer, data, c->elsize-sent);
if (ret < 0)
return -1;
data = (char *)data + ret;
} while ((sent += ret) < c->elsize);
/* block until receiver gets data */
read(c->barrier.reader, &d, 1);
return 0;
}
int recv(Channel *c, void *data)
{
ssize_t ret;
size_t recved=0;
char d = 0;
do {
ret = read(c->data_chan.reader, data, c->elsize-recved);
if (ret <= 0)
return -1;
data = (char *)data + ret;
} while ((recved += ret) < c->elsize);
/* read blocks on sends for us, so unblock senders */
/* NOTE: do not put this before the read loop or we can get deadlocks
when the kernel's pipe buffer fills up. */
write(c->barrier.writer, &d, 1);
return 0;
}