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 GPU_DCHECK(!acquired_); | |
apatrick_chromium
2012/03/29 21:03:58
This should not assert if another thread has acqui
greggman
2012/03/29 21:25:25
Done.
| |
40 pthread_mutex_lock(&mutex_); | |
41 acquired_ = true; | |
42 } | |
43 | |
44 void Release() { | |
45 GPU_DCHECK(acquired_); | |
46 pthread_mutex_unlock(&mutex_); | |
47 acquired_ = false; | |
apatrick_chromium
2012/03/29 21:03:58
There's a race here. Once the mutex is unlocked, a
greggman
2012/03/29 21:25:25
Done.
| |
48 } | |
49 | |
50 bool Try() { | |
51 GPU_DCHECK(!acquired_); | |
52 acquired_ = pthread_mutex_trylock(&mutex_) == 0; | |
apatrick_chromium
2012/03/29 21:03:58
acquired_ should not be set to false if trylock fa
greggman
2012/03/29 21:25:25
Done.
| |
53 return acquired_; | |
apatrick_chromium
2012/03/29 21:03:58
Another thread might set this to true even though
greggman
2012/03/29 21:25:25
Done.
| |
54 } | |
55 | |
56 void AssertAcquired() const { | |
57 GPU_DCHECK(acquired_); | |
58 } | |
59 | |
60 private: | |
61 bool acquired_; | |
62 pthread_mutex_t mutex_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(LockImpl); | |
65 }; | |
66 | |
67 #else // !__native_client__ | |
68 | |
69 struct LockImpl : public base::Lock { | |
apatrick_chromium
2012/03/29 21:03:58
This could be a typedef.
greggman
2012/03/29 21:25:25
Done.
greggman
2012/03/29 21:31:32
I get an error when it's a typedef because of the
| |
70 }; | |
71 | |
72 #endif // !__native_client__ | |
73 | |
74 Lock::Lock() | |
75 : lock_(new LockImpl()) { | |
76 } | |
77 | |
78 Lock::~Lock() { | |
79 } | |
80 | |
81 void Lock::Acquire() { | |
82 lock_->Acquire(); | |
83 } | |
84 | |
85 void Lock::Release() { | |
86 lock_->Release(); | |
87 } | |
88 | |
89 bool Lock::Try() { | |
90 return lock_->Try(); | |
91 } | |
92 | |
93 void Lock::AssertAcquired() const { | |
94 return lock_->AssertAcquired(); | |
95 } | |
96 | |
21 } // namespace gpu | 97 } // namespace gpu |
22 | 98 |
OLD | NEW |