blob: 67ab434cf4dfecd1b1889825778aac54103601f9 [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#include "rsLocklessFifo.h"
18
19using namespace android;
20
21#include <utils/Log.h>
22
23LocklessCommandFifo::LocklessCommandFifo()
24{
25}
26
27LocklessCommandFifo::~LocklessCommandFifo()
28{
29}
30
31bool LocklessCommandFifo::init(uint32_t sizeInBytes)
32{
33 // Add room for a buffer reset command
34 mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4));
35 if (!mBuffer) {
36 LOGE("LocklessFifo allocation failure");
37 return false;
38 }
39
40 int status = pthread_mutex_init(&mMutex, NULL);
41 if (status) {
42 LOGE("LocklessFifo mutex init failure");
43 free(mBuffer);
44 return false;
45 }
46 status = pthread_cond_init(&mCondition, NULL);
47 if (status) {
48 LOGE("LocklessFifo condition init failure");
49 pthread_mutex_destroy(&mMutex);
50 free(mBuffer);
51 return false;
52 }
53
54 mSize = sizeInBytes;
55 mPut = mBuffer;
56 mGet = mBuffer;
57 mEnd = mBuffer + (sizeInBytes) - 1;
58 dumpState("init");
59 return true;
60}
61
62uint32_t LocklessCommandFifo::getFreeSpace() const
63{
64 int32_t freeSpace = 0;
65 //dumpState("getFreeSpace");
66
67 if (mPut >= mGet) {
68 freeSpace = mEnd - mPut;
69 } else {
70 freeSpace = mGet - mPut;
71 }
72
73 if (freeSpace < 0) {
74 freeSpace = 0;
75 }
76
Jason Sams565ac362009-06-03 16:04:54 -070077 //LOGE("free %i", freeSpace);
Jason Sams326e0dd2009-05-22 14:03:28 -070078 return freeSpace;
79}
80
81bool LocklessCommandFifo::isEmpty() const
82{
83 return mPut == mGet;
84}
85
86
87void * LocklessCommandFifo::reserve(uint32_t sizeInBytes)
88{
Jason Sams565ac362009-06-03 16:04:54 -070089 // Add space for command header and loop token;
90 sizeInBytes += 8;
Jason Sams326e0dd2009-05-22 14:03:28 -070091
92 //dumpState("reserve");
93 if (getFreeSpace() < sizeInBytes) {
94 makeSpace(sizeInBytes);
95 }
96
97 return mPut + 4;
98}
99
100void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes)
101{
102 //LOGE("commit cmd %i size %i", command, sizeInBytes);
103 //dumpState("commit 1");
104 reinterpret_cast<uint16_t *>(mPut)[0] = command;
105 reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes;
106 mPut += ((sizeInBytes + 3) & ~3) + 4;
107 //dumpState("commit 2");
108
109}
110
111void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes)
112{
113 commit(command, sizeInBytes);
114 flush();
115}
116
117void LocklessCommandFifo::flush()
118{
119 //dumpState("flush 1");
120 while(mPut != mGet) {
121 usleep(1);
122 }
123 //dumpState("flush 2");
124}
125
126const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData)
127{
128 while(1) {
129 while(isEmpty()) {
130 usleep(10);
131 }
132 //dumpState("get 3");
133
134 *command = reinterpret_cast<const uint16_t *>(mGet)[0];
135 *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1];
136 //LOGE("Got %i, %i", *command, *bytesData);
137
138 if (*command) {
139 // non-zero command is valid
140 return mGet+4;
141 }
142
143 // zero command means reset to beginning.
144 mGet = mBuffer;
145 }
146}
147
148void LocklessCommandFifo::next()
149{
150 uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1];
151 mGet += ((bytes + 3) & ~3) + 4;
152 //dumpState("next");
153}
154
155void LocklessCommandFifo::makeSpace(uint32_t bytes)
156{
Jason Sams565ac362009-06-03 16:04:54 -0700157 //dumpState("make space");
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 if ((mPut+bytes) > mEnd) {
159 // Need to loop regardless of where get is.
Jason Sams565ac362009-06-03 16:04:54 -0700160 while((mGet > mPut) && (mBuffer+4 >= mGet)) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700161 sleep(1);
162 }
163
164 // Toss in a reset then the normal wait for space will do the rest.
165 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
166 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
Jason Sams565ac362009-06-03 16:04:54 -0700167 mPut = mBuffer;
Jason Sams326e0dd2009-05-22 14:03:28 -0700168 }
169
170 // it will fit here so we just need to wait for space.
171 while(getFreeSpace() < bytes) {
172 sleep(1);
173 }
174
175}
176
177void LocklessCommandFifo::dumpState(const char *s) const
178{
179 LOGE("%s put %p, get %p, buf %p, end %p", s, mPut, mGet, mBuffer, mEnd);
180}
181
182