Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(791)

Side by Side Diff: base/synchronization/atomic_flag.h

Issue 2187333002: Loosen/improve AtomicFlag semantics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: side-effects Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_ 5 #ifndef BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
6 #define BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_ 6 #define BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
7 7
8 #include "base/atomicops.h" 8 #include "base/atomicops.h"
9 #include "base/base_export.h" 9 #include "base/base_export.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/threading/thread_checker.h" 11 #include "base/sequence_checker.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 // A flag that can safely be set from one thread and read from other threads. 15 // A flag that can safely be set from one thread and read from other threads.
16 // 16 //
17 // This class IS NOT intended for synchronization between threads. 17 // This class IS NOT intended for synchronization between threads.
18 class BASE_EXPORT AtomicFlag { 18 class BASE_EXPORT AtomicFlag {
19 public: 19 public:
20 AtomicFlag(); 20 AtomicFlag();
21 ~AtomicFlag() = default; 21 ~AtomicFlag() = default;
22 22
23 // Set the flag. May only be called on the thread which created the object. 23 // Set the flag. Must always be called from the same sequence.
24 void Set(); 24 void Set();
25 25
26 // Returns true iff the flag was set. 26 // Returns true iff the flag was set. If this returns true, the current thread
27 // is guaranteed to be synchronized with all memory operations on the sequence
28 // which invoked Set() up until at least the first call to Set() on it.
27 bool IsSet() const; 29 bool IsSet() const;
28 30
29 // Resets the flag. Be careful when using this: callers might not expect 31 // Resets the flag. Be careful when using this: callers might not expect
30 // IsSet() to return false after returning true once. 32 // IsSet() to return false after returning true once. After a call to
31 void UnsafeResetForTesting(); 33 // UnsafeReset(), the AtomicFlag is safe and regains it's initial properties
danakj 2016/08/01 17:14:56 I dont like the comment saying it becomes safe by
gab 2016/08/01 17:46:45 Well it's "unsafe" unless external measures (as de
34 // (Set() may even bind to a new sequence) if and only if all threads using it
35 // are synchronized past the call to UnsafeReset().
36 void UnsafeReset();
fdoray 2016/08/01 16:15:51 Keep ForTesting() if you no longer need to call th
danakj 2016/08/01 17:14:56 I agree.
32 37
33 private: 38 private:
34 base::subtle::Atomic32 flag_ = 0; 39 base::subtle::Atomic32 flag_ = 0;
35 ThreadChecker thread_checker_; 40 SequenceChecker set_sequence_checker_;
fdoray 2016/07/29 20:41:37 Given that there are no guarantees about the synch
gab 2016/08/01 15:08:29 I think it's useful because the IsSet() callers ca
fdoray 2016/08/01 16:15:51 If there are multiple callers to Set(), IsSet() ca
36 41
37 DISALLOW_COPY_AND_ASSIGN(AtomicFlag); 42 DISALLOW_COPY_AND_ASSIGN(AtomicFlag);
38 }; 43 };
39 44
40 } // namespace base 45 } // namespace base
41 46
42 #endif // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_ 47 #endif // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
OLDNEW
« no previous file with comments | « no previous file | base/synchronization/atomic_flag.cc » ('j') | base/synchronization/atomic_flag_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698