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