1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef SkMutex_DEFINED 9cb93a386Sopenharmony_ci#define SkMutex_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "include/core/SkTypes.h" 12cb93a386Sopenharmony_ci#include "include/private/SkMacros.h" 13cb93a386Sopenharmony_ci#include "include/private/SkSemaphore.h" 14cb93a386Sopenharmony_ci#include "include/private/SkThreadAnnotations.h" 15cb93a386Sopenharmony_ci#include "include/private/SkThreadID.h" 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ciclass SK_CAPABILITY("mutex") SkMutex { 18cb93a386Sopenharmony_cipublic: 19cb93a386Sopenharmony_ci constexpr SkMutex() = default; 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ci void acquire() SK_ACQUIRE() { 22cb93a386Sopenharmony_ci fSemaphore.wait(); 23cb93a386Sopenharmony_ci SkDEBUGCODE(fOwner = SkGetThreadID();) 24cb93a386Sopenharmony_ci } 25cb93a386Sopenharmony_ci 26cb93a386Sopenharmony_ci void release() SK_RELEASE_CAPABILITY() { 27cb93a386Sopenharmony_ci this->assertHeld(); 28cb93a386Sopenharmony_ci SkDEBUGCODE(fOwner = kIllegalThreadID;) 29cb93a386Sopenharmony_ci fSemaphore.signal(); 30cb93a386Sopenharmony_ci } 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ci void assertHeld() SK_ASSERT_CAPABILITY(this) { 33cb93a386Sopenharmony_ci SkASSERT(fOwner == SkGetThreadID()); 34cb93a386Sopenharmony_ci } 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ciprivate: 37cb93a386Sopenharmony_ci SkSemaphore fSemaphore{1}; 38cb93a386Sopenharmony_ci SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) 39cb93a386Sopenharmony_ci}; 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ciclass SK_SCOPED_CAPABILITY SkAutoMutexExclusive { 42cb93a386Sopenharmony_cipublic: 43cb93a386Sopenharmony_ci SkAutoMutexExclusive(SkMutex& mutex) SK_ACQUIRE(mutex) : fMutex(mutex) { fMutex.acquire(); } 44cb93a386Sopenharmony_ci ~SkAutoMutexExclusive() SK_RELEASE_CAPABILITY() { fMutex.release(); } 45cb93a386Sopenharmony_ci 46cb93a386Sopenharmony_ci SkAutoMutexExclusive(const SkAutoMutexExclusive&) = delete; 47cb93a386Sopenharmony_ci SkAutoMutexExclusive(SkAutoMutexExclusive&&) = delete; 48cb93a386Sopenharmony_ci 49cb93a386Sopenharmony_ci SkAutoMutexExclusive& operator=(const SkAutoMutexExclusive&) = delete; 50cb93a386Sopenharmony_ci SkAutoMutexExclusive& operator=(SkAutoMutexExclusive&&) = delete; 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ciprivate: 53cb93a386Sopenharmony_ci SkMutex& fMutex; 54cb93a386Sopenharmony_ci}; 55cb93a386Sopenharmony_ci 56cb93a386Sopenharmony_ci#endif // SkMutex_DEFINED 57