tor-browser

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

atomic_flag.h (1482B)


      1 // Copyright 2011 The Chromium Authors
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
      6 #define BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include <atomic>
     11 
     12 #include "base/base_export.h"
     13 #include "base/sequence_checker.h"
     14 
     15 namespace base {
     16 
     17 // A flag that can safely be set from one thread and read from other threads.
     18 //
     19 // This class IS NOT intended for synchronization between threads.
     20 class BASE_EXPORT AtomicFlag {
     21 public:
     22  AtomicFlag();
     23 
     24  AtomicFlag(const AtomicFlag&) = delete;
     25  AtomicFlag& operator=(const AtomicFlag&) = delete;
     26 
     27  ~AtomicFlag();
     28 
     29  // Set the flag. Must always be called from the same sequence.
     30  void Set();
     31 
     32  // Returns true iff the flag was set. If this returns true, the current thread
     33  // is guaranteed to be synchronized with all memory operations on the sequence
     34  // which invoked Set() up until at least the first call to Set() on it.
     35  bool IsSet() const {
     36    // Inline here: this has a measurable performance impact on base::WeakPtr.
     37    return flag_.load(std::memory_order_acquire) != 0;
     38  }
     39 
     40  // Resets the flag. Be careful when using this: callers might not expect
     41  // IsSet() to return false after returning true once.
     42  void UnsafeResetForTesting();
     43 
     44 private:
     45  std::atomic<uint_fast8_t> flag_{0};
     46  SEQUENCE_CHECKER(set_sequence_checker_);
     47 };
     48 
     49 }  // namespace base
     50 
     51 #endif  // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_