• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
21 #include "rsUtils.h"
22 #include "rsSignal.h"
23 
24 namespace android {
25 namespace renderscript {
26 
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
30 // will not require locking.  It is not threadsafe for multiple
31 // readers or writers by design.
32 
33 class LocklessCommandFifo {
34 public:
35     bool init(uint32_t size);
36     void shutdown();
37     void setTimoutCallback(void (*)(void *), void *, uint64_t timeout);
38 
39     void printDebugData() const;
40 
41     LocklessCommandFifo();
42     ~LocklessCommandFifo();
43 
44 protected:
45     uint8_t * volatile mPut;
46     uint8_t * volatile mGet;
47     uint8_t * mBuffer;
48     uint8_t * mEnd;
49     uint8_t mSize;
50     bool mInShutdown;
51     bool mInitialized;
52 
53     Signal mSignalToWorker;
54     Signal mSignalToControl;
55 
56 public:
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();
62     bool wait(uint64_t timeout = 0);
63 
64     const void * get(uint32_t *command, uint32_t *bytesData, uint64_t timeout = 0);
65     void next();
66 
67     void makeSpace(uint32_t bytes);
68     bool makeSpaceNonBlocking(uint32_t bytes);
69 
70     bool isEmpty() const;
71     uint32_t getFreeSpace() const;
72 
73 private:
74     void dumpState(const char *) const;
75 
76     void (*mTimeoutCallback)(void *);
77     void * mTimeoutCallbackData;
78     uint64_t mTimeoutWait;
79 };
80 
81 
82 }
83 }
84 #endif
85