Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) ARM Limited 2010-2013. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #ifndef __FIFO_H__ |
| 10 | #define __FIFO_H__ |
| 11 | |
| 12 | #ifdef WIN32 |
| 13 | #include <windows.h> |
| 14 | #define sem_t HANDLE |
| 15 | #define sem_init(sem, pshared, value) ((*(sem) = CreateSemaphore(NULL, value, INFINITE, NULL)) == NULL) |
| 16 | #define sem_wait(sem) WaitForSingleObject(*(sem), INFINITE) |
| 17 | #define sem_post(sem) ReleaseSemaphore(*(sem), 1, NULL) |
| 18 | #define sem_destroy(sem) CloseHandle(*(sem)) |
| 19 | #else |
| 20 | #include <semaphore.h> |
| 21 | #endif |
| 22 | |
| 23 | class Fifo { |
| 24 | public: |
| 25 | Fifo(int singleBufferSize, int totalBufferSize, sem_t* readerSem); |
| 26 | ~Fifo(); |
| 27 | int numBytesFilled() const; |
| 28 | bool isEmpty() const; |
| 29 | bool isFull() const; |
| 30 | bool willFill(int additional) const; |
| 31 | char* start() const; |
| 32 | char* write(int length); |
| 33 | void release(); |
| 34 | char* read(int *const length); |
| 35 | |
| 36 | private: |
| 37 | int mSingleBufferSize, mWrite, mRead, mReadCommit, mRaggedEnd, mWrapThreshold; |
| 38 | sem_t mWaitForSpaceSem; |
| 39 | sem_t* mReaderSem; |
| 40 | char* mBuffer; |
| 41 | bool mEnd; |
| 42 | }; |
| 43 | |
| 44 | #endif //__FIFO_H__ |