blob: d0a4356a6cf6944b4d0b1a893c3426ba7c067631 [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 Samsd19f10d2009-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
Jason Samsf5b45962009-08-25 14:49:07 -070028// will not require locking. It is not threadsafe for multiple
Jason Samsd19f10d2009-05-22 14:03:28 -070029// readers or writers by design.
30
Jason Samsf5b45962009-08-25 14:49:07 -070031class LocklessCommandFifo
Jason Samsd19f10d2009-05-22 14:03:28 -070032{
33public:
34 bool init(uint32_t size);
Jason Samsf5b45962009-08-25 14:49:07 -070035 void shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -070036
37 LocklessCommandFifo();
38 ~LocklessCommandFifo();
39
40
41protected:
Jason Sams5f7fc272009-06-18 16:58:42 -070042 class Signal {
43 public:
44 Signal();
45 ~Signal();
46
47 bool init();
48
49 void set();
50 void wait();
51
52 protected:
53 bool mSet;
54 pthread_mutex_t mMutex;
55 pthread_cond_t mCondition;
56 };
57
Jason Samsd19f10d2009-05-22 14:03:28 -070058 uint8_t * volatile mPut;
59 uint8_t * volatile mGet;
60 uint8_t * mBuffer;
61 uint8_t * mEnd;
62 uint8_t mSize;
Jason Samsf5b45962009-08-25 14:49:07 -070063 bool mInShutdown;
Jason Samsd19f10d2009-05-22 14:03:28 -070064
Jason Sams5f7fc272009-06-18 16:58:42 -070065 Signal mSignalToWorker;
66 Signal mSignalToControl;
67
68
Jason Samsd19f10d2009-05-22 14:03:28 -070069
70public:
71 void * reserve(uint32_t bytes);
72 void commit(uint32_t command, uint32_t bytes);
73 void commitSync(uint32_t command, uint32_t bytes);
74
75 void flush();
76 const void * get(uint32_t *command, uint32_t *bytesData);
77 void next();
78
79 void makeSpace(uint32_t bytes);
80
81 bool isEmpty() const;
82 uint32_t getFreeSpace() const;
83
84
85private:
86 void dumpState(const char *) const;
87};
88
89
90}
91#endif