aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-05-04 11:31:29 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-04 11:31:29 -0700
commit427c2819d9237d7d7729c59238036cfc73c072ea (patch)
tree90d1d7ebba7d16bfbe56a6f59c6cf135c7161acf /src
parent325474dd42db6d5a16cc4cf18f06dad4e0d60e9f (diff)
Modernize SkMutex and SkSemaphore.
- use <atomic> - fuse SkMutex and SkBaseMutex - fuse SkSemaphore and SkBaseSemaphore Still TODO: - replace SK_DECLARE_STATIC_MUTEX(name) with static SkMutex name I just didn't want to bother fixing all that up until I know this CL sticks. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1947153002 No public API changes. TBR=reed@google.com Review-Url: https://codereview.chromium.org/1947153002
Diffstat (limited to 'src')
-rw-r--r--src/core/SkSemaphore.cpp49
-rw-r--r--src/lazy/SkDiscardableMemoryPool.cpp13
-rw-r--r--src/lazy/SkDiscardableMemoryPool.h3
3 files changed, 18 insertions, 47 deletions
diff --git a/src/core/SkSemaphore.cpp b/src/core/SkSemaphore.cpp
index da422e282f..0646b152e8 100644
--- a/src/core/SkSemaphore.cpp
+++ b/src/core/SkSemaphore.cpp
@@ -9,7 +9,7 @@
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include <mach/mach.h>
- struct SkBaseSemaphore::OSSemaphore {
+ struct SkSemaphore::OSSemaphore {
semaphore_t fSemaphore;
OSSemaphore() {
@@ -21,7 +21,7 @@
void wait() { semaphore_wait(fSemaphore); }
};
#elif defined(SK_BUILD_FOR_WIN32)
- struct SkBaseSemaphore::OSSemaphore {
+ struct SkSemaphore::OSSemaphore {
HANDLE fSemaphore;
OSSemaphore() {
@@ -41,7 +41,7 @@
// It's important we test for Mach before this. This code will compile but not work there.
#include <errno.h>
#include <semaphore.h>
- struct SkBaseSemaphore::OSSemaphore {
+ struct SkSemaphore::OSSemaphore {
sem_t fSemaphore;
OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial count*/); }
@@ -57,43 +57,16 @@
///////////////////////////////////////////////////////////////////////////////
-void SkBaseSemaphore::signal(int n) {
- SkASSERT(n >= 0);
-
- // We only want to call the OS semaphore when our logical count crosses
- // from <= 0 to >0 (when we need to wake sleeping threads).
- //
- // This is easiest to think about with specific examples of prev and n.
- // If n == 5 and prev == -3, there are 3 threads sleeping and we signal
- // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
- //
- // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0,
- // so we don't call the OS semaphore, leaving the count at (prev + n).
- int prev = sk_atomic_fetch_add(&fCount, n, sk_memory_order_release);
- int toSignal = SkTMin(-prev, n);
- if (toSignal > 0) {
- this->osSignal(toSignal);
- }
+void SkSemaphore::osSignal(int n) {
+ fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
+ fOSSemaphore->signal(n);
}
-static SkBaseSemaphore::OSSemaphore* semaphore(SkBaseSemaphore* semaphore) {
- return semaphore->fOSSemaphore.get([](){ return new SkBaseSemaphore::OSSemaphore(); });
+void SkSemaphore::osWait() {
+ fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
+ fOSSemaphore->wait();
}
-void SkBaseSemaphore::osSignal(int n) { semaphore(this)->signal(n); }
-
-void SkBaseSemaphore::osWait() { semaphore(this)->wait(); }
-
-void SkBaseSemaphore::deleteSemaphore() {
- delete (OSSemaphore*) fOSSemaphore;
+SkSemaphore::~SkSemaphore() {
+ delete fOSSemaphore;
}
-
-///////////////////////////////////////////////////////////////////////////////
-
-SkSemaphore::SkSemaphore(){ fBaseSemaphore = {0, {0}}; }
-
-SkSemaphore::~SkSemaphore() { fBaseSemaphore.deleteSemaphore(); }
-
-void SkSemaphore::wait() { fBaseSemaphore.wait(); }
-
-void SkSemaphore::signal(int n) {fBaseSemaphore.signal(n); }
diff --git a/src/lazy/SkDiscardableMemoryPool.cpp b/src/lazy/SkDiscardableMemoryPool.cpp
index 1f3bcf93e9..2be4c755f1 100644
--- a/src/lazy/SkDiscardableMemoryPool.cpp
+++ b/src/lazy/SkDiscardableMemoryPool.cpp
@@ -29,7 +29,7 @@ public:
/**
* Without mutex, will be not be thread safe.
*/
- DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = nullptr);
+ DiscardableMemoryPool(size_t budget, SkMutex* mutex = nullptr);
virtual ~DiscardableMemoryPool();
SkDiscardableMemory* create(size_t bytes) override;
@@ -52,9 +52,9 @@ public:
#endif // SK_LAZY_CACHE_STATS
private:
- SkBaseMutex* fMutex;
- size_t fBudget;
- size_t fUsed;
+ SkMutex* fMutex;
+ size_t fBudget;
+ size_t fUsed;
SkTInternalLList<PoolDiscardableMemory> fList;
/** Function called to free memory if needed */
@@ -128,8 +128,7 @@ void PoolDiscardableMemory::unlock() {
////////////////////////////////////////////////////////////////////////////////
-DiscardableMemoryPool::DiscardableMemoryPool(size_t budget,
- SkBaseMutex* mutex)
+DiscardableMemoryPool::DiscardableMemoryPool(size_t budget, SkMutex* mutex)
: fMutex(mutex)
, fBudget(budget)
, fUsed(0) {
@@ -241,7 +240,7 @@ void DiscardableMemoryPool::dumpPool() {
} // namespace
-SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) {
+SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkMutex* mutex) {
return new DiscardableMemoryPool(size, mutex);
}
diff --git a/src/lazy/SkDiscardableMemoryPool.h b/src/lazy/SkDiscardableMemoryPool.h
index 92ba48bcb4..ad8d796f66 100644
--- a/src/lazy/SkDiscardableMemoryPool.h
+++ b/src/lazy/SkDiscardableMemoryPool.h
@@ -52,8 +52,7 @@ public:
* the pool works.
* Without mutex, will be not be thread safe.
*/
- static SkDiscardableMemoryPool* Create(
- size_t size, SkBaseMutex* mutex = nullptr);
+ static SkDiscardableMemoryPool* Create(size_t size, SkMutex* mutex = nullptr);
};
/**