xref: /aosp_15_r20/external/cronet/base/threading/thread_collision_warner.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2010 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 #include "base/threading/thread_collision_warner.h"
6 
7 #include <ostream>
8 
9 #include "base/notreached.h"
10 #include "base/threading/platform_thread.h"
11 
12 namespace base {
13 
warn()14 void DCheckAsserter::warn() {
15   NOTREACHED() << "Thread Collision";
16 }
17 
CurrentThread()18 static subtle::Atomic32 CurrentThread() {
19   const PlatformThreadId current_thread_id = PlatformThread::CurrentId();
20   // We need to get the thread id into an atomic data type. This might be a
21   // truncating conversion, but any loss-of-information just increases the
22   // chance of a fault negative, not a false positive.
23   const subtle::Atomic32 atomic_thread_id =
24       static_cast<subtle::Atomic32>(current_thread_id);
25 
26   return atomic_thread_id;
27 }
28 
EnterSelf()29 void ThreadCollisionWarner::EnterSelf() {
30   // If the active thread is 0 then I'll write the current thread ID
31   // if two or more threads arrive here only one will succeed to
32   // write on valid_thread_id_ the current thread ID.
33   subtle::Atomic32 current_thread_id = CurrentThread();
34 
35   int previous_value = subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
36                                                         0,
37                                                         current_thread_id);
38   if (previous_value != 0 && previous_value != current_thread_id) {
39     // gotcha! a thread is trying to use the same class and that is
40     // not current thread.
41     asserter_->warn();
42   }
43 
44   subtle::NoBarrier_AtomicIncrement(&counter_, 1);
45 }
46 
Enter()47 void ThreadCollisionWarner::Enter() {
48   subtle::Atomic32 current_thread_id = CurrentThread();
49 
50   if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
51                                        0,
52                                        current_thread_id) != 0) {
53     // gotcha! another thread is trying to use the same class.
54     asserter_->warn();
55   }
56 
57   subtle::NoBarrier_AtomicIncrement(&counter_, 1);
58 }
59 
Leave()60 void ThreadCollisionWarner::Leave() {
61   if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0) {
62     subtle::NoBarrier_Store(&valid_thread_id_, 0);
63   }
64 }
65 
66 }  // namespace base
67