// Copyright 2021 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "dawn_platform/WorkerThread.h" #include #include #include #include "common/Assert.h" namespace { class AsyncWaitableEventImpl { public: AsyncWaitableEventImpl() : mIsComplete(false) { } void Wait() { std::unique_lock lock(mMutex); mCondition.wait(lock, [this] { return mIsComplete; }); } bool IsComplete() { std::lock_guard lock(mMutex); return mIsComplete; } void MarkAsComplete() { { std::lock_guard lock(mMutex); mIsComplete = true; } mCondition.notify_all(); } private: std::mutex mMutex; std::condition_variable mCondition; bool mIsComplete; }; class AsyncWaitableEvent final : public dawn_platform::WaitableEvent { public: explicit AsyncWaitableEvent() : mWaitableEventImpl(std::make_shared()) { } void Wait() override { mWaitableEventImpl->Wait(); } bool IsComplete() override { return mWaitableEventImpl->IsComplete(); } std::shared_ptr GetWaitableEventImpl() const { return mWaitableEventImpl; } private: std::shared_ptr mWaitableEventImpl; }; } // anonymous namespace namespace dawn_platform { std::unique_ptr AsyncWorkerThreadPool::PostWorkerTask( dawn_platform::PostWorkerTaskCallback callback, void* userdata) { std::unique_ptr waitableEvent = std::make_unique(); std::function doTask = [callback, userdata, waitableEventImpl = waitableEvent->GetWaitableEventImpl()]() { callback(userdata); waitableEventImpl->MarkAsComplete(); }; std::thread thread(doTask); thread.detach(); return waitableEvent; } } // namespace dawn_platform