aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkSharedMutex.h
blob: f3430040e38b51ee05ddb9d2915db44a30ff6ec3 (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
/*
 * 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"
    
// This is a shared lock implementation similar to pthreads rwlocks. This 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.
#ifdef SK_DEBUG
    void assertHeld() const;
#else
    void assertHeld() const {}
#endif

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

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

    // Fail if shared lock not held.
#ifdef SK_DEBUG
    void assertHeldShared() const;
#else
    void assertHeldShared() const {}
#endif

private:
    SkAtomic<int32_t> fQueueCounts;
    SkSemaphore fSharedQueue;
    SkSemaphore fExclusiveQueue;
};


#endif // SkSharedLock_DEFINED