/* * Copyright 2015 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "../private/SkSemaphore.h" #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) #include struct SkSemaphore::OSSemaphore { semaphore_t fSemaphore; OSSemaphore() { semaphore_create(mach_task_self(), &fSemaphore, SYNC_POLICY_LIFO, 0/*initial count*/); } ~OSSemaphore() { semaphore_destroy(mach_task_self(), fSemaphore); } void signal(int n) { while (n --> 0) { semaphore_signal(fSemaphore); } } void wait() { semaphore_wait(fSemaphore); } }; #elif defined(SK_BUILD_FOR_WIN32) struct SkSemaphore::OSSemaphore { HANDLE fSemaphore; OSSemaphore() { fSemaphore = CreateSemaphore(nullptr /*security attributes, optional*/, 0 /*initial count*/, MAXLONG /*max count*/, nullptr /*name, optional*/); } ~OSSemaphore() { CloseHandle(fSemaphore); } void signal(int n) { ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, optional*/); } void wait() { WaitForSingleObject(fSemaphore, INFINITE/*timeout in ms*/); } }; #else // It's important we test for Mach before this. This code will compile but not work there. #include #include struct SkSemaphore::OSSemaphore { sem_t fSemaphore; OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial count*/); } ~OSSemaphore() { sem_destroy(&fSemaphore); } void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } } void wait() { // Try until we're not interrupted. while(sem_wait(&fSemaphore) == -1 && errno == EINTR); } }; #endif /////////////////////////////////////////////////////////////////////////////// void SkSemaphore::osSignal(int n) { fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); fOSSemaphore->signal(n); } void SkSemaphore::osWait() { fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); fOSSemaphore->wait(); } SkSemaphore::~SkSemaphore() { delete fOSSemaphore; }