1 // Copyright 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.
5 // This is a "No Compile Test" suite.
6 // https://dev.chromium.org/developers/testing/no-compile-tests
8 #include "base/thread_annotations.h"
14 void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
15 void Release() UNLOCK_FUNCTION() {}
18 class SCOPED_LOCKABLE AutoLock {
20 AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
23 ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); }
30 void BuggyIncrement();
33 int counter_ GUARDED_BY(lock_);
36 #if defined(NCTEST_LOCK_WITHOUT_UNLOCK) // [r"fatal error: mutex 'lock_' is still held at the end of function"]
38 void ThreadSafe::BuggyIncrement() {
41 // Forgot to release the lock.
44 #elif defined(NCTEST_ACCESS_WITHOUT_LOCK) // [r"fatal error: writing variable 'counter_' requires holding mutex 'lock_' exclusively"]
46 void ThreadSafe::BuggyIncrement() {
47 // Member access without holding the lock guarding it.
51 #elif defined(NCTEST_ACCESS_WITHOUT_SCOPED_LOCK) // [r"fatal error: writing variable 'counter_' requires holding mutex 'lock_' exclusively"]
53 void ThreadSafe::BuggyIncrement() {
55 AutoLock auto_lock(lock_);
56 // The AutoLock will go out of scope before the guarded member access.
61 #elif defined(NCTEST_GUARDED_BY_WRONG_TYPE) // [r"fatal error: 'guarded_by' attribute requires arguments whose type is annotated"]
64 int global_counter GUARDED_BY(not_lockable);
66 // Defined to avoid link error.
67 void ThreadSafe::BuggyIncrement() { }
71 } // anonymous namespace