[go: nahoru, domu]

blob: 726da7cf625c652b48477ffee2f0d281ee6123a6 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Victor Costan63cd2c22018-02-16 02:29:072// 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
Ali Hijazia8877892022-11-10 20:51:037#include "base/memory/raw_ref.h"
Victor Costan63cd2c22018-02-16 02:29:078#include "testing/gtest/include/gtest/gtest.h"
9
10namespace {
11
12class LOCKABLE Lock {
13 public:
14 void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
15 void Release() UNLOCK_FUNCTION() {}
16};
17
18class SCOPED_LOCKABLE AutoLock {
19 public:
20 AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
21 lock.Acquire();
22 }
Ali Hijazia8877892022-11-10 20:51:0323 ~AutoLock() UNLOCK_FUNCTION() { lock_->Release(); }
Victor Costan63cd2c22018-02-16 02:29:0724
25 private:
Ali Hijazia8877892022-11-10 20:51:0326 const raw_ref<Lock> lock_;
Victor Costan63cd2c22018-02-16 02:29:0727};
28
29class ThreadSafe {
30 public:
31 void ExplicitIncrement();
32 void ImplicitIncrement();
33
34 private:
35 Lock lock_;
36 int counter_ GUARDED_BY(lock_);
37};
38
39void ThreadSafe::ExplicitIncrement() {
40 lock_.Acquire();
41 ++counter_;
42 lock_.Release();
43}
44
45void ThreadSafe::ImplicitIncrement() {
46 AutoLock auto_lock(lock_);
47 counter_++;
48}
49
50TEST(ThreadAnnotationsTest, ExplicitIncrement) {
51 ThreadSafe thread_safe;
52 thread_safe.ExplicitIncrement();
53}
54TEST(ThreadAnnotationsTest, ImplicitIncrement) {
55 ThreadSafe thread_safe;
56 thread_safe.ImplicitIncrement();
57}
58
59} // anonymous namespace