From 4eeaa07477d84698785545b4429c79ca10e16036 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 17 Aug 2016 14:28:23 -0700 Subject: [PATCH] flow::Semaphore for simple platform specific unnamed semaphores. (#2937) --- flow/BUILD.gn | 1 + synchronization/BUILD.gn | 14 ++++ synchronization/semaphore.cc | 121 +++++++++++++++++++++++++++++++++++ synchronization/semaphore.h | 39 +++++++++++ 4 files changed, 175 insertions(+) create mode 100644 synchronization/BUILD.gn create mode 100644 synchronization/semaphore.cc create mode 100644 synchronization/semaphore.h diff --git a/flow/BUILD.gn b/flow/BUILD.gn index 1100c403a6058..3bcb43e2f8bda 100644 --- a/flow/BUILD.gn +++ b/flow/BUILD.gn @@ -46,6 +46,7 @@ source_set("flow") { deps = [ "//flutter/glue", "//flutter/skia", + "//flutter/synchronization", "//lib/ftl", "//mojo/services/gfx/composition/interfaces", ] diff --git a/synchronization/BUILD.gn b/synchronization/BUILD.gn new file mode 100644 index 0000000000000..fa027d887c239 --- /dev/null +++ b/synchronization/BUILD.gn @@ -0,0 +1,14 @@ +# Copyright 2016 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +source_set("synchronization") { + sources = [ + "semaphore.cc", + "semaphore.h", + ] + + deps = [ + "//lib/ftl", + ] +} diff --git a/synchronization/semaphore.cc b/synchronization/semaphore.cc new file mode 100644 index 0000000000000..a863a07285bd0 --- /dev/null +++ b/synchronization/semaphore.cc @@ -0,0 +1,121 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/synchronization/semaphore.h" +#include "lib/ftl/build_config.h" +#include "lib/ftl/logging.h" + +#if OS_MACOSX + +#include + +namespace flutter { + +class PlatformSemaphore { + public: + explicit PlatformSemaphore(uint32_t count) + : _sem(dispatch_semaphore_create(count)) {} + + ~PlatformSemaphore() { + if (_sem != nullptr) { + dispatch_release(reinterpret_cast(_sem)); + _sem = nullptr; + } + } + + bool IsValid() const { return _sem != nullptr; } + + bool TryWait() { + if (_sem == nullptr) { + return false; + } + + return dispatch_semaphore_wait(_sem, DISPATCH_TIME_NOW) == 0; + } + + void Signal() { + if (_sem != nullptr) { + dispatch_semaphore_signal(_sem); + } + } + + private: + dispatch_semaphore_t _sem; + + FTL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore); +}; + +} // namespace flutter + +#else // OS_MACOSX + +#include +#include "lib/ftl/files/eintr_wrapper.h" + +namespace flutter { + +class PlatformSemaphore { + public: + explicit PlatformSemaphore(uint32_t count) + : valid_(::sem_init(&sem_, 0 /* not shared */, count) == 0) {} + + ~PlatformSemaphore() { + if (valid_) { + int result = ::sem_destroy(&sem_); + // Can only be EINVAL which should not be possible since we checked for + // validity. + FTL_DCHECK(result == 0); + } + } + + bool IsValid() const { return valid_; } + + bool TryWait() { + if (!valid_) { + return false; + } + + return HANDLE_EINTR(::sem_trywait(&sem_)) == 0; + } + + void Signal() { + if (!valid_) { + return; + } + + ::sem_post(&sem_); + + return; + } + + private: + bool valid_; + sem_t sem_; + + FTL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore); +}; + +} // namespace flutter + +#endif + +namespace flutter { + +Semaphore::Semaphore(uint32_t count) : _impl(new PlatformSemaphore(count)) {} + +Semaphore::~Semaphore() = default; + +bool Semaphore::IsValid() const { + return _impl->IsValid(); +} + +bool Semaphore::TryWait() { + return _impl->TryWait(); +} + +void Semaphore::Signal() { + return _impl->Signal(); +} + +} // namespace flutter diff --git a/synchronization/semaphore.h b/synchronization/semaphore.h new file mode 100644 index 0000000000000..41e7355da9e0d --- /dev/null +++ b/synchronization/semaphore.h @@ -0,0 +1,39 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNCHRONIZATION_SEMAPHORE_H_ +#define SYNCHRONIZATION_SEMAPHORE_H_ + +#include + +#include "lib/ftl/macros.h" +#include "lib/ftl/time/time_delta.h" +#include "lib/ftl/compiler_specific.h" + +namespace flutter { + +class PlatformSemaphore; + +class Semaphore { + public: + explicit Semaphore(uint32_t count); + + ~Semaphore(); + + bool IsValid() const; + + FTL_WARN_UNUSED_RESULT + bool TryWait(); + + void Signal(); + + private: + std::unique_ptr _impl; + + FTL_DISALLOW_COPY_AND_ASSIGN(Semaphore); +}; + +} // namespace flutter + +#endif // SYNCHRONIZATION_SEMAPHORE_H_