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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/* Generic one-way pipes
Copyright (C) 1995 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <string.h> /* For bzero() */
#include <assert.h>
#include <mach/time_value.h>
#include <mach/mach_host.h>
#include "pipe.h"
static inline void
timestamp (time_value_t *stamp)
{
host_get_time (mach_host_self (), stamp);
}
/* ---------------------------------------------------------------- */
/* Creates a new pipe of class CLASS and returns it in RESULT. */
error_t
pipe_create (struct pipe_class *class, struct pipe **pipe)
{
struct pipe *new = malloc (sizeof (struct pipe));
if (new == NULL)
return ENOMEM;
new->refs = 0;
new->flags = 0;
new->class = class;
bzero (&new->read_time, sizeof (new->read_time));
bzero (&new->write_time, sizeof (new->write_time));
condition_init (&new->pending_reads);
condition_init (&new->pending_selects);
mutex_init (&new->lock);
new->interrupt_seq_num = 0;
pq_create (&new->queue);
*pipe = new;
return 0;
}
/* Free PIPE and any resources it holds. */
void
pipe_free (struct pipe *pipe)
{
pq_free (pipe->queue);
free (pipe);
}
/* Wake up all threads waiting on PIPE, which should be locked. */
inline void
pipe_kick (struct pipe *pipe)
{
/* Now wake them all up for the bad news... */
condition_broadcast (&pipe->pending_reads);
mutex_unlock (&pipe->lock);
condition_broadcast (&pipe->pending_selects);
mutex_lock (&pipe->lock); /* Get back the lock on PIPE. */
}
/* Discard a reference to PIPE, which should be unlocked, being sure to make
users aware of this. */
void
pipe_break (struct pipe *pipe)
{
mutex_lock (&pipe->lock);
/* As there may be multiple writers on a connectionless socket, we
never allow EOF to be signaled on the reader. */
if (! (pipe->class->flags & PIPE_CLASS_CONNECTIONLESS))
pipe->flags |= PIPE_BROKEN;
if (pipe->refs > 1)
/* Other references to PIPE besides ours? Wake 'em up. */
pipe_kick (pipe);
pipe_release (pipe);
}
/* Writes up to LEN bytes of DATA, to PIPE, which should be locked, and
returns the amount written in AMOUNT. If present, the information in
CONTROL & PORTS is written in a preceding control packet. If an error is
returned, nothing is done. */
error_t
pipe_send (struct pipe *pipe, void *source,
char *data, size_t data_len,
char *control, size_t control_len,
mach_port_t *ports, size_t num_ports,
size_t *amount)
{
error_t err = 0;
if (pipe->flags & PIPE_BROKEN)
return EPIPE;
if (control_len > 0 || num_ports > 0)
/* Write a control packet. */
{
/* Note that we don't record the source address in control packets, as
it's recorded in the following data packet anyway, and this prevents
it from being dealloc'd twice; this depends on the fact that we
always write a data packet. */
struct packet *control_packet =
pq_queue (pipe->queue, PACKET_TYPE_CONTROL, NULL);
if (control_packet == NULL)
err = ENOBUFS;
else
{
err = packet_write (control_packet, control, control_len, NULL);
if (!err)
err = packet_set_ports (control_packet, ports, num_ports);
if (err)
/* Trash CONTROL_PACKET somehow XXX */;
}
}
if (!err)
err = (*pipe->class->write)(pipe->queue, source, data, data_len, amount);
if (!err)
{
timestamp (&pipe->write_time);
/* And wakeup anyone that might be interested in it. */
condition_signal (&pipe->pending_reads);
mutex_unlock (&pipe->lock);
mutex_lock (&pipe->lock); /* Get back the lock on PIPE. */
/* Only wakeup selects if there's still data available. */
if (pipe_is_readable (pipe, 0))
{
condition_signal (&pipe->pending_selects);
/* We leave PIPE locked here, assuming the caller will soon unlock
it and allow others access. */
}
}
return err;
}
/* Reads up to AMOUNT bytes from PIPE, which should be locked, into DATA, and
returns the amount read in DATA_LEN. If NOBLOCK is true, EWOULDBLOCK is
returned instead of block when no data is immediately available. If an
error is returned, nothing is done. If source isn't NULL, the address of
the socket from which the data was sent is returned in it; this may be
NULL if it wasn't specified by the sender (which is usually the case with
connection-oriented protcols).
If there is control data waiting (before any data), then the behavior
depends on whether this is an `ordinary read' (when CONTROL and PORTS are
both NULL), in which case any control data is skipped, or a `msg read', in
which case the contents of the first control packet is returned (in
CONTROL and PORTS), as well as the first data packet following that (if
the control packet is followed by another control packet or no packet in
this case, a zero length data buffer is returned; the user should be
careful to distinguish this case from EOF (when no control or ports data
is returned either). */
error_t
pipe_recv (struct pipe *pipe, int noblock, unsigned *flags, void **source,
char **data, size_t *data_len, size_t amount,
char **control, size_t *control_len,
mach_port_t **ports, size_t *num_ports)
{
error_t err;
struct packet *packet;
struct pq *pq = pipe->queue;
/* True if the user isn't asking for any `control' data. */
int data_only = (control == NULL && ports == NULL);
err = pipe_wait (pipe, noblock, data_only);
if (err)
return err;
packet = pq_head (pq, PACKET_TYPE_ANY, 0);
if (data_only)
/* The user doesn't want to know about control info, so skip any... */
while (packet && packet->type == PACKET_TYPE_CONTROL)
packet = pq_next (pq, PACKET_TYPE_ANY, 0);
else if (packet && packet->type == PACKET_TYPE_CONTROL)
/* Read this control packet first, before looking for a data packet. */
{
void *control_source;
if (control != NULL)
packet_read (packet, control, control_len, packet_readable (packet));
if (ports != NULL)
/* Copy out the port rights being sent. */
packet_read_ports (packet, ports, num_ports);
packet_read_source (packet, &control_source);
packet = pq_next (pq, PACKET_TYPE_DATA, control_source);
/* Control packets should only have a source address if they're not
followed by a data packet. */
assert (!!packet == !control_source);
if (!packet)
if (source)
/* Since there is no data, say where the control data came from. */
*source = control_source;
else
pipe_dealloc_addr (control_source);
}
else
/* No control data... */
{
if (control_len)
*control_len = 0;
if (num_ports)
*num_ports = 0;
}
if (!err)
if (packet)
/* Read some data (PACKET must be a data packet at this point). */
{
int dq = 1; /* True if we should dequeue this packet. */
if (source)
packet_read_source (packet, source);
err = (*pipe->class->read)(packet, &dq, flags, data, data_len, amount);
if (dq)
pq_dequeue (pq);
}
else
/* Return EOF. */
*data_len = 0;
if (!err && packet)
timestamp (&pipe->read_time);
return err;
}
|