/src/skia/include/private/base/SingleOwner.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016 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 skgpu_SingleOwner_DEFINED |
9 | | #define skgpu_SingleOwner_DEFINED |
10 | | |
11 | | #include "include/private/base/SkDebug.h" // IWYU pragma: keep |
12 | | |
13 | | #if defined(SK_DEBUG) |
14 | | #include "include/private/base/SkAssert.h" |
15 | | #include "include/private/base/SkMutex.h" |
16 | | #include "include/private/base/SkThreadAnnotations.h" |
17 | | #include "include/private/base/SkThreadID.h" |
18 | | |
19 | | #endif |
20 | | |
21 | | namespace skgpu { |
22 | | |
23 | | #if defined(SK_DEBUG) |
24 | | |
25 | | #define SKGPU_ASSERT_SINGLE_OWNER(obj) \ |
26 | 711k | skgpu::SingleOwner::AutoEnforce debug_SingleOwner(obj, __FILE__, __LINE__); |
27 | | |
28 | | // This is a debug tool to verify an object is only being used from one thread at a time. |
29 | | class SingleOwner { |
30 | | public: |
31 | 0 | SingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {} |
32 | | |
33 | | struct AutoEnforce { |
34 | | AutoEnforce(SingleOwner* so, const char* file, int line) |
35 | 0 | : fFile(file), fLine(line), fSO(so) { |
36 | 0 | fSO->enter(file, line); |
37 | 0 | } |
38 | 0 | ~AutoEnforce() { fSO->exit(fFile, fLine); } |
39 | | |
40 | | const char* fFile; |
41 | | int fLine; |
42 | | SingleOwner* fSO; |
43 | | }; |
44 | | |
45 | | private: |
46 | 0 | void enter(const char* file, int line) { |
47 | 0 | SkAutoMutexExclusive lock(fMutex); |
48 | 0 | SkThreadID self = SkGetThreadID(); |
49 | 0 | SkASSERTF(fOwner == self || fOwner == kIllegalThreadID, "%s:%d Single owner failure.", |
50 | 0 | file, line); |
51 | 0 | fReentranceCount++; |
52 | 0 | fOwner = self; |
53 | 0 | } |
54 | | |
55 | 0 | void exit(const char* file, int line) { |
56 | 0 | SkAutoMutexExclusive lock(fMutex); |
57 | 0 | SkASSERTF(fOwner == SkGetThreadID(), "%s:%d Single owner failure.", file, line); |
58 | 0 | fReentranceCount--; |
59 | 0 | if (fReentranceCount == 0) { |
60 | 0 | fOwner = kIllegalThreadID; |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | SkMutex fMutex; |
65 | | SkThreadID fOwner SK_GUARDED_BY(fMutex); |
66 | | int fReentranceCount SK_GUARDED_BY(fMutex); |
67 | | }; |
68 | | #else |
69 | | #define SKGPU_ASSERT_SINGLE_OWNER(obj) |
70 | | class SingleOwner {}; // Provide a no-op implementation so we can pass pointers to constructors |
71 | | #endif |
72 | | |
73 | | } // namespace skgpu |
74 | | |
75 | | #endif |