blob: dafc512f118a7f10217f052979992a3de09bbe8c [file] [log] [blame]
Jason Samsd19f10d2009-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 Sams4b962e52009-06-22 17:15:15 -070021#include "rsUtils.h"
Jason Samsc1d726c2010-03-18 11:39:44 -070022#include "rsSignal.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070023
24namespace android {
Jason Samsc1d726c2010-03-18 11:39:44 -070025namespace renderscript {
Jason Samsd19f10d2009-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 Samsf5b45962009-08-25 14:49:07 -070030// will not require locking. It is not threadsafe for multiple
Jason Samsd19f10d2009-05-22 14:03:28 -070031// readers or writers by design.
32
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080033class LocklessCommandFifo {
Jason Samsd19f10d2009-05-22 14:03:28 -070034public:
35 bool init(uint32_t size);
Jason Samsf5b45962009-08-25 14:49:07 -070036 void shutdown();
Jason Sams5316b9e2011-09-13 15:41:01 -070037 void setTimoutCallback(void (*)(void *), void *, uint64_t timeout);
Jason Samsd19f10d2009-05-22 14:03:28 -070038
Jason Samsd1ac9812011-01-18 18:12:26 -080039 void printDebugData() const;
40
Jason Samsd19f10d2009-05-22 14:03:28 -070041 LocklessCommandFifo();
42 ~LocklessCommandFifo();
43
Jason Samsd19f10d2009-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 Samsf5b45962009-08-25 14:49:07 -070050 bool mInShutdown;
Alex Sakhartchouka8bb9212011-08-19 09:43:18 -070051 bool mInitialized;
Jason Samsd19f10d2009-05-22 14:03:28 -070052
Jason Sams5f7fc272009-06-18 16:58:42 -070053 Signal mSignalToWorker;
54 Signal mSignalToControl;
55
Jason Samsd19f10d2009-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 Samsbfc78912011-08-12 15:05:15 -070062 bool wait(uint64_t timeout = 0);
Jason Samsc1d726c2010-03-18 11:39:44 -070063
Jason Samsbfc78912011-08-12 15:05:15 -070064 const void * get(uint32_t *command, uint32_t *bytesData, uint64_t timeout = 0);
Jason Samsd19f10d2009-05-22 14:03:28 -070065 void next();
66
67 void makeSpace(uint32_t bytes);
Jason Samsd0cb1062010-08-18 12:38:03 -070068 bool makeSpaceNonBlocking(uint32_t bytes);
Jason Samsd19f10d2009-05-22 14:03:28 -070069
70 bool isEmpty() const;
71 uint32_t getFreeSpace() const;
72
Jason Samsd19f10d2009-05-22 14:03:28 -070073private:
74 void dumpState(const char *) const;
Jason Sams5316b9e2011-09-13 15:41:01 -070075
76 void (*mTimeoutCallback)(void *);
77 void * mTimeoutCallbackData;
78 uint64_t mTimeoutWait;
Jason Samsd19f10d2009-05-22 14:03:28 -070079};
80
81
82}
Jason Samsc1d726c2010-03-18 11:39:44 -070083}
Jason Samsd19f10d2009-05-22 14:03:28 -070084#endif