blob: a020f9e1be94b3fd5fc3b2625609069320b75602 [file] [log] [blame]
herbc782b2a2015-06-29 13:46:55 -07001/*
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 SkSharedLock_DEFINED
9#define SkSharedLock_DEFINED
10
11#include "SkAtomics.h"
12#include "SkSemaphore.h"
13#include "SkTypes.h"
14
15// This is a shared lock implementation similar to pthreads rwlocks. This implementation is
16// cribbed from Preshing's article:
17// http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
18//
19// This lock does not obey strict queue ordering. It will always alternate between readers and
20// a single writer.
21class SkSharedMutex {
22public:
23 SkSharedMutex();
24
25 // Acquire lock for exclusive use.
26 void acquire();
27
28 // Release lock for exclusive use.
29 void release();
30
31 // Acquire lock for shared use.
32 void acquireShared();
33
34 // Release lock for shared use.
35 void releaseShared();
36
37private:
38 SkAtomic<int32_t> fQueueCounts;
39 SkSemaphore fSharedQueue;
40 SkSemaphore fExclusiveQueue;
41};
42
43#endif // SkSharedLock_DEFINED