blob: abeddf76a26c27754d2c21e3a23dfaf343374210 [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 Sams326e0dd2009-05-22 14:03:28 -070022
23namespace android {
24
25
26// A simple FIFO to be used as a producer / consumer between two
27// threads. One is writer and one is reader. The common cases
28// will not require locking. It is not threadsafe for multiple
29// readers or writers by design.
30
31class LocklessCommandFifo
32{
33public:
34 bool init(uint32_t size);
35
36 LocklessCommandFifo();
37 ~LocklessCommandFifo();
38
39
40protected:
Jason Sams732f1c02009-06-18 16:58:42 -070041 class Signal {
42 public:
43 Signal();
44 ~Signal();
45
46 bool init();
47
48 void set();
49 void wait();
50
51 protected:
52 bool mSet;
53 pthread_mutex_t mMutex;
54 pthread_cond_t mCondition;
55 };
56
Jason Sams326e0dd2009-05-22 14:03:28 -070057 uint8_t * volatile mPut;
58 uint8_t * volatile mGet;
59 uint8_t * mBuffer;
60 uint8_t * mEnd;
61 uint8_t mSize;
62
Jason Sams732f1c02009-06-18 16:58:42 -070063 Signal mSignalToWorker;
64 Signal mSignalToControl;
65
66
Jason Sams326e0dd2009-05-22 14:03:28 -070067
68public:
69 void * reserve(uint32_t bytes);
70 void commit(uint32_t command, uint32_t bytes);
71 void commitSync(uint32_t command, uint32_t bytes);
72
73 void flush();
74 const void * get(uint32_t *command, uint32_t *bytesData);
75 void next();
76
77 void makeSpace(uint32_t bytes);
78
79 bool isEmpty() const;
80 uint32_t getFreeSpace() const;
81
82
83private:
84 void dumpState(const char *) const;
85};
86
87
88}
89#endif