summaryrefslogtreecommitdiff
path: root/libpipe/pipe.h
blob: 964329907a513c5382746400bb563ca4652a7e98 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* Generic one-way pipes

   Copyright (C) 1995, 1996 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. */

#ifndef __PIPE_H__
#define __PIPE_H__

#define EWOULDBLOCK EAGAIN /* XXX */

#include <cthreads.h>		/* For conditions & mutexes */
#include <features.h>

#ifdef PIPE_DEFINE_EI
#define PIPE_EI
#else
#define PIPE_EI __extern_inline
#endif

#include "pq.h"


/* A description of a class of pipes and how to operate on them.  */
struct pipe_class
{
  /* What sort of socket this corresponds too.  */
  int sock_type;

  /* Flags, from PIPE_CLASS_*, below.  */
  unsigned flags;

  /* Operations: */
  /* Read from PACKET into DATA &c, and set *DEQUEUE to true if PACKET should
     be subsequently discarded.  */
  error_t (*read)(struct packet *packet, int *dequeue, unsigned *flags,
		  char **data, size_t *data_len, size_t amount);
  /* Write DATA &c into the packet queue PQ.  */
  error_t (*write)(struct pq *pq, void *source,
		   char *data, size_t data_len, size_t *amount);
};

/* pipe_class flags  */
#define PIPE_CLASS_CONNECTIONLESS	0x1 /* A non-stream protocol.  */

/* Some pre-defined pipe_classes.  */
extern struct pipe_class *stream_pipe_class;
extern struct pipe_class *dgram_pipe_class;
extern struct pipe_class *seqpack_pipe_class;

/* A unidirectional data pipe; it transfers data from READER to WRITER.  */
struct pipe
{
  /* What kind of pipe we are.  */
  struct pipe_class *class;

  /* We use this to keep track of active threads using this pipe, so that
     while a thread is waiting to read from a pipe and that pipe gets
     deallocated (say by socket_shutdown), it doesn't actually go away until
     the reader realizes what happened.  It is normally frobbed using
     pipe_acquire & pipe_release, which do locking as well..  */
  unsigned readers, writers;

  /* Various flags, from PIPE_* below.  */
  unsigned flags;

  /* Various timestamps for this pipe.  */
  time_value_t read_time;
  time_value_t write_time;

  struct condition pending_reads;
  struct condition pending_read_selects;

  struct condition pending_writes;
  struct condition pending_write_selects;

  /* The maximum number of characters that this pipe will hold without
     further writes blocking.  */
  size_t write_limit;

  /* Write requests of less than this much are always done atomically.  */
  size_t write_atomic;

  struct mutex lock;

  /* A queue of incoming packets, of type either PACKET_TYPE_DATA or
     PACKET_TYPE_CONTROL.  Each data packet represents one datagram for
     protocols that maintain record boundaries.  Control packets always
     represent the control information to be returned from one read
     operation, and will be returned in conjunction with the following data
     packet (if any).  Reads interested only in data just skip control
     packets until they find a data packet.  */
  struct pq *queue;
};

/* Pipe flags.  */
#define PIPE_BROKEN	0x1	/* This pipe isn't connected.  */


extern size_t pipe_readable (struct pipe *pipe, int data_only);

extern int pipe_is_readable (struct pipe *pipe, int data_only);

extern error_t pipe_wait_readable (struct pipe *pipe, int noblock, int data_only);

extern error_t pipe_select_readable (struct pipe *pipe, int data_only);

extern error_t pipe_wait_writable (struct pipe *pipe, int noblock);

extern error_t pipe_select_writable (struct pipe *pipe);

#if defined(__USE_EXTERN_INLINES) || defined(PIPE_DEFINE_EI)

/* Returns the number of characters quickly readable from PIPE.  If DATA_ONLY
   is true, then `control' packets are ignored.  */
PIPE_EI size_t
pipe_readable (struct pipe *pipe, int data_only)
{
  size_t readable = 0;
  struct pq *pq = pipe->queue;
  struct packet *packet = pq_head (pq, PACKET_TYPE_ANY, NULL);
  while (packet)
    {
      if (packet->type == PACKET_TYPE_DATA)
	readable += packet_readable (packet);
      packet = packet->next;
    }
  return readable;
}

/* Returns true if there's any data available in PIPE.  If DATA_ONLY is true,
   then `control' packets are ignored.  Note that this is different than
   (pipe_readable (PIPE) > 0) in the case where a control packet containing
   only ports is present.  */
PIPE_EI int
pipe_is_readable (struct pipe *pipe, int data_only)
{
  struct pq *pq = pipe->queue;
  struct packet *packet = pq_head (pq, PACKET_TYPE_ANY, NULL);
  if (data_only)
    while (packet && packet->type == PACKET_TYPE_CONTROL)
      packet = packet->next;
  return (packet != NULL);
}

/* Waits for PIPE to be readable, or an error to occur.  If NOBLOCK is true,
   this operation will return EWOULDBLOCK instead of blocking when no data is
   immediately available.  If DATA_ONLY is true, then `control' packets are
   ignored.  */
PIPE_EI error_t
pipe_wait_readable (struct pipe *pipe, int noblock, int data_only)
{
  while (! pipe_is_readable (pipe, data_only) && ! (pipe->flags & PIPE_BROKEN))
    {
      if (noblock)
	return EWOULDBLOCK;
      if (hurd_condition_wait (&pipe->pending_reads, &pipe->lock))
	return EINTR;
    }
  return 0;
}

/* Waits for PIPE to be readable, or an error to occur.  This call only
   returns once threads waiting using pipe_wait_readable have been woken and
   given a chance to read, and if there is still data available thereafter.
   If DATA_ONLY is true, then `control' packets are ignored.  */
PIPE_EI error_t
pipe_select_readable (struct pipe *pipe, int data_only)
{
  while (! pipe_is_readable (pipe, data_only) && ! (pipe->flags & PIPE_BROKEN))
    if (hurd_condition_wait (&pipe->pending_read_selects, &pipe->lock))
      return EINTR;
  return 0;
}

/* Block until data can be written to PIPE.  If NOBLOCK is true, then
   EWOULDBLOCK is returned instead of blocking if this can't be done
   immediately.  */
PIPE_EI error_t
pipe_wait_writable (struct pipe *pipe, int noblock)
{
  size_t limit = pipe->write_limit;
  if (pipe->flags & PIPE_BROKEN)
    return EPIPE;
  while (pipe_readable (pipe, 1) >= limit)
    {
      if (noblock)
	return EWOULDBLOCK;
      if (hurd_condition_wait (&pipe->pending_writes, &pipe->lock))
	return EINTR;
      if (pipe->flags & PIPE_BROKEN)
	return EPIPE;
    }
  return 0;
}

/* Block until some data can be written to PIPE.  This call only returns once
   threads waiting using pipe_wait_writable have been woken and given a
   chance to write, and if there is still space available thereafter.  */
PIPE_EI error_t
pipe_select_writable (struct pipe *pipe)
{
  size_t limit = pipe->write_limit;
  while (! (pipe->flags & PIPE_BROKEN) && pipe_readable (pipe, 1) >= limit)
    if (hurd_condition_wait (&pipe->pending_writes, &pipe->lock))
      return EINTR;
  return 0;
}

#endif /* Use extern inlines.  */

/* Creates a new pipe of class CLASS and returns it in RESULT.  */
error_t pipe_create (struct pipe_class *class, struct pipe **pipe);

/* Free PIPE and any resources it holds.  */
void pipe_free (struct pipe *pipe);

/* Take any actions necessary when PIPE acquires its first reader.  */ 
void _pipe_first_reader (struct pipe *pipe);

/* Take any actions necessary when PIPE acquires its first writer.  */ 
void _pipe_first_writer (struct pipe *pipe);

/* Take any actions necessary when PIPE's last reader has gone away.  PIPE
   should be locked.  */
void _pipe_no_readers (struct pipe *pipe);

/* Take any actions necessary when PIPE's last writer has gone away.  PIPE
   should be locked.  */
void _pipe_no_writers (struct pipe *pipe);

extern void pipe_acquire_reader (struct pipe *pipe);

extern void pipe_acquire_writer (struct pipe *pipe);

extern void pipe_release_reader (struct pipe *pipe);

extern void pipe_release_writer (struct pipe *pipe);

extern void pipe_add_reader (struct pipe *pipe);

extern void pipe_add_writer (struct pipe *pipe);

extern void pipe_remove_reader (struct pipe *pipe);

extern void pipe_remove_writer (struct pipe *pipe);

extern void pipe_drain (struct pipe *pipe);

#if defined(__USE_EXTERN_INLINES) || defined(PIPE_DEFINE_EI)

/* Lock PIPE and increment its readers count.  */
PIPE_EI void
pipe_acquire_reader (struct pipe *pipe)
{
  mutex_lock (&pipe->lock);
  if (pipe->readers++ == 0)
    _pipe_first_reader (pipe);
}

/* Lock PIPE and increment its writers count.  */
PIPE_EI void
pipe_acquire_writer (struct pipe *pipe)
{
  mutex_lock (&pipe->lock);
  if (pipe->writers++ == 0)
    _pipe_first_writer (pipe);
}

/* Decrement PIPE's (which should be locked) reader count and unlock it.  If
   there are no more refs to PIPE, it will be destroyed.  */
PIPE_EI void
pipe_release_reader (struct pipe *pipe)
{
  if (--pipe->readers == 0)
    _pipe_no_readers (pipe);
  else
    mutex_unlock (&pipe->lock);
}

/* Decrement PIPE's (which should be locked) writer count and unlock it.  If
   there are no more refs to PIPE, it will be destroyed.  */
PIPE_EI void
pipe_release_writer (struct pipe *pipe)
{
  if (--pipe->writers == 0)
    _pipe_no_writers (pipe);
  else
    mutex_unlock (&pipe->lock);
}

/* Increment PIPE's reader count.  PIPE should be unlocked.  */
PIPE_EI void
pipe_add_reader (struct pipe *pipe)
{
  pipe_acquire_reader (pipe);
  mutex_unlock (&pipe->lock);
}

/* Increment PIPE's writer count.  PIPE should be unlocked.  */
PIPE_EI void
pipe_add_writer (struct pipe *pipe)
{
  pipe_acquire_writer (pipe);
  mutex_unlock (&pipe->lock);
}

/* Decrement PIPE's (which should be unlocked) reader count and unlock it.  If
   there are no more refs to PIPE, it will be destroyed.  */
PIPE_EI void
pipe_remove_reader (struct pipe *pipe)
{
  mutex_lock (&pipe->lock);
  pipe_release_reader (pipe);
}

/* Decrement PIPE's (which should be unlocked) writer count and unlock it.  If
   there are no more refs to PIPE, it will be destroyed.  */
PIPE_EI void
pipe_remove_writer (struct pipe *pipe)
{
  mutex_lock (&pipe->lock);
  pipe_release_writer (pipe);
}

/* Empty out PIPE of any data.  PIPE should be locked.  */
PIPE_EI void
pipe_drain (struct pipe *pipe)
{
  pq_drain (pipe->queue);
}

#endif /* Use extern inlines.  */

/* 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.  If non-NULL, SOURCE is recorded as the source
   of the data, to be provided to any readers of it; if no reader ever reads
   it, it's deallocated by calling pipe_dealloc_addr.  */
error_t pipe_send (struct pipe *pipe, int noblock, 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);

/* Writes up to LEN bytes of DATA, to PIPE, which should be locked, and
   returns the amount written in AMOUNT.  If an error is returned, nothing is
   done.  If non-NULL, SOURCE is recorded as the source of the data, to be
   provided to any readers of it; if no reader ever reads it, it's
   deallocated by calling pipe_dealloc_addr.  */
#define pipe_write(pipe, noblock, source, data, data_len, amount) \
  pipe_send (pipe, noblock, source, data, data_len, 0, 0, 0, 0, amount)

/* 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
   corresponding source provided by the sender 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);


/* 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
   corresponding source provided by the sender 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).  */
#define pipe_read(pipe, noblock, source, data, data_len, amount) \
  pipe_recv (pipe, noblock, 0, source, data, data_len, amount, 0,0,0,0)

/* Hold this lock before attempting to lock multiple pipes. */
extern struct mutex pipe_multiple_lock;

/* Return when either RPIPE is available for reading (if SELECT_READ is set
   in *SELECT_TYPE), or WPIPE is available for writing (if select_write is
   set in *SELECT_TYPE).  *SELECT_TYPE is modified to reflect which (or both)
   is now available.  DATA_ONLY should be true if only data packets should be
   waited for on RPIPE.  Neither RPIPE or WPIPE should be locked when calling
   this function (unlike most pipe functions).  */
error_t pipe_pair_select (struct pipe *rpipe, struct pipe *wpipe,
			  int *select_type, int data_only);

/* ---------------------------------------------------------------- */
/* User-provided functions.  */

/* This routine may be provided by the user, in which case, it should be a
   function taking a non-NULL source address and deallocating it.  It
   defaults to calling ports_port_deref.  */
void pipe_dealloc_addr (void *addr);

#endif /* __PIPE_H__ */