Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(549)

Side by Side Diff: include/core/SkMutex.h

Issue 1368333004: Move Mutexy things to private. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/tools.gyp ('k') | include/core/SkOnce.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
8 #ifndef SkMutex_DEFINED
9 #define SkMutex_DEFINED
10
11 // This file is not part of the public Skia API.
12 #include "../private/SkSemaphore.h"
13 #include "SkTypes.h"
14
15 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}}
16
17 #ifdef SK_DEBUG
18 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0}
19 #else
20 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT}
21 #endif
22
23 // Using POD-style initialization prevents the generation of a static initialize r.
24 //
25 // Without magic statics there are no thread safety guarantees on initialization
26 // of local statics (even POD). As a result, it is illegal to use
27 // SK_DECLARE_STATIC_MUTEX in a function.
28 //
29 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be
30 // initialized in a class with this macro.
31 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_ BASE_MUTEX_INIT;
32
33 // TODO(herb): unify this with the ThreadID in SkSharedMutex.cpp.
34 #ifdef SK_DEBUG
35 #ifdef SK_BUILD_FOR_WIN
36 #include <windows.h>
37 inline int64_t sk_get_thread_id() { return GetCurrentThreadId(); }
38 #else
39 #include <pthread.h>
40 inline int64_t sk_get_thread_id() { return (int64_t)pthread_self(); }
41 #endif
42 #endif
43
44 typedef int64_t SkThreadID;
45
46 SkDEBUGCODE(static const SkThreadID kNoOwner = 0;)
47
48 struct SkBaseMutex {
49 void acquire() {
50 fSemaphore.wait();
51 SkDEBUGCODE(fOwner = sk_get_thread_id();)
52 }
53
54 void release() {
55 this->assertHeld();
56 SkDEBUGCODE(fOwner = kNoOwner;)
57 fSemaphore.signal();
58 }
59
60 void assertHeld() {
61 SkASSERT(fOwner == sk_get_thread_id());
62 }
63
64 SkBaseSemaphore fSemaphore;
65 SkDEBUGCODE(SkThreadID fOwner;)
66 };
67
68 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq uire to work.
69 class SkMutex : public SkBaseMutex {
70 public:
71 SkMutex () {
72 fSemaphore = SK_MUTEX_SEMAPHORE_INIT;
73 SkDEBUGCODE(fOwner = kNoOwner);
74 }
75 ~SkMutex () { fSemaphore.deleteSemaphore(); }
76 SkMutex(const SkMutex&) = delete;
77 SkMutex& operator=(const SkMutex&) = delete;
78 };
79
80 template <typename Lock>
81 class SkAutoTAcquire : SkNoncopyable {
82 public:
83 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) {
84 SkASSERT(fMutex != NULL);
85 mutex.acquire();
86 }
87
88 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) {
89 if (mutex) {
90 mutex->acquire();
91 }
92 }
93
94 /** If the mutex has not been released, release it now. */
95 ~SkAutoTAcquire() {
96 if (fMutex) {
97 fMutex->release();
98 }
99 }
100
101 /** If the mutex has not been released, release it now. */
102 void release() {
103 if (fMutex) {
104 fMutex->release();
105 fMutex = NULL;
106 }
107 }
108
109 /** Assert that we're holding the mutex. */
110 void assertHeld() {
111 SkASSERT(fMutex);
112 fMutex->assertHeld();
113 }
114
115 private:
116 Lock* fMutex;
117 };
118
119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
121
122 #endif//SkMutex_DEFINED
OLDNEW
« no previous file with comments | « gyp/tools.gyp ('k') | include/core/SkOnce.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698