Bumping manifests a=b2g-bump
[gecko.git] / gfx / layers / ipc / AsyncTransactionTracker.cpp
blob55fd309858f7dde9eff845128bf6cf1b6fb257e9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "AsyncTransactionTracker.h"
10 #include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild
12 namespace mozilla {
13 namespace layers {
15 uint64_t AsyncTransactionTracker::sSerialCounter(0);
16 Mutex* AsyncTransactionTracker::sLock = nullptr;
18 AsyncTransactionTracker::AsyncTransactionTracker()
19 : mSerial(GetNextSerial())
20 , mCompletedMonitor("AsyncTransactionTracker.mCompleted")
21 , mCompleted(false)
25 AsyncTransactionTracker::~AsyncTransactionTracker()
29 void
30 AsyncTransactionTracker::WaitComplete()
32 MOZ_ASSERT(!InImageBridgeChildThread());
34 MonitorAutoLock mon(mCompletedMonitor);
35 int count = 0;
36 const int maxCount = 5;
37 while (!mCompleted && (count < maxCount)) {
38 if (!NS_SUCCEEDED(mCompletedMonitor.Wait(PR_MillisecondsToInterval(10000)))) {
39 NS_WARNING("Failed to wait Monitor");
40 return;
42 if (count > 1) {
43 printf_stderr("Waiting async transaction complete.\n");
45 count++;
48 if (!mCompleted) {
49 printf_stderr("Timeout of waiting transaction complete.");
53 void
54 AsyncTransactionTracker::NotifyComplete()
56 MonitorAutoLock mon(mCompletedMonitor);
57 MOZ_ASSERT(!mCompleted);
58 mCompleted = true;
59 Complete();
60 mCompletedMonitor.Notify();
63 void
64 AsyncTransactionTracker::NotifyCancel()
66 MonitorAutoLock mon(mCompletedMonitor);
67 MOZ_ASSERT(!mCompleted);
68 mCompleted = true;
69 Cancel();
70 mCompletedMonitor.Notify();
73 uint64_t AsyncTransactionTrackersHolder::sSerialCounter(0);
74 Mutex* AsyncTransactionTrackersHolder::sHolderLock = nullptr;
76 std::map<uint64_t, AsyncTransactionTrackersHolder*> AsyncTransactionTrackersHolder::sTrackersHolders;
78 AsyncTransactionTrackersHolder::AsyncTransactionTrackersHolder()
79 : mSerial(GetNextSerial())
80 , mIsTrackersHolderDestroyed(false)
82 MOZ_COUNT_CTOR(AsyncTransactionTrackersHolder);
84 MOZ_ASSERT(sHolderLock);
85 MutexAutoLock lock(*sHolderLock);
86 sTrackersHolders[mSerial] = this;
90 AsyncTransactionTrackersHolder::~AsyncTransactionTrackersHolder()
92 if (!mIsTrackersHolderDestroyed) {
93 DestroyAsyncTransactionTrackersHolder();
97 if (sHolderLock) {
98 sHolderLock->Lock();
100 sTrackersHolders.erase(mSerial);
101 if (sHolderLock) {
102 sHolderLock->Unlock();
105 MOZ_COUNT_DTOR(AsyncTransactionTrackersHolder);
108 void
109 AsyncTransactionTrackersHolder::HoldUntilComplete(AsyncTransactionTracker* aTransactionTracker)
111 if (!aTransactionTracker) {
112 return;
115 if (mIsTrackersHolderDestroyed && aTransactionTracker) {
116 aTransactionTracker->NotifyComplete();
117 return;
120 if (aTransactionTracker) {
121 MutexAutoLock lock(*sHolderLock);
122 mAsyncTransactionTrackeres[aTransactionTracker->GetId()] = aTransactionTracker;
126 void
127 AsyncTransactionTrackersHolder::TransactionCompleteted(uint64_t aTransactionId)
129 MutexAutoLock lock(*sHolderLock);
130 TransactionCompletetedInternal(aTransactionId);
133 void
134 AsyncTransactionTrackersHolder::TransactionCompletetedInternal(uint64_t aTransactionId)
136 std::map<uint64_t, RefPtr<AsyncTransactionTracker> >::iterator it
137 = mAsyncTransactionTrackeres.find(aTransactionId);
138 if (it != mAsyncTransactionTrackeres.end()) {
139 it->second->NotifyComplete();
140 mAsyncTransactionTrackeres.erase(it);
144 void
145 AsyncTransactionTrackersHolder::SetReleaseFenceHandle(FenceHandle& aReleaseFenceHandle,
146 uint64_t aTransactionId)
148 std::map<uint64_t, RefPtr<AsyncTransactionTracker> >::iterator it
149 = mAsyncTransactionTrackeres.find(aTransactionId);
150 if (it != mAsyncTransactionTrackeres.end()) {
151 it->second->SetReleaseFenceHandle(aReleaseFenceHandle);
155 /*static*/ void
156 AsyncTransactionTrackersHolder::TransactionCompleteted(uint64_t aHolderId, uint64_t aTransactionId)
158 MutexAutoLock lock(*sHolderLock);
159 AsyncTransactionTrackersHolder* holder = sTrackersHolders[aHolderId];
160 if (!holder) {
161 return;
163 holder->TransactionCompletetedInternal(aTransactionId);
166 /*static*/ void
167 AsyncTransactionTrackersHolder::SetReleaseFenceHandle(FenceHandle& aReleaseFenceHandle,
168 uint64_t aHolderId,
169 uint64_t aTransactionId)
171 MutexAutoLock lock(*sHolderLock);
172 AsyncTransactionTrackersHolder* holder = sTrackersHolders[aHolderId];
173 if (!holder) {
174 return;
176 holder->SetReleaseFenceHandle(aReleaseFenceHandle, aTransactionId);
179 void
180 AsyncTransactionTrackersHolder::ClearAllAsyncTransactionTrackers()
182 if (sHolderLock) {
183 sHolderLock->Lock();
185 std::map<uint64_t, RefPtr<AsyncTransactionTracker> >::iterator it;
186 for (it = mAsyncTransactionTrackeres.begin();
187 it != mAsyncTransactionTrackeres.end(); it++) {
188 it->second->NotifyCancel();
190 mAsyncTransactionTrackeres.clear();
191 if (sHolderLock) {
192 sHolderLock->Unlock();
196 void
197 AsyncTransactionTrackersHolder::DestroyAsyncTransactionTrackersHolder() {
198 mIsTrackersHolderDestroyed = true;
199 ClearAllAsyncTransactionTrackers();
203 } // namespace layers
204 } // namespace mozilla