xref: /aosp_15_r20/external/libchrome/base/thread_annotations_unittest.nc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker// Copyright 2018 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker// found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker// This is a "No Compile Test" suite.
6*635a8641SAndroid Build Coastguard Worker// https://dev.chromium.org/developers/testing/no-compile-tests
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker#include "base/thread_annotations.h"
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Workernamespace {
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Workerclass LOCKABLE Lock {
13*635a8641SAndroid Build Coastguard Worker public:
14*635a8641SAndroid Build Coastguard Worker  void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
15*635a8641SAndroid Build Coastguard Worker  void Release() UNLOCK_FUNCTION() {}
16*635a8641SAndroid Build Coastguard Worker};
17*635a8641SAndroid Build Coastguard Worker
18*635a8641SAndroid Build Coastguard Workerclass SCOPED_LOCKABLE AutoLock {
19*635a8641SAndroid Build Coastguard Worker public:
20*635a8641SAndroid Build Coastguard Worker  AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
21*635a8641SAndroid Build Coastguard Worker    lock.Acquire();
22*635a8641SAndroid Build Coastguard Worker  }
23*635a8641SAndroid Build Coastguard Worker  ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); }
24*635a8641SAndroid Build Coastguard Worker
25*635a8641SAndroid Build Coastguard Worker private:
26*635a8641SAndroid Build Coastguard Worker  Lock& lock_;
27*635a8641SAndroid Build Coastguard Worker};
28*635a8641SAndroid Build Coastguard Workerclass ThreadSafe {
29*635a8641SAndroid Build Coastguard Worker public:
30*635a8641SAndroid Build Coastguard Worker  void BuggyIncrement();
31*635a8641SAndroid Build Coastguard Worker private:
32*635a8641SAndroid Build Coastguard Worker  Lock lock_;
33*635a8641SAndroid Build Coastguard Worker  int counter_ GUARDED_BY(lock_);
34*635a8641SAndroid Build Coastguard Worker};
35*635a8641SAndroid Build Coastguard Worker
36*635a8641SAndroid Build Coastguard Worker#if defined(NCTEST_LOCK_WITHOUT_UNLOCK)  // [r"fatal error: mutex 'lock_' is still held at the end of function"]
37*635a8641SAndroid Build Coastguard Worker
38*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::BuggyIncrement() {
39*635a8641SAndroid Build Coastguard Worker  lock_.Acquire();
40*635a8641SAndroid Build Coastguard Worker  ++counter_;
41*635a8641SAndroid Build Coastguard Worker  // Forgot to release the lock.
42*635a8641SAndroid Build Coastguard Worker}
43*635a8641SAndroid Build Coastguard Worker
44*635a8641SAndroid Build Coastguard Worker#elif defined(NCTEST_ACCESS_WITHOUT_LOCK)  // [r"fatal error: writing variable 'counter_' requires holding mutex 'lock_' exclusively"]
45*635a8641SAndroid Build Coastguard Worker
46*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::BuggyIncrement() {
47*635a8641SAndroid Build Coastguard Worker  // Member access without holding the lock guarding it.
48*635a8641SAndroid Build Coastguard Worker  ++counter_;
49*635a8641SAndroid Build Coastguard Worker}
50*635a8641SAndroid Build Coastguard Worker
51*635a8641SAndroid Build Coastguard Worker#elif defined(NCTEST_ACCESS_WITHOUT_SCOPED_LOCK)  // [r"fatal error: writing variable 'counter_' requires holding mutex 'lock_' exclusively"]
52*635a8641SAndroid Build Coastguard Worker
53*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::BuggyIncrement() {
54*635a8641SAndroid Build Coastguard Worker  {
55*635a8641SAndroid Build Coastguard Worker    AutoLock auto_lock(lock_);
56*635a8641SAndroid Build Coastguard Worker    // The AutoLock will go out of scope before the guarded member access.
57*635a8641SAndroid Build Coastguard Worker  }
58*635a8641SAndroid Build Coastguard Worker  ++counter_;
59*635a8641SAndroid Build Coastguard Worker}
60*635a8641SAndroid Build Coastguard Worker
61*635a8641SAndroid Build Coastguard Worker#elif defined(NCTEST_GUARDED_BY_WRONG_TYPE)  // [r"fatal error: 'guarded_by' attribute requires arguments whose type is annotated"]
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Workerint not_lockable;
64*635a8641SAndroid Build Coastguard Workerint global_counter GUARDED_BY(not_lockable);
65*635a8641SAndroid Build Coastguard Worker
66*635a8641SAndroid Build Coastguard Worker// Defined to avoid link error.
67*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::BuggyIncrement() { }
68*635a8641SAndroid Build Coastguard Worker
69*635a8641SAndroid Build Coastguard Worker#endif
70*635a8641SAndroid Build Coastguard Worker
71*635a8641SAndroid Build Coastguard Worker}  // anonymous namespace
72