Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / dom / quota / ThreadUtils.h
blob7bc849242b77e95173dc5c5abcc099fa58185f51
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 DOM_QUOTA_THREADUTILS_H_
8 #define DOM_QUOTA_THREADUTILS_H_
10 #include <cstdint>
11 #include <functional>
13 #include "mozilla/StaticPrefsBase.h"
15 enum class nsresult : uint32_t;
17 namespace mozilla::dom::quota {
19 /**
20 * Add a temporary thread observer and listen for the "AfterProcessNextEvent"
21 * notification. Once the notification is received, remove the temporary thread
22 * observer and call aCallback.
23 * In practice, this calls aCallback immediately after the current thread is
24 * done with running and releasing recently popped event from thread's event
25 * queue.
26 * If called multiple times, all the callbacks will be executed, in the
27 * order in which RunAfterProcessingCurrentEvent() was called.
28 * Use this method if you need to dispatch the same or some other runnable to
29 * another thread in a way which prevents any race conditions (for example
30 * unpredictable releases of objects).
31 * This method should be used only in existing code which can't be easily
32 * converted to use MozPromise which doesn't have the problem with
33 * unpredictable releases of objects, see:
34 * https://searchfox.org/mozilla-central/rev/4582d908c17fbf7924f5699609fe4a12c28ddc4a/xpcom/threads/MozPromise.h#866
36 * Note: Calling this method from a thread pool is not supported since thread
37 * pools don't fire the "AfterProcessNextEvent" notification. The method has
38 * a diagnostic assertion for that so any calls like that will be caught
39 * in builds with enabled diagnostic assertions. The callback will never
40 * get executed in other builds, such as release builds. The limitation can
41 * be removed completely when thread pool implementation gets support for firing
42 * the "AfterProcessNextEvent".
44 nsresult RunAfterProcessingCurrentEvent(std::function<void()>&& aCallback);
46 /**
47 * Causes the current thread to yield for a specified amount of milliseconds if
48 * a mirrored preference value is not zero.
50 * @param aMirroredPrefValue
51 * A mirrored preference value. If this value is greater than zero, the
52 * function will cause the current thread to sleep for that number of
53 * milliseconds.
55 void SleepIfEnabled(StripAtomic<RelaxedAtomicUint32> aMirroredPrefValue);
57 } // namespace mozilla::dom::quota
59 #endif // DOM_QUOTA_THREADUTILS_H_