blob: bdda3f549b50c8a3368c6a2a3c63f1c4a2def45e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/**
2 * Copyright (C) ARM Limited 2010-2014. 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, LONG_MAX, 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
23class Fifo {
24public:
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
36private:
37 int mSingleBufferSize, mWrite, mRead, mReadCommit, mRaggedEnd, mWrapThreshold;
38 sem_t mWaitForSpaceSem;
39 sem_t* mReaderSem;
40 char* mBuffer;
41 bool mEnd;
42
43 // Intentionally unimplemented
44 Fifo(const Fifo &);
45 Fifo &operator=(const Fifo &);
46};
47
48#endif //__FIFO_H__