xref: /aosp_15_r20/external/libchrome/base/thread_annotations_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright (c) 2018 The Chromium Authors. All rights reserved.
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 "thread_annotations.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace {
10 
11 class LOCKABLE Lock {
12  public:
Acquire()13   void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
Release()14   void Release() UNLOCK_FUNCTION() {}
15 };
16 
17 class SCOPED_LOCKABLE AutoLock {
18  public:
EXCLUSIVE_LOCK_FUNCTION(lock)19   AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
20     lock.Acquire();
21   }
UNLOCK_FUNCTION()22   ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); }
23 
24  private:
25   Lock& lock_;
26 };
27 
28 class ThreadSafe {
29  public:
30   void ExplicitIncrement();
31   void ImplicitIncrement();
32 
33  private:
34   Lock lock_;
35   int counter_ GUARDED_BY(lock_);
36 };
37 
ExplicitIncrement()38 void ThreadSafe::ExplicitIncrement() {
39   lock_.Acquire();
40   ++counter_;
41   lock_.Release();
42 }
43 
ImplicitIncrement()44 void ThreadSafe::ImplicitIncrement() {
45   AutoLock auto_lock(lock_);
46   counter_++;
47 }
48 
TEST(ThreadAnnotationsTest,ExplicitIncrement)49 TEST(ThreadAnnotationsTest, ExplicitIncrement) {
50   ThreadSafe thread_safe;
51   thread_safe.ExplicitIncrement();
52 }
TEST(ThreadAnnotationsTest,ImplicitIncrement)53 TEST(ThreadAnnotationsTest, ImplicitIncrement) {
54   ThreadSafe thread_safe;
55   thread_safe.ImplicitIncrement();
56 }
57 
58 }  // anonymous namespace
59