aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkSharedMutex.h
blob: 302940bbc402cf84002f8c740b395a3ff0b347c9 (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
/*
 * 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 SkSharedLock_DEFINED
#define SkSharedLock_DEFINED

#include "SkAtomics.h"
#include "SkSemaphore.h"
#include "SkTypes.h"

#ifdef SK_DEBUG
    #include "SkMutex.h"

    // On GCC 4.8, targeting ARMv7 with NEON, using libc++, we need to typedef float float32_t,
    // (or include <arm_neon.h> which does that) before #including <memory> here.
    // This makes no sense.  I'm not very interested in understanding why... this is an old,
    // bizarre platform configuration that we should just let die.
    #include <ciso646>  // Include something innocuous to define _LIBCPP_VERISON if it's libc++.
    #if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8 \
     && defined(SK_CPU_ARM32) && defined(SK_ARM_HAS_NEON) \
     && defined(_LIBCPP_VERSION)
        typedef float float32_t;
    #endif

    #include <memory>
#endif  // SK_DEBUG

// There are two shared lock implementations one debug the other is high performance. They implement
// an interface similar to pthread's rwlocks.
// This is a shared lock implementation similar to pthreads rwlocks. The high performance
// implementation is cribbed from Preshing's article:
// http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
//
// This lock does not obey strict queue ordering. It will always alternate between readers and
// a single writer.
class SkSharedMutex {
public:
    SkSharedMutex();
    ~SkSharedMutex();
    // Acquire lock for exclusive use.
    void acquire();

    // Release lock for exclusive use.
    void release();

    // Fail if exclusive is not held.
    void assertHeld() const;

    // Acquire lock for shared use.
    void acquireShared();

    // Release lock for shared use.
    void releaseShared();

    // Fail if shared lock not held.
    void assertHeldShared() const;

private:
#ifdef SK_DEBUG
    class ThreadIDSet;
    std::unique_ptr<ThreadIDSet> fCurrentShared;
    std::unique_ptr<ThreadIDSet> fWaitingExclusive;
    std::unique_ptr<ThreadIDSet> fWaitingShared;
    int fSharedQueueSelect{0};
    mutable SkMutex fMu;
    SkSemaphore fSharedQueue[2];
    SkSemaphore fExclusiveQueue;
#else
    SkAtomic<int32_t> fQueueCounts;
    SkSemaphore       fSharedQueue;
    SkSemaphore       fExclusiveQueue;
#endif  // SK_DEBUG
};

#ifndef SK_DEBUG
inline void SkSharedMutex::assertHeld() const {};
inline void SkSharedMutex::assertHeldShared() const {};
#endif  // SK_DEBUG

class SkAutoSharedMutexShared {
public:
    SkAutoSharedMutexShared(SkSharedMutex& lock) : fLock(lock) { lock.acquireShared(); }
    ~SkAutoSharedMutexShared() { fLock.releaseShared(); }
private:
    SkSharedMutex& fLock;
};

#define SkAutoSharedMutexShared(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexShared)

#endif // SkSharedLock_DEFINED