blob: b8ceeed3ecda5d3c19092b12cbadc562e00c069f [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RS_LOCKLESS_FIFO_H
18#define ANDROID_RS_LOCKLESS_FIFO_H
19
20
Jason Sams1aa5a4e2009-06-22 17:15:15 -070021#include "rsUtils.h"
Jason Sams12b14ae2010-03-18 11:39:44 -070022#include "rsSignal.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070023
24namespace android {
Jason Sams12b14ae2010-03-18 11:39:44 -070025namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070026
27
28// A simple FIFO to be used as a producer / consumer between two
29// threads. One is writer and one is reader. The common cases
Jason Sams8c0ee652009-08-25 14:49:07 -070030// will not require locking. It is not threadsafe for multiple
Jason Sams326e0dd2009-05-22 14:03:28 -070031// readers or writers by design.
32
Jason Sams8c0ee652009-08-25 14:49:07 -070033class LocklessCommandFifo
Jason Sams326e0dd2009-05-22 14:03:28 -070034{
35public:
36 bool init(uint32_t size);
Jason Sams8c0ee652009-08-25 14:49:07 -070037 void shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -070038
39 LocklessCommandFifo();
40 ~LocklessCommandFifo();
41
Jason Sams326e0dd2009-05-22 14:03:28 -070042protected:
43 uint8_t * volatile mPut;
44 uint8_t * volatile mGet;
45 uint8_t * mBuffer;
46 uint8_t * mEnd;
47 uint8_t mSize;
Jason Sams8c0ee652009-08-25 14:49:07 -070048 bool mInShutdown;
Jason Sams326e0dd2009-05-22 14:03:28 -070049
Jason Sams732f1c02009-06-18 16:58:42 -070050 Signal mSignalToWorker;
51 Signal mSignalToControl;
52
Jason Sams326e0dd2009-05-22 14:03:28 -070053public:
54 void * reserve(uint32_t bytes);
55 void commit(uint32_t command, uint32_t bytes);
56 void commitSync(uint32_t command, uint32_t bytes);
57
58 void flush();
Jason Sams12b14ae2010-03-18 11:39:44 -070059 void wait();
60
Jason Sams326e0dd2009-05-22 14:03:28 -070061 const void * get(uint32_t *command, uint32_t *bytesData);
62 void next();
63
64 void makeSpace(uint32_t bytes);
Jason Sams8c46b102010-08-18 12:38:03 -070065 bool makeSpaceNonBlocking(uint32_t bytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070066
67 bool isEmpty() const;
68 uint32_t getFreeSpace() const;
69
70
71private:
72 void dumpState(const char *) const;
73};
74
75
76}
Jason Sams12b14ae2010-03-18 11:39:44 -070077}
Jason Sams326e0dd2009-05-22 14:03:28 -070078#endif