diff options
author | herb <herb@google.com> | 2015-09-29 11:47:45 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-29 11:47:45 -0700 |
commit | 62a69c26b3a34c259918d6c97b4dea76b6285b67 (patch) | |
tree | 833cdd358618557089590ffc3beb15209d7fa3db /include/private/SkSpinlock.h | |
parent | 05302f8f24cf0254e1fa713fbfc766387505e511 (diff) |
Move Mutexy things to private.
There is no API change.
TBR=reed@google.com
BUG=skia:
Review URL: https://codereview.chromium.org/1368333004
Diffstat (limited to 'include/private/SkSpinlock.h')
-rw-r--r-- | include/private/SkSpinlock.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/include/private/SkSpinlock.h b/include/private/SkSpinlock.h new file mode 100644 index 0000000000..ef52ab013a --- /dev/null +++ b/include/private/SkSpinlock.h @@ -0,0 +1,44 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +// This file is not part of the public Skia API. + +#ifndef SkSpinlock_DEFINED +#define SkSpinlock_DEFINED + +#include "../private/SkAtomics.h" + +#define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name + +// This class has no constructor and must be zero-initialized (the macro above does this). +class SK_API SkPODSpinlock { +public: + void acquire() { + // To act as a mutex, we need an acquire barrier if we take the lock. + if (sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { + // Lock was contended. Fall back to an out-of-line spin loop. + this->contendedAcquire(); + } + } + + void release() { + // To act as a mutex, we need a release barrier. + sk_atomic_store(&fLocked, false, sk_memory_order_release); + } + +private: + void contendedAcquire(); + bool fLocked; +}; + +// For non-global-static use cases, this is normally what you want. +class SkSpinlock : public SkPODSpinlock { +public: + SkSpinlock() { this->release(); } +}; + +#endif//SkSpinlock_DEFINED |