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 3 // found in the LICENSE file. 4 5 #include "gpu/command_buffer/client/atomicops.h" 6 #include "gpu/command_buffer/common/logging.h" 7 8 #if !defined(__native_client__) 9 #include "base/atomicops.h" 10 #include "base/synchronization/lock.h" 11 #else 12 #include <pthread.h> 13 #endif 14 15 namespace gpu { 16 17 void MemoryBarrier() { 18 #if defined(__native_client__) 19 __sync_synchronize(); 20 #else 21 base::subtle::MemoryBarrier(); 22 #endif 23 } 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 98 } // namespace gpu 99 100