tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ThreadUtils.h (2577B)


      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/. */
      6 
      7 #ifndef DOM_QUOTA_THREADUTILS_H_
      8 #define DOM_QUOTA_THREADUTILS_H_
      9 
     10 #include <cstdint>
     11 #include <functional>
     12 
     13 #include "mozilla/StaticPrefsBase.h"
     14 
     15 enum class nsresult : uint32_t;
     16 
     17 namespace mozilla::dom::quota {
     18 
     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
     35 *
     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".
     43 */
     44 nsresult RunAfterProcessingCurrentEvent(std::function<void()>&& aCallback);
     45 
     46 /**
     47 * Causes the current thread to yield for a specified amount of milliseconds if
     48 * a mirrored preference value is not zero.
     49 *
     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.
     54 */
     55 void SleepIfEnabled(StripAtomic<RelaxedAtomicUint32> aMirroredPrefValue);
     56 
     57 }  // namespace mozilla::dom::quota
     58 
     59 #endif  // DOM_QUOTA_THREADUTILS_H_