OSDN Git Service

AndroidIA: Added support for Travis CI
[android-x86/external-minigbm.git] / cros_gralloc / cros_gralloc_spinlock.h
1 /*
2  * Copyright 2017 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #ifndef CROS_GRALLOC_SPINLOCK_H
8 #define CROS_GRALLOC_SPINLOCK_H
9
10 #include <atomic>
11
12 #ifndef DISABLE_LOCK
13 class SpinLock {
14  public:
15   void lock() {
16     while (atomic_lock_.test_and_set(std::memory_order_acquire)) {
17     }
18   }
19
20   void unlock() {
21     atomic_lock_.clear(std::memory_order_release);
22   }
23
24  private:
25   std::atomic_flag atomic_lock_ = ATOMIC_FLAG_INIT;
26 };
27
28 class ScopedSpinLock {
29  public:
30   explicit ScopedSpinLock(SpinLock& lock) : lock_(lock) {
31     lock_.lock();
32     locked_ = true;
33   }
34
35   ~ScopedSpinLock() {
36     if (locked_) {
37       lock_.unlock();
38       locked_ = false;
39     }
40   }
41
42  private:
43   SpinLock& lock_;
44   bool locked_;
45 };
46
47 #define SCOPED_SPIN_LOCK(X) \
48 ScopedSpinLock lock(X);
49 #else
50 class SpinLock {
51  public:
52   void lock() {
53   }
54
55   void unlock() {
56   }
57 };
58
59 #define SCOPED_SPIN_LOCK(X) ((void)0)
60 #endif
61
62 #endif  // PUBLIC_SPINLOCK_H_