[4] | 1 | /****************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 5 | * University of Illinois at Chicago |
---|
| 6 | * |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above |
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | * in the documentation and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | * the names of its contributors may be used to endorse or promote |
---|
| 19 | * products derived from this software without specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | * |
---|
| 33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 34 | * |
---|
| 35 | *****************************************************************************/ |
---|
| 36 | |
---|
| 37 | #ifndef BPIO_H |
---|
| 38 | #define BPIO_H 1 |
---|
| 39 | |
---|
| 40 | #define _FILE_OFFSET_BITS 64 |
---|
| 41 | |
---|
| 42 | #include <stdio.h> |
---|
| 43 | #include <unistd.h> |
---|
| 44 | #include <fcntl.h> |
---|
| 45 | #include <pthread.h> |
---|
| 46 | |
---|
| 47 | typedef struct bpbuf_s bpbuf_t; |
---|
| 48 | |
---|
| 49 | typedef struct bpio_s { |
---|
| 50 | int nfillers; /* readers read from disk -> bufs */ |
---|
| 51 | bpbuf_t *bpb; /* bpb[reader(0..nfillers-1)] */ |
---|
| 52 | int bufsize; /* matching that of bpb->bufsize */ |
---|
| 53 | int readsize; /* ditto */ |
---|
| 54 | int drain; /* next bpbuf_t to drain (dynamic, but owned by drain thread) */ |
---|
| 55 | int fwd; /* bpb->incrpos = fwd * nfillers * bufsize */ |
---|
| 56 | |
---|
| 57 | } bpio_t; |
---|
| 58 | |
---|
| 59 | /* Is someone waiting on our condition-variable? Why? */ |
---|
| 60 | enum business { NO, EMPTY, FULL, PAUSED }; |
---|
| 61 | |
---|
| 62 | struct bpbuf_s { |
---|
| 63 | /* Static stuff */ |
---|
| 64 | bpio_t *bpio; |
---|
| 65 | int nbufs; /* number of buffers per reader */ |
---|
| 66 | int bufsize; |
---|
| 67 | int readsize; |
---|
| 68 | unsigned char *bufspace; |
---|
| 69 | unsigned char **bufs; /* page-aligned buffers bufs[0..nbufs-1] */ |
---|
| 70 | int bpbno; /* our index in parent */ |
---|
| 71 | int fd; /* file descriptor */ |
---|
| 72 | |
---|
| 73 | volatile off_t *curpos; /* curpos[0..nbufs-1] -- filepos whence read */ |
---|
| 74 | volatile off_t wrappos; /* wrap back to poswrap after passing EOF */ |
---|
| 75 | volatile off_t eofpos; /* here's the nominal EOF point */ |
---|
| 76 | volatile off_t incrpos; /* increment from one read to the next */ |
---|
| 77 | volatile int doloop; /* automatically loop at EOF? */ |
---|
| 78 | |
---|
| 79 | /* Dynamic stuff, held under locks */ |
---|
| 80 | volatile int fillwaiting; /* is filler waiting? */ |
---|
| 81 | volatile int drainwaiting; /* is drainer waiting? */ |
---|
| 82 | volatile int filling; /* is filler thread supposed to be running? */ |
---|
| 83 | volatile int busy; /* is filler busy reading? (if "filling" was cleared, has it noticed?) */ |
---|
| 84 | |
---|
| 85 | volatile off_t filepos;/* file offset for next buffer to be filled*/ |
---|
| 86 | volatile int wp; /* next-buffer-to-be-drained (to display) index */ |
---|
| 87 | volatile int rp; /* next-buffer-to-be-filled (from file) index */ |
---|
| 88 | |
---|
| 89 | pthread_t bthread; |
---|
| 90 | pthread_mutex_t bmut; |
---|
| 91 | pthread_cond_t bfillwait; /* filler waiting (due to not-yet-setup or full queue)? */ |
---|
| 92 | pthread_cond_t bdrainwait; /* drainer wait (due to empty queue)? */ |
---|
| 93 | |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | extern void bpclose( bpio_t *bpio ); |
---|
| 97 | extern int bpopen( bpio_t *bpio, char *fname ); |
---|
| 98 | extern int bpbfull( bpbuf_t *bpb ); |
---|
| 99 | extern int bpbempty( bpbuf_t *bpb ); |
---|
| 100 | extern bpio_t *bpinit( bpio_t *bpio, int nfillers, int bufsize, int readsize, int nbufseach ); |
---|
| 101 | extern void bpseek( bpio_t *bpio, off_t pos ); |
---|
| 102 | extern unsigned char *bpcurbuf( bpio_t *bpio ); |
---|
| 103 | extern off_t bptell( bpio_t *bpio ); |
---|
| 104 | extern void bpstart( bpio_t *bpio, int wrap ); |
---|
| 105 | extern void bpstop( bpio_t *bpio ); |
---|
| 106 | extern void bpforward( bpio_t *bpio, int fwd ); |
---|
| 107 | extern void bprange( bpio_t *bpio, off_t from, off_t to ); |
---|
| 108 | |
---|
| 109 | extern void *bpfiller( void *vbpb ); |
---|
| 110 | extern int bpdrain( bpio_t *bpio, int (*sink)( unsigned char *buf, int nbytes, void *arg ), void *arg ); |
---|
| 111 | extern void bpsync( bpio_t *bpio ); |
---|
| 112 | |
---|
| 113 | #endif |
---|