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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
|
/*
* Copyright (c) 1997-1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit 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 GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Pipes. A pipe is two stream-like objects connected together via a
* couple of shared buffers. The implementation is truely UNIDIRECTIONAL!
* If you try to use them bidirectionally, it will almost certainly
* deadlock. If you need something so fancy as true bidirectional pipes,
* I suggest using socketpair() in the C/Posix library instead (although
* that requires linking with the network stack).
*/
#include <oskit/com/pipe.h>
#include <oskit/com/stream.h>
#include <oskit/io/asyncio.h>
#include <oskit/com/services.h>
#include <oskit/com/lock_mgr.h>
#include <oskit/com/lock.h>
#include <oskit/com/condvar.h>
#include <oskit/com/listener_mgr.h>
#include <oskit/queue.h>
#include <oskit/error.h>
#include <malloc.h>
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
/*
* A pipe has an input buffer and a connection to the other side of
* the pipe. A write to a pipe writes to the input buffer on the other
* side, and a read comes from the local side. In either case, there
* must be resource available or the caller will sleep until there is
* sufficient resource.
*
* Pipe semantics dictate that the pipe stays valid even after one
* side has closed so that the other side can read the remaining
* buffered data. Both sides of the pipe will be deallocated only
* when both counts go to zero.
*
* Note that the lock/condvar pair is *shared* between both sides of
* the pipe.
*/
struct pipe {
oskit_pipe_t pipei; /* Pipe COM interface */
oskit_asyncio_t pipea; /* Asyncio COM interface */
oskit_u32_t count; /* Reference count */
struct pipe *sister; /* Other side of the pipe */
oskit_u32_t flags; /* Flags */
queue_head_t pipeq; /* Queue of data buffers */
int dataready; /* Amount of data ready */
oskit_lock_t *lock; /* Thread lock (shared) */
oskit_condvar_t *condvar; /* Thread condvar */
struct listener_mgr *readers; /* listeners for asyncio */
struct listener_mgr *writers; /* listeners for asyncio */
};
/*
* Pipe buffers are queues of mbuf like thingies, each with an associated
* buffer, offset and size.
*/
struct pbuf {
queue_chain_t chain; /* Queueing element */
int size; /* Original size for dealloc */
int count; /* Amount of data ready */
char *bufp; /* Current pointer into data */
char data[0]; /* Buffer space for data */
};
/*
* The maximum size that a pipe buffer can grow to.
*/
/*#define MAXPIPEBUF (1024 * 64) */
#define MAXPIPEBUF 512
/*
* Flags.
*/
#define PIPE_CLOSED 0x01
#define PIPE_SLEEPREAD 0x02
#define PIPE_SLEEPWRITE 0x04
#define PIPE_WIDOWED 0x08
#define PIPE_READSEL 0x10
#define PIPE_WRITESEL 0x20
/*
* Thread safe locking. Not sure it makes any sense to use pipes in
* single threaded application, but whatever ...
*/
#define PLOCK(pipe) if (pipe->lock) oskit_lock_lock(pipe->lock)
#define PUNLOCK(pipe) if (pipe->lock) oskit_lock_unlock(pipe->lock)
#define PWAIT(pipe) \
if (pipe->condvar) oskit_condvar_wait(pipe->condvar, pipe->lock)
#define PSIGNAL(pipe) \
if (pipe->condvar) oskit_condvar_signal(pipe->condvar)
/*
* Stream operators
*/
static oskit_error_t
pipe_query(oskit_pipe_t *f, const oskit_iid_t *iid, void **out_ihandle)
{
struct pipe *pipe = (struct pipe *) f;
assert(pipe->count);
if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_stream_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_pipe_iid, sizeof(*iid)) == 0) {
*out_ihandle = pipe;
++pipe->count;
return 0;
}
if (memcmp(iid, &oskit_asyncio_iid, sizeof(*iid)) == 0) {
*out_ihandle = &pipe->pipea;
++pipe->count;
return 0;
}
*out_ihandle = NULL;
return OSKIT_E_NOINTERFACE;
}
static OSKIT_COMDECL_U
pipe_addref(oskit_pipe_t *f)
{
struct pipe *pipe = (struct pipe *) f;
assert(pipe->count);
return ++pipe->count;
}
static void
pipe_free(struct pipe *pipe)
{
struct pbuf *pbuf;
/*
* Free all the buffers and then free the pipe object.
*/
while (!queue_empty(&pipe->pipeq)) {
queue_remove_first(&pipe->pipeq, pbuf, struct pbuf *, chain);
sfree(pbuf, sizeof(*pbuf) + pbuf->size);
}
if (pipe->lock)
oskit_lock_release(pipe->lock);
if (pipe->condvar)
oskit_condvar_release(pipe->condvar);
oskit_destroy_listener_mgr(pipe->readers);
oskit_destroy_listener_mgr(pipe->writers);
free(pipe);
}
static oskit_u32_t
pipe_release(oskit_pipe_t *f)
{
struct pipe *pipe = (struct pipe *) f;
struct pipe *psis = pipe->sister;
int newcount;
PLOCK(pipe);
assert(pipe->count);
if ((newcount = --pipe->count) == 0) {
/*
* Both sides of the pipe must be closed before it
* can be deallocated.
*/
if (psis->flags & PIPE_CLOSED) {
pipe_free(pipe->sister);
pipe_free(pipe);
return 0;
}
pipe->flags |= PIPE_CLOSED;
psis->flags |= PIPE_WIDOWED;
if (psis->flags & (PIPE_SLEEPREAD|PIPE_SLEEPWRITE))
PSIGNAL(psis);
}
PUNLOCK(pipe);
return newcount;
}
/*** Operations inherited from oskit_stream interface ***/
static OSKIT_COMDECL
pipe_read(oskit_pipe_t *f, void *buf, oskit_u32_t len,
oskit_u32_t *out_actual)
{
struct pipe *pipe = (struct pipe *) f;
struct pipe *psis = pipe->sister;
char *bp = buf;
struct pbuf *pbuf;
int count;
PLOCK(pipe);
/*
* Look for read on closed pipe.
*/
if (pipe->flags & PIPE_CLOSED) {
PUNLOCK(pipe);
return OSKIT_EBADF;
}
*out_actual = 0;
/*
* If no data available, sleep until the sister puts some data in.
* Look for EOF condition; Nothing to read and pipe is widowed.
*/
while (!pipe->dataready) {
if (pipe->flags & PIPE_WIDOWED) {
PUNLOCK(pipe);
return 0;
}
pipe->flags |= PIPE_SLEEPREAD;
PWAIT(pipe);
pipe->flags &= ~PIPE_SLEEPREAD;
}
/*
* Iterate through the buffers, copying out as much data as
* possible, up to the maximum specified by the caller.
*/
while (!queue_empty(&pipe->pipeq) && len > 0) {
pbuf = (struct pbuf *) queue_first(&pipe->pipeq);
/*
* For a buffer, copyout only as much data as can fit.
*/
count = (pbuf->count > len) ? len : pbuf->count;
memcpy(bp, pbuf->bufp, count);
pbuf->bufp += count;
pbuf->count -= count;
*out_actual += count;
len -= count;
bp += count;
/*
* If the buffer is empty, dealloc.
*/
if (pbuf->count == 0) {
queue_remove(&pipe->pipeq, pbuf, struct pbuf *, chain);
sfree(pbuf, sizeof(*pbuf) + pbuf->size);
}
pipe->dataready -= count;
/*
* Look for a select sleeper on the write side.
*/
if (psis->flags & PIPE_WRITESEL) {
PUNLOCK(pipe);
oskit_listener_mgr_notify(psis->writers);
PLOCK(pipe);
}
/*
* If the sister was waiting to put more data in, then
* wake it up.
*/
if (psis->flags & PIPE_SLEEPWRITE) {
psis->flags &= ~PIPE_SLEEPWRITE;
PSIGNAL(psis);
}
}
PUNLOCK(pipe);
return 0;
}
static OSKIT_COMDECL
pipe_write(oskit_pipe_t *f, const void *buf,
oskit_u32_t len, oskit_u32_t *out_actual)
{
struct pipe *pipe = (struct pipe *) f;
struct pipe *psis = pipe->sister;
char *bp = (char *) buf;
struct pbuf *pbuf;
int count;
PLOCK(pipe);
/*
* Look for write on closed pipe.
*/
if (pipe->flags & PIPE_CLOSED) {
PUNLOCK(pipe);
return OSKIT_EBADF;
}
/*
* Look for a broken pipe.
*/
if (pipe->flags & PIPE_WIDOWED) {
PUNLOCK(pipe);
return OSKIT_EPIPE;
}
*out_actual = 0;
/*
* Loop, stuffing stuff into the other side until we run out of
* stuff, or reach the maximum allowed. If the other side fills,
* up, must sleep until it wakes this side up again. Each time
* data is stuffed into the other side, be sure to wake it up.
*/
while (len > 0) {
/*
* Is there room for the data? If not, must wait.
*/
while (psis->dataready >= MAXPIPEBUF) {
pipe->flags |= PIPE_SLEEPWRITE;
PWAIT(pipe);
pipe->flags &= ~PIPE_SLEEPWRITE;
}
if (MAXPIPEBUF - psis->dataready > len)
count = len;
else
count = MAXPIPEBUF - psis->dataready;
if ((pbuf = smalloc(sizeof(*pbuf) + count)) == 0)
panic("pipe_write: Out of memory");
memcpy(pbuf->data, bp, count);
pbuf->bufp = pbuf->data;
pbuf->count = count;
pbuf->size = count;
*out_actual += count;
len -= count;
bp += count;
queue_enter(&psis->pipeq, pbuf, struct pbuf *, chain);
psis->dataready += count;
/*
* Look for a select sleeper on the read side.
*/
if (psis->flags & PIPE_READSEL) {
PUNLOCK(pipe);
oskit_listener_mgr_notify(psis->readers);
PLOCK(pipe);
}
/*
* Wakeup the sister side if it was waiting for data.
*/
if (psis->flags & PIPE_SLEEPREAD) {
psis->flags &= ~PIPE_SLEEPREAD;
PSIGNAL(psis);
}
}
PUNLOCK(pipe);
return 0;
}
static OSKIT_COMDECL
pipe_seek(oskit_pipe_t *f, oskit_s64_t offset,
oskit_seek_t whence, oskit_u64_t *out_newpos)
{
return OSKIT_ESPIPE;
}
static OSKIT_COMDECL
pipe_setsize(oskit_pipe_t *f, oskit_u64_t new_size)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_copy_to(oskit_pipe_t *f, oskit_stream_t *dst, oskit_u64_t size,
oskit_u64_t *out_read, oskit_u64_t *out_written)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_commit(oskit_pipe_t *f, oskit_u32_t commit_flags)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_revert(oskit_pipe_t *f)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_lockregion(oskit_pipe_t *f, oskit_u64_t offset,
oskit_u64_t size, oskit_u32_t lock_type)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_unlockregion(oskit_pipe_t *f, oskit_u64_t offset,
oskit_u64_t size, oskit_u32_t lock_type)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_stat(oskit_pipe_t *f, oskit_stream_stat_t *out_stat,
oskit_u32_t stat_flags)
{
return OSKIT_ENOTSUP;
}
static OSKIT_COMDECL
pipe_clone(oskit_pipe_t *f, oskit_pipe_t **out_stream)
{
return OSKIT_ENOTSUP;
}
static struct oskit_pipe_ops pipe_ops = {
pipe_query,
pipe_addref,
pipe_release,
pipe_read,
pipe_write,
pipe_seek,
pipe_setsize,
pipe_copy_to,
pipe_commit,
pipe_revert,
pipe_lockregion,
pipe_unlockregion,
pipe_stat,
pipe_clone,
};
/*
**********************************************************************
* Async IO interface,
*/
static OSKIT_COMDECL
pipe_asyncio_query(oskit_asyncio_t *f,
const struct oskit_guid *iid, void **out_ihandle)
{
struct pipe *pipe = (struct pipe *) (f-1);
return pipe_query(&pipe->pipei, iid, out_ihandle);
}
static OSKIT_COMDECL_U
pipe_asyncio_addref(oskit_asyncio_t *f)
{
struct pipe *pipe = (struct pipe *) (f-1);
return pipe_addref(&pipe->pipei);
}
static OSKIT_COMDECL_U
pipe_asyncio_release(oskit_asyncio_t *f)
{
struct pipe *pipe = (struct pipe *) (f-1);
return pipe_release(&pipe->pipei);
}
/*
* return a mask with all conditions that currently apply to that socket
* must be called with splnet()!
*/
static oskit_u32_t
get_pipe_conditions(struct pipe *pipe)
{
struct pipe *psis = pipe->sister;
oskit_u32_t res = 0;
if (pipe->dataready)
res |= OSKIT_ASYNCIO_READABLE;
if (pipe->flags & (PIPE_CLOSED|PIPE_WIDOWED))
res |= OSKIT_ASYNCIO_EXCEPTION;
if (psis->dataready < MAXPIPEBUF)
res |= OSKIT_ASYNCIO_WRITABLE;
return res;
}
/*
* Poll for currently pending asynchronous I/O conditions.
* If successful, returns a mask of the OSKIT_ASYNC_IO_* flags above,
* indicating which conditions are currently present.
*/
static OSKIT_COMDECL
pipe_asyncio_poll(oskit_asyncio_t *f)
{
struct pipe *pipe = (struct pipe *) (f-1);
oskit_u32_t res = 0;
PLOCK(pipe);
res = get_pipe_conditions(pipe);
PUNLOCK(pipe);
return res;
}
/*
* Add a callback object (a "listener" for async I/O events).
* When an event of interest occurs on this I/O object
* (i.e., when one of the three I/O conditions becomes true),
* all registered listeners will be called.
* Also, if successful, this method returns a mask
* describing which of the OSKIT_ASYNC_IO_* conditions are already true,
* which the caller must check in order to avoid missing events
* that occur just before the listener is registered.
*/
static OSKIT_COMDECL
pipe_asyncio_add_listener(oskit_asyncio_t *f,
struct oskit_listener *l, oskit_s32_t mask)
{
struct pipe *pipe = (struct pipe *) (f-1);
oskit_s32_t cond;
PLOCK(pipe);
cond = get_pipe_conditions(pipe);
/* for read and exceptional conditions */
if (mask & (OSKIT_ASYNCIO_READABLE|OSKIT_ASYNCIO_EXCEPTION)) {
oskit_listener_mgr_add(pipe->readers, l);
pipe->flags |= PIPE_READSEL;
}
/* for write */
if (mask & OSKIT_ASYNCIO_WRITABLE) {
oskit_listener_mgr_add(pipe->writers, l);
pipe->flags |= PIPE_WRITESEL;
}
PUNLOCK(pipe);
return cond;
}
/*
* Remove a previously registered listener callback object.
* Returns an error if the specified callback has not been registered.
*/
static OSKIT_COMDECL
pipe_asyncio_remove_listener(oskit_asyncio_t *f, struct oskit_listener *l0)
{
struct pipe *pipe = (struct pipe *) (f-1);
oskit_error_t rc1, rc2;
PLOCK(pipe);
/*
* we don't know where was added - if at all - so let's check
* both lists
*
* turn off notifications if no listeners left
*/
rc1 = oskit_listener_mgr_remove(pipe->readers, l0);
if (oskit_listener_mgr_count(pipe->readers) == 0) {
pipe->flags &= ~PIPE_READSEL;
}
rc2 = oskit_listener_mgr_remove(pipe->writers, l0);
if (oskit_listener_mgr_count(pipe->writers) == 0) {
pipe->flags &= ~PIPE_WRITESEL;
}
PUNLOCK(pipe);
/* flag error if both removes failed */
return (rc1 && rc2) ? OSKIT_E_INVALIDARG : 0; /* is that right ? */
}
/*
* return the number of bytes that can be read, basically ioctl(FIONREAD)
*/
static OSKIT_COMDECL
pipe_asyncio_readable(oskit_asyncio_t *f)
{
struct pipe *pipe = (struct pipe *) (f-1);
oskit_u32_t count;
PLOCK(pipe);
count = pipe->dataready;
PUNLOCK(pipe);
return count;
}
static struct oskit_asyncio_ops pipe_asyncioops =
{
pipe_asyncio_query,
pipe_asyncio_addref,
pipe_asyncio_release,
pipe_asyncio_poll,
pipe_asyncio_add_listener,
pipe_asyncio_remove_listener,
pipe_asyncio_readable
};
/*
* Sole user entry point for this module. Create an `oskit_pipe' object.
*/
OSKIT_COMDECL
oskit_create_pipe(oskit_pipe_t **out_pipe0, oskit_pipe_t **out_pipe1)
{
struct pipe *pipe0, *pipe1;
oskit_lock_mgr_t *lock_mgr;
oskit_lock_t *lock;
oskit_condvar_t *condvar;
if ((pipe0 = malloc(sizeof(struct pipe))) == 0)
return OSKIT_ENOMEM;
if ((pipe1 = malloc(sizeof(struct pipe))) == 0) {
free(pipe0);
return OSKIT_ENOMEM;
}
memset(pipe0, 0, sizeof(*pipe0));
memset(pipe1, 0, sizeof(*pipe1));
pipe0->pipei.ops = &pipe_ops;
pipe1->pipei.ops = &pipe_ops;
pipe0->pipea.ops = &pipe_asyncioops;
pipe1->pipea.ops = &pipe_asyncioops;
pipe0->count = 1;
pipe1->count = 1;
pipe0->sister = pipe1;
pipe1->sister = pipe0;
queue_init(&pipe0->pipeq);
queue_init(&pipe1->pipeq);
pipe0->readers = oskit_create_listener_mgr((oskit_iunknown_t *)
&pipe0->pipea);
pipe0->writers = oskit_create_listener_mgr((oskit_iunknown_t *)
&pipe0->pipea);
pipe1->readers = oskit_create_listener_mgr((oskit_iunknown_t *)
&pipe1->pipea);
pipe1->writers = oskit_create_listener_mgr((oskit_iunknown_t *)
&pipe1->pipea);
/*
* See if thread-safe locks are required. Note that I don't think it
* makes any sense to use pipes in a single threaded kernel, but
* at least the program will link and run. It will probably just
* deadlock though.
*/
if (oskit_lookup_first(&oskit_lock_mgr_iid, (void *) &lock_mgr))
panic("oskit_create_pipe: oskit_lookup_first");
if (lock_mgr) {
if (oskit_lock_mgr_allocate_lock(lock_mgr, &lock))
panic("oskit_create_pipe: lock_mgr_allocate_lock");
oskit_lock_addref(lock);
pipe0->lock = lock;
pipe1->lock = lock;
if (oskit_lock_mgr_allocate_condvar(lock_mgr, &condvar))
panic("oskit_create_pipe: lock_mgr_allocate_condvar");
pipe0->condvar = condvar;
if (oskit_lock_mgr_allocate_condvar(lock_mgr, &condvar))
panic("oskit_create_pipe: lock_mgr_allocate_condvar");
pipe1->condvar = condvar;
}
*out_pipe0 = &pipe0->pipei;
*out_pipe1 = &pipe1->pipei;
return 0;
}
|