1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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
15 uint64_t AsyncTransactionTracker::sSerialCounter(0);
16 Mutex
* AsyncTransactionTracker::sLock
= nullptr;
18 AsyncTransactionTracker::AsyncTransactionTracker()
19 : mSerial(GetNextSerial())
20 , mCompletedMonitor("AsyncTransactionTracker.mCompleted")
25 AsyncTransactionTracker::~AsyncTransactionTracker()
30 AsyncTransactionTracker::WaitComplete()
32 MOZ_ASSERT(!InImageBridgeChildThread());
34 MonitorAutoLock
mon(mCompletedMonitor
);
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");
43 printf_stderr("Waiting async transaction complete.\n");
49 printf_stderr("Timeout of waiting transaction complete.");
54 AsyncTransactionTracker::NotifyComplete()
56 MonitorAutoLock
mon(mCompletedMonitor
);
57 MOZ_ASSERT(!mCompleted
);
60 mCompletedMonitor
.Notify();
64 AsyncTransactionTracker::NotifyCancel()
66 MonitorAutoLock
mon(mCompletedMonitor
);
67 MOZ_ASSERT(!mCompleted
);
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();
100 sTrackersHolders
.erase(mSerial
);
102 sHolderLock
->Unlock();
105 MOZ_COUNT_DTOR(AsyncTransactionTrackersHolder
);
109 AsyncTransactionTrackersHolder::HoldUntilComplete(AsyncTransactionTracker
* aTransactionTracker
)
111 if (!aTransactionTracker
) {
115 if (mIsTrackersHolderDestroyed
&& aTransactionTracker
) {
116 aTransactionTracker
->NotifyComplete();
120 if (aTransactionTracker
) {
121 MutexAutoLock
lock(*sHolderLock
);
122 mAsyncTransactionTrackeres
[aTransactionTracker
->GetId()] = aTransactionTracker
;
127 AsyncTransactionTrackersHolder::TransactionCompleteted(uint64_t aTransactionId
)
129 MutexAutoLock
lock(*sHolderLock
);
130 TransactionCompletetedInternal(aTransactionId
);
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
);
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
);
156 AsyncTransactionTrackersHolder::TransactionCompleteted(uint64_t aHolderId
, uint64_t aTransactionId
)
158 MutexAutoLock
lock(*sHolderLock
);
159 AsyncTransactionTrackersHolder
* holder
= sTrackersHolders
[aHolderId
];
163 holder
->TransactionCompletetedInternal(aTransactionId
);
167 AsyncTransactionTrackersHolder::SetReleaseFenceHandle(FenceHandle
& aReleaseFenceHandle
,
169 uint64_t aTransactionId
)
171 MutexAutoLock
lock(*sHolderLock
);
172 AsyncTransactionTrackersHolder
* holder
= sTrackersHolders
[aHolderId
];
176 holder
->SetReleaseFenceHandle(aReleaseFenceHandle
, aTransactionId
);
180 AsyncTransactionTrackersHolder::ClearAllAsyncTransactionTrackers()
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();
192 sHolderLock
->Unlock();
197 AsyncTransactionTrackersHolder::DestroyAsyncTransactionTrackersHolder() {
198 mIsTrackersHolderDestroyed
= true;
199 ClearAllAsyncTransactionTrackers();
203 } // namespace layers
204 } // namespace mozilla