[4] | 1 | /*************************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Module: sageBuf.h |
---|
| 5 | * Author : Byungil Jeong |
---|
| 6 | * |
---|
| 7 | * Description: SAGE buffer classes |
---|
| 8 | * |
---|
| 9 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 10 | * University of Illinois at Chicago |
---|
| 11 | * |
---|
| 12 | * All rights reserved. |
---|
| 13 | * |
---|
| 14 | * Redistribution and use in source and binary forms, with or without |
---|
| 15 | * modification, are permitted provided that the following conditions are met: |
---|
| 16 | * |
---|
| 17 | * * Redistributions of source code must retain the above copyright |
---|
| 18 | * notice, this list of conditions and the following disclaimer. |
---|
| 19 | * * Redistributions in binary form must reproduce the above |
---|
| 20 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 21 | * in the documentation and/or other materials provided with the distribution. |
---|
| 22 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 23 | * the names of its contributors may be used to endorse or promote |
---|
| 24 | * products derived from this software without specific prior written permission. |
---|
| 25 | * |
---|
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 29 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 37 | * |
---|
| 38 | * Direct questions, comments etc about SAGE to sage_users@listserv.uic.edu or |
---|
| 39 | * http://www.evl.uic.edu/cavern/forum/ |
---|
| 40 | * |
---|
| 41 | *****************************************************************************************/ |
---|
| 42 | |
---|
| 43 | #ifndef _SAGE_BUF_H |
---|
| 44 | #define _SAGE_BUF_H |
---|
| 45 | |
---|
| 46 | #include "sageBase.h" |
---|
| 47 | |
---|
| 48 | #define MAX_READER_NUM 100 |
---|
| 49 | |
---|
| 50 | typedef char* sageBufEntry; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * the abstract class of sage buffers |
---|
| 54 | */ |
---|
| 55 | class sageBuf { |
---|
| 56 | protected: |
---|
| 57 | int readIdx; |
---|
| 58 | int writeIdx; |
---|
| 59 | int entryNum; |
---|
| 60 | int bufLen; |
---|
| 61 | int entrySize; |
---|
| 62 | sageBufEntry *entries; |
---|
| 63 | |
---|
| 64 | pthread_mutex_t bufLock; |
---|
| 65 | pthread_cond_t notEmpty; |
---|
| 66 | bool full, empty; |
---|
| 67 | |
---|
| 68 | void initArray(int size); |
---|
| 69 | |
---|
| 70 | public: |
---|
| 71 | sageBuf(); |
---|
| 72 | inline bool isEmpty() { return empty; } |
---|
| 73 | inline bool isFull() { return full; } |
---|
| 74 | inline int size() { return bufLen; } |
---|
| 75 | inline void setEntryNum(int num) { entryNum = num; } |
---|
| 76 | inline int getEntryNum() { return entryNum; } |
---|
| 77 | inline int getReadIdx() { return readIdx; } |
---|
| 78 | inline int filled() { return (entryNum*100)/bufLen; } |
---|
| 79 | |
---|
| 80 | sageBufEntry operator[](int idx); |
---|
| 81 | |
---|
| 82 | void reset(); |
---|
| 83 | void resetIdx(); |
---|
| 84 | int getStatus() { return entryNum*100/bufLen; } |
---|
| 85 | virtual void setBlockingFlag(bool blk) {} |
---|
| 86 | virtual bool pushBack(sageBufEntry entry) = 0; |
---|
| 87 | virtual sageBufEntry front() = 0; |
---|
| 88 | virtual bool next() = 0; |
---|
| 89 | virtual sageBufEntry front(int id) { return NULL; } |
---|
| 90 | virtual sageBufEntry next(int id) { return NULL; } |
---|
| 91 | //virtual int import(sageBuf *buf) = 0; |
---|
| 92 | virtual void releaseLock() = 0; |
---|
| 93 | ~sageBuf(); |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * no concurrent read and write |
---|
| 98 | */ |
---|
| 99 | class sageSerialBuf : public sageBuf { |
---|
| 100 | public: |
---|
| 101 | sageSerialBuf(int len) { initArray(len); } |
---|
| 102 | bool pushBack(sageBufEntry entry); |
---|
| 103 | sageBufEntry front(); |
---|
| 104 | bool next(); |
---|
| 105 | //int import(sageBuf *buf); |
---|
| 106 | void releaseLock() {} |
---|
| 107 | }; |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * sageCircBufSingle |
---|
| 111 | * instantiated in sageBlockBuf |
---|
| 112 | */ |
---|
| 113 | class sageCircBufSingle : public sageBuf { |
---|
| 114 | public: |
---|
| 115 | int instID; |
---|
| 116 | |
---|
| 117 | protected: |
---|
| 118 | bool blocking; |
---|
| 119 | bool active; |
---|
| 120 | |
---|
| 121 | public: |
---|
| 122 | sageCircBufSingle(int len, bool blkRead) : blocking(blkRead), active(true), instID(-1) |
---|
| 123 | { initArray(len); } |
---|
| 124 | bool pushBack(sageBufEntry entry); |
---|
| 125 | sageBufEntry front(); |
---|
| 126 | bool next(); |
---|
| 127 | bool isActive() { return active; } |
---|
| 128 | |
---|
| 129 | void setBlockingFlag(bool blk) { blocking = blk; } |
---|
| 130 | void releaseLock(); |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | class sageCircBuf : public sageBuf { |
---|
| 134 | protected: |
---|
| 135 | pthread_cond_t notFull; |
---|
| 136 | |
---|
| 137 | public: |
---|
| 138 | sageCircBuf(int len); |
---|
| 139 | bool pushBack(sageBufEntry entry, bool blk); |
---|
| 140 | bool pushBack(sageBufEntry entry) { return pushBack(entry, false); } |
---|
| 141 | sageBufEntry front(bool blk); |
---|
| 142 | sageBufEntry front() { return front(false); } |
---|
| 143 | bool next(); |
---|
| 144 | void releaseLock(); |
---|
| 145 | }; |
---|
| 146 | |
---|
| 147 | class sageRAB : public sageBuf { |
---|
| 148 | protected: |
---|
| 149 | int *nextIdx; |
---|
| 150 | int head; |
---|
| 151 | int tail; |
---|
| 152 | int freeHead; |
---|
| 153 | int freeTail; |
---|
| 154 | |
---|
| 155 | public: |
---|
| 156 | sageRAB(int len); |
---|
| 157 | |
---|
| 158 | bool insert(sageBufEntry entry); |
---|
| 159 | bool remove(int idx, int prev); |
---|
| 160 | inline int start() { return head; } |
---|
| 161 | void next(int &idx); |
---|
| 162 | |
---|
| 163 | bool next() { return false; } |
---|
| 164 | sageBufEntry front() { return NULL; } |
---|
| 165 | bool pushBack(sageBufEntry entry) { return false; } |
---|
| 166 | void releaseLock() {} |
---|
| 167 | }; |
---|
| 168 | |
---|
| 169 | class readerInfo { |
---|
| 170 | public: |
---|
| 171 | int readIdx; |
---|
| 172 | bool empty; |
---|
| 173 | bool active; |
---|
| 174 | |
---|
| 175 | readerInfo() : readIdx(0), empty(true), active(false) {} |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * circular buffer with multiple readers |
---|
| 180 | */ |
---|
| 181 | class sageCircBufMulti : public sageBuf { |
---|
| 182 | private: |
---|
| 183 | sageBufEntry front() { return NULL; } |
---|
| 184 | bool next(); |
---|
| 185 | //int import(sageBuf *buf) { return 0; } |
---|
| 186 | int distToWriteIdx(int ridx); |
---|
| 187 | |
---|
| 188 | protected: |
---|
| 189 | bool blocking; |
---|
| 190 | int readerNum; |
---|
| 191 | int maxReaderID; |
---|
| 192 | |
---|
| 193 | readerInfo readers[MAX_READER_NUM]; |
---|
| 194 | |
---|
| 195 | public: |
---|
| 196 | sageCircBufMulti(int len, bool blkRead) : readerNum(0), blocking(blkRead), maxReaderID(-1) |
---|
| 197 | { initArray(len); } |
---|
| 198 | |
---|
| 199 | int addReader(int id); |
---|
| 200 | void removeReader(int id); |
---|
| 201 | inline bool isActive(int id) { return readers[id].active; } |
---|
| 202 | |
---|
| 203 | bool pushBack(sageBufEntry entry); |
---|
| 204 | sageBufEntry front(int id); |
---|
| 205 | sageBufEntry next(int id); |
---|
| 206 | |
---|
| 207 | void setBlockingFlag(bool blk) { blocking = blk; } |
---|
| 208 | void releaseLock() { blocking=false, empty=false, pthread_cond_broadcast(¬Empty); } |
---|
| 209 | }; |
---|
| 210 | |
---|
| 211 | #endif |
---|
| 212 | |
---|
| 213 | /* |
---|
| 214 | class sageBufMulti : public sageBuf { |
---|
| 215 | protected: |
---|
| 216 | int readerNum; |
---|
| 217 | readerInfo readers[MAX_READER_NUM]; |
---|
| 218 | |
---|
| 219 | public: |
---|
| 220 | sageBufMulti(int len) : readerNum(1) { initArray(len); } |
---|
| 221 | bool pushBack(sageBufEntry entry); |
---|
| 222 | sageBufEntry front(); |
---|
| 223 | bool next(); |
---|
| 224 | int import(sageBuf *buf); |
---|
| 225 | }; |
---|
| 226 | */ |
---|