blob: dafc512f118a7f10217f052979992a3de09bbe8c [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 Sams2382aba2011-09-13 15:41:01 -070037 void setTimoutCallback(void (*)(void *), void *, uint64_t timeout);
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Jason Sams1dda6752011-01-18 18:12:26 -080039 void printDebugData() const;
40
Jason Sams326e0dd2009-05-22 14:03:28 -070041 LocklessCommandFifo();
42 ~LocklessCommandFifo();
43
Jason Sams326e0dd2009-05-22 14:03:28 -070044protected:
45 uint8_t * volatile mPut;
46 uint8_t * volatile mGet;
47 uint8_t * mBuffer;
48 uint8_t * mEnd;
49 uint8_t mSize;
Jason Sams8c0ee652009-08-25 14:49:07 -070050 bool mInShutdown;
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -070051 bool mInitialized;
Jason Sams326e0dd2009-05-22 14:03:28 -070052
Jason Sams732f1c02009-06-18 16:58:42 -070053 Signal mSignalToWorker;
54 Signal mSignalToControl;
55
Jason Sams326e0dd2009-05-22 14:03:28 -070056public:
57 void * reserve(uint32_t bytes);
58 void commit(uint32_t command, uint32_t bytes);
59 void commitSync(uint32_t command, uint32_t bytes);
60
61 void flush();
Jason Samse0aab4a2011-08-12 15:05:15 -070062 bool wait(uint64_t timeout = 0);
Jason Sams12b14ae2010-03-18 11:39:44 -070063
Jason Samse0aab4a2011-08-12 15:05:15 -070064 const void * get(uint32_t *command, uint32_t *bytesData, uint64_t timeout = 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070065 void next();
66
67 void makeSpace(uint32_t bytes);
Jason Sams8c46b102010-08-18 12:38:03 -070068 bool makeSpaceNonBlocking(uint32_t bytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070069
70 bool isEmpty() const;
71 uint32_t getFreeSpace() const;
72
Jason Sams326e0dd2009-05-22 14:03:28 -070073private:
74 void dumpState(const char *) const;
Jason Sams2382aba2011-09-13 15:41:01 -070075
76 void (*mTimeoutCallback)(void *);
77 void * mTimeoutCallbackData;
78 uint64_t mTimeoutWait;
Jason Sams326e0dd2009-05-22 14:03:28 -070079};
80
81
82}
Jason Sams12b14ae2010-03-18 11:39:44 -070083}
Jason Sams326e0dd2009-05-22 14:03:28 -070084#endif