thread_annotations_unittest.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "thread_annotations.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace {
  7. class LOCKABLE Lock {
  8. public:
  9. void Acquire() EXCLUSIVE_LOCK_FUNCTION() {}
  10. void Release() UNLOCK_FUNCTION() {}
  11. };
  12. class SCOPED_LOCKABLE AutoLock {
  13. public:
  14. AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) {
  15. lock.Acquire();
  16. }
  17. ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); }
  18. private:
  19. Lock& lock_;
  20. };
  21. class ThreadSafe {
  22. public:
  23. void ExplicitIncrement();
  24. void ImplicitIncrement();
  25. private:
  26. Lock lock_;
  27. int counter_ GUARDED_BY(lock_);
  28. };
  29. void ThreadSafe::ExplicitIncrement() {
  30. lock_.Acquire();
  31. ++counter_;
  32. lock_.Release();
  33. }
  34. void ThreadSafe::ImplicitIncrement() {
  35. AutoLock auto_lock(lock_);
  36. counter_++;
  37. }
  38. TEST(ThreadAnnotationsTest, ExplicitIncrement) {
  39. ThreadSafe thread_safe;
  40. thread_safe.ExplicitIncrement();
  41. }
  42. TEST(ThreadAnnotationsTest, ImplicitIncrement) {
  43. ThreadSafe thread_safe;
  44. thread_safe.ImplicitIncrement();
  45. }
  46. } // anonymous namespace