OLD | NEW |
| 1 /* |
| 2 * Copyright 2015 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
1 #ifndef SkMutex_DEFINED | 8 #ifndef SkMutex_DEFINED |
2 #define SkMutex_DEFINED | 9 #define SkMutex_DEFINED |
3 | 10 |
4 // This file is not part of the public Skia API. | 11 // This file is not part of the public Skia API. |
5 #include "SkTypes.h" | 12 #include "SkTypes.h" |
6 | 13 |
7 #if defined(SK_BUILD_FOR_WIN) | 14 #if defined(SK_BUILD_FOR_WIN) |
8 #include "../ports/SkMutex_win.h" | 15 #include "../ports/SkMutex_win.h" |
9 #else | 16 #else |
10 #include "../ports/SkMutex_pthread.h" | 17 #include "../ports/SkMutex_pthread.h" |
11 #endif | 18 #endif |
12 | 19 |
13 class SkAutoMutexAcquire : SkNoncopyable { | 20 template <typename Lock> |
| 21 class SkAutoTAcquire : SkNoncopyable { |
14 public: | 22 public: |
15 explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { | 23 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { |
16 SkASSERT(fMutex != NULL); | 24 SkASSERT(fMutex != NULL); |
17 mutex.acquire(); | 25 mutex.acquire(); |
18 } | 26 } |
19 | 27 |
20 explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { | 28 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { |
21 if (mutex) { | 29 if (mutex) { |
22 mutex->acquire(); | 30 mutex->acquire(); |
23 } | 31 } |
24 } | 32 } |
25 | 33 |
26 /** If the mutex has not been released, release it now. */ | 34 /** If the mutex has not been released, release it now. */ |
27 ~SkAutoMutexAcquire() { | 35 ~SkAutoTAcquire() { |
28 if (fMutex) { | 36 if (fMutex) { |
29 fMutex->release(); | 37 fMutex->release(); |
30 } | 38 } |
31 } | 39 } |
32 | 40 |
33 /** If the mutex has not been released, release it now. */ | 41 /** If the mutex has not been released, release it now. */ |
34 void release() { | 42 void release() { |
35 if (fMutex) { | 43 if (fMutex) { |
36 fMutex->release(); | 44 fMutex->release(); |
37 fMutex = NULL; | 45 fMutex = NULL; |
38 } | 46 } |
39 } | 47 } |
40 | 48 |
41 /** Assert that we're holding the mutex. */ | 49 /** Assert that we're holding the mutex. */ |
42 void assertHeld() { | 50 void assertHeld() { |
43 SkASSERT(fMutex); | 51 SkASSERT(fMutex); |
44 fMutex->assertHeld(); | 52 fMutex->assertHeld(); |
45 } | 53 } |
46 | 54 |
47 private: | 55 private: |
48 SkBaseMutex* fMutex; | 56 Lock* fMutex; |
49 }; | 57 }; |
| 58 |
| 59 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
| 60 |
50 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 61 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
51 | 62 |
52 | 63 |
53 #endif//SkMutex_DEFINED | 64 #endif//SkMutex_DEFINED |
OLD | NEW |