OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "../client/atomicops.h" | 5 #include "../client/atomicops.h" |
| 6 #include "../common/logging.h" |
6 | 7 |
7 #if !defined(__native_client__) | 8 #if !defined(__native_client__) |
8 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 10 #include "base/synchronization/lock.h" |
| 11 #else |
| 12 #include <pthread.h> |
9 #endif | 13 #endif |
10 | 14 |
11 namespace gpu { | 15 namespace gpu { |
12 | 16 |
13 void MemoryBarrier() { | 17 void MemoryBarrier() { |
14 #if defined(__native_client__) | 18 #if defined(__native_client__) |
15 __sync_synchronize(); | 19 __sync_synchronize(); |
16 #else | 20 #else |
17 base::subtle::MemoryBarrier(); | 21 base::subtle::MemoryBarrier(); |
18 #endif | 22 #endif |
19 } | 23 } |
20 | 24 |
| 25 #if defined(__native_client__) |
| 26 |
| 27 class LockImpl { |
| 28 public: |
| 29 LockImpl() |
| 30 : acquired_(false) { |
| 31 pthread_mutex_init(&mutex_, NULL); |
| 32 } |
| 33 |
| 34 ~LockImpl() { |
| 35 pthread_mutex_destroy(&mutex_); |
| 36 } |
| 37 |
| 38 void Acquire() { |
| 39 pthread_mutex_lock(&mutex_); |
| 40 acquired_ = true; |
| 41 } |
| 42 |
| 43 void Release() { |
| 44 GPU_DCHECK(acquired_); |
| 45 acquired_ = false; |
| 46 pthread_mutex_unlock(&mutex_); |
| 47 } |
| 48 |
| 49 bool Try() { |
| 50 bool acquired = pthread_mutex_trylock(&mutex_) == 0; |
| 51 if (acquired) { |
| 52 acquired_ = true; |
| 53 } |
| 54 return acquired; |
| 55 } |
| 56 |
| 57 void AssertAcquired() const { |
| 58 GPU_DCHECK(acquired_); |
| 59 } |
| 60 |
| 61 private: |
| 62 bool acquired_; |
| 63 pthread_mutex_t mutex_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(LockImpl); |
| 66 }; |
| 67 |
| 68 #else // !__native_client__ |
| 69 |
| 70 class LockImpl : public base::Lock { |
| 71 }; |
| 72 |
| 73 #endif // !__native_client__ |
| 74 |
| 75 Lock::Lock() |
| 76 : lock_(new LockImpl()) { |
| 77 } |
| 78 |
| 79 Lock::~Lock() { |
| 80 } |
| 81 |
| 82 void Lock::Acquire() { |
| 83 lock_->Acquire(); |
| 84 } |
| 85 |
| 86 void Lock::Release() { |
| 87 lock_->Release(); |
| 88 } |
| 89 |
| 90 bool Lock::Try() { |
| 91 return lock_->Try(); |
| 92 } |
| 93 |
| 94 void Lock::AssertAcquired() const { |
| 95 return lock_->AssertAcquired(); |
| 96 } |
| 97 |
21 } // namespace gpu | 98 } // namespace gpu |
22 | 99 |
OLD | NEW |