skia2/include/private/SkMutex.h
Mike Klein c0bd9f9fe5 rewrite includes to not need so much -Ifoo
Current strategy: everything from the top

Things to look at first are the manual changes:

   - added tools/rewrite_includes.py
   - removed -Idirectives from BUILD.gn
   - various compile.sh simplifications
   - tweak tools/embed_resources.py
   - update gn/find_headers.py to write paths from the top
   - update gn/gn_to_bp.py SkUserConfig.h layout
     so that #include "include/config/SkUserConfig.h" always
     gets the header we want.

No-Presubmit: true
Change-Id: I73a4b181654e0e38d229bc456c0d0854bae3363e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/209706
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
2019-04-24 16:27:11 +00:00

95 lines
2.1 KiB
C++

/*
* 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 SkMutex_DEFINED
#define SkMutex_DEFINED
#include "include/core/SkTypes.h"
#include "include/private/SkMacros.h"
#include "include/private/SkSemaphore.h"
#include "include/private/SkThreadID.h"
#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name;
class SkBaseMutex {
public:
constexpr SkBaseMutex() = default;
void acquire() {
fSemaphore.wait();
SkDEBUGCODE(fOwner = SkGetThreadID();)
}
void release() {
this->assertHeld();
SkDEBUGCODE(fOwner = kIllegalThreadID;)
fSemaphore.signal();
}
void assertHeld() {
SkASSERT(fOwner == SkGetThreadID());
}
protected:
SkBaseSemaphore fSemaphore{1};
SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};)
};
class SkMutex : public SkBaseMutex {
public:
using SkBaseMutex::SkBaseMutex;
~SkMutex() { fSemaphore.cleanup(); }
};
class SkAutoMutexAcquire {
public:
template <typename T>
SkAutoMutexAcquire(T* mutex) : fMutex(mutex) {
if (mutex) {
mutex->acquire();
}
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
}
template <typename T>
SkAutoMutexAcquire(T& mutex) : SkAutoMutexAcquire(&mutex) {}
~SkAutoMutexAcquire() { this->release(); }
void release() {
if (fMutex) {
fRelease(fMutex);
}
fMutex = nullptr;
}
private:
void* fMutex;
void (*fRelease)(void*);
};
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
// SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire.
// It assumes that there is a valid mutex, obviating the null check.
class SkAutoExclusive {
public:
template <typename T>
SkAutoExclusive(T& mutex) : fMutex(&mutex) {
mutex.acquire();
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
}
~SkAutoExclusive() { fRelease(fMutex); }
private:
void* fMutex;
void (*fRelease)(void*);
};
#define SkAutoExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoExclusive)
#endif//SkMutex_DEFINED