blob: eabdc3e97f05463c83e06ce37312f2dd80a2c106 [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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033class LocklessCommandFifo {
Jason Sams326e0dd2009-05-22 14:03:28 -070034public:
35 bool init(uint32_t size);
Jason Sams8c0ee652009-08-25 14:49:07 -070036 void shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -070037
Jason Sams1dda6752011-01-18 18:12:26 -080038 void printDebugData() const;
39
Jason Sams326e0dd2009-05-22 14:03:28 -070040 LocklessCommandFifo();
41 ~LocklessCommandFifo();
42
Jason Sams326e0dd2009-05-22 14:03:28 -070043protected:
44 uint8_t * volatile mPut;
45 uint8_t * volatile mGet;
46 uint8_t * mBuffer;
47 uint8_t * mEnd;
48 uint8_t mSize;
Jason Sams8c0ee652009-08-25 14:49:07 -070049 bool mInShutdown;
Jason Sams326e0dd2009-05-22 14:03:28 -070050
Jason Sams732f1c02009-06-18 16:58:42 -070051 Signal mSignalToWorker;
52 Signal mSignalToControl;
53
Jason Sams326e0dd2009-05-22 14:03:28 -070054public:
55 void * reserve(uint32_t bytes);
56 void commit(uint32_t command, uint32_t bytes);
57 void commitSync(uint32_t command, uint32_t bytes);
58
59 void flush();
Jason Sams12b14ae2010-03-18 11:39:44 -070060 void wait();
61
Jason Sams326e0dd2009-05-22 14:03:28 -070062 const void * get(uint32_t *command, uint32_t *bytesData);
63 void next();
64
65 void makeSpace(uint32_t bytes);
Jason Sams8c46b102010-08-18 12:38:03 -070066 bool makeSpaceNonBlocking(uint32_t bytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070067
68 bool isEmpty() const;
69 uint32_t getFreeSpace() const;
70
Jason Sams326e0dd2009-05-22 14:03:28 -070071private:
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