blob: 77a00f02354074ab6b6635ffaba4d3b48746401d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkMutex_DEFINED
#define SkMutex_DEFINED
// This file is not part of the public Skia API.
#include "../private/SkSemaphore.h"
#include "SkTypes.h"
#define SK_MUTEX_SEMAPHORE_INIT {1, {0}}
#ifdef SK_DEBUG
#define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0}
#else
#define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT}
#endif
// Using POD-style initialization prevents the generation of a static initializer.
//
// Without magic statics there are no thread safety guarantees on initialization
// of local statics (even POD). As a result, it is illegal to use
// SK_DECLARE_STATIC_MUTEX in a function.
//
// Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be
// initialized in a class with this macro.
#define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_BASE_MUTEX_INIT;
// TODO(herb): unify this with the ThreadID in SkSharedMutex.cpp.
#ifdef SK_DEBUG
#ifdef SK_BUILD_FOR_WIN
#include <windows.h>
inline int64_t sk_get_thread_id() { return GetCurrentThreadId(); }
#else
#include <pthread.h>
inline int64_t sk_get_thread_id() { return (int64_t)pthread_self(); }
#endif
#endif
typedef int64_t SkThreadID;
SkDEBUGCODE(static const SkThreadID kNoOwner = 0;)
struct SkBaseMutex {
void acquire() {
fSemaphore.wait();
SkDEBUGCODE(fOwner = sk_get_thread_id();)
}
void release() {
this->assertHeld();
SkDEBUGCODE(fOwner = kNoOwner;)
fSemaphore.signal();
}
void assertHeld() {
SkASSERT(fOwner == sk_get_thread_id());
}
SkBaseSemaphore fSemaphore;
SkDEBUGCODE(SkThreadID fOwner;)
};
// This needs to use subclassing instead of encapsulation to make SkAutoMutexAcquire to work.
class SkMutex : public SkBaseMutex {
public:
SkMutex () {
fSemaphore = SK_MUTEX_SEMAPHORE_INIT;
SkDEBUGCODE(fOwner = kNoOwner);
}
~SkMutex () { fSemaphore.deleteSemaphore(); }
SkMutex(const SkMutex&) = delete;
SkMutex& operator=(const SkMutex&) = delete;
};
template <typename Lock>
class SkAutoTAcquire : SkNoncopyable {
public:
explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) {
SkASSERT(fMutex != NULL);
mutex.acquire();
}
explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) {
if (mutex) {
mutex->acquire();
}
}
/** If the mutex has not been released, release it now. */
~SkAutoTAcquire() {
if (fMutex) {
fMutex->release();
}
}
/** If the mutex has not been released, release it now. */
void release() {
if (fMutex) {
fMutex->release();
fMutex = NULL;
}
}
/** Assert that we're holding the mutex. */
void assertHeld() {
SkASSERT(fMutex);
fMutex->assertHeld();
}
private:
Lock* fMutex;
};
typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
#endif//SkMutex_DEFINED
|