1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_SYNCHRONOUSTASK_H
8 #define MOZILLA_GFX_SYNCHRONOUSTASK_H
10 #include "mozilla/ReentrantMonitor.h" // for ReentrantMonitor, etc
15 // Helper that creates a monitor and a "done" flag, then enters the monitor.
16 class MOZ_STACK_CLASS SynchronousTask
{
17 friend class AutoCompleteTask
;
20 explicit SynchronousTask(const char* name
) : mMonitor(name
), mDone(false) {}
22 nsresult
Wait(PRIntervalTime aInterval
= PR_INTERVAL_NO_TIMEOUT
) {
23 ReentrantMonitorAutoEnter
lock(mMonitor
);
25 // For indefinite timeouts, wait in a while loop to handle spurious
27 while (aInterval
== PR_INTERVAL_NO_TIMEOUT
&& !mDone
) {
31 // For finite timeouts, we only check once for completion, and otherwise
32 // rely on the ReentrantMonitor to manage the interval. If the monitor
33 // returns too early, we'll never know, but we can check if the mDone
34 // flag was set to true, indicating that the task finished successfully.
36 // We ignore the return value from ReentrantMonitor::Wait, because it's
37 // always NS_OK, even in the case of timeout.
38 mMonitor
.Wait(aInterval
);
41 return NS_ERROR_ABORT
;
50 ReentrantMonitorAutoEnter
lock(mMonitor
);
56 ReentrantMonitor mMonitor MOZ_UNANNOTATED
;
60 class MOZ_STACK_CLASS AutoCompleteTask final
{
62 explicit AutoCompleteTask(SynchronousTask
* aTask
) : mTask(aTask
) {}
63 ~AutoCompleteTask() { mTask
->Complete(); }
66 SynchronousTask
* mTask
;
70 } // namespace mozilla