blob: b4aafef72a1f12f5c28e018c95b18fc8567e13ec [file] [log] [blame]
Andrew Top0d1858f2019-05-15 22:01:47 -07001// 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
9namespace {
10
11class LOCKABLE Lock {
12 public:
13 void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
14 void Release() UNLOCK_FUNCTION() {}
15};
16
17class SCOPED_LOCKABLE AutoLock {
18 public:
19 AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
20 lock.Acquire();
21 }
22 ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); }
23
24 private:
25 Lock& lock_;
26};
27
28class ThreadSafe {
29 public:
30 void ExplicitIncrement();
31 void ImplicitIncrement();
32
33 private:
34 Lock lock_;
35 int counter_ GUARDED_BY(lock_);
36};
37
38void ThreadSafe::ExplicitIncrement() {
39 lock_.Acquire();
40 ++counter_;
41 lock_.Release();
42}
43
44void ThreadSafe::ImplicitIncrement() {
45 AutoLock auto_lock(lock_);
46 counter_++;
47}
48
49TEST(ThreadAnnotationsTest, ExplicitIncrement) {
50 ThreadSafe thread_safe;
51 thread_safe.ExplicitIncrement();
52}
53TEST(ThreadAnnotationsTest, ImplicitIncrement) {
54 ThreadSafe thread_safe;
55 thread_safe.ImplicitIncrement();
56}
57
58} // anonymous namespace