From c04dcc2e7d834218ef2d4194331e383402495ae1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 20:07:22 +0200 Subject: Adding upstream version 2:20.4+dfsg. Signed-off-by: Daniel Baumann --- xbmc/threads/test/TestHelpers.h | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 xbmc/threads/test/TestHelpers.h (limited to 'xbmc/threads/test/TestHelpers.h') diff --git a/xbmc/threads/test/TestHelpers.h b/xbmc/threads/test/TestHelpers.h new file mode 100644 index 0000000..4d8752c --- /dev/null +++ b/xbmc/threads/test/TestHelpers.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#pragma once + +#include "threads/Thread.h" + +#include +#include + +#include + +template +inline static bool waitForWaiters(E& event, int numWaiters, std::chrono::milliseconds duration) +{ + for (auto i = std::chrono::milliseconds::zero(); i < duration; i++) + { + if (event.getNumWaits() == numWaiters) + return true; + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + return false; +} + +inline static bool waitForThread(std::atomic& mutex, + int numWaiters, + std::chrono::milliseconds duration) +{ + CCriticalSection sec; + for (auto i = std::chrono::milliseconds::zero(); i < duration; i++) + { + if (mutex == (long)numWaiters) + return true; + + { + std::unique_lock tmplock(sec); // kick any memory syncs + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + return false; +} + +class AtomicGuard +{ + std::atomic* val; +public: + inline AtomicGuard(std::atomic* val_) : val(val_) { if (val) ++(*val); } + inline ~AtomicGuard() { if (val) --(*val); } +}; + +class thread +{ + std::unique_ptr cthread; + +public: + inline explicit thread(IRunnable& runnable) + : cthread(std::make_unique(&runnable, "DumbThread")) + { + cthread->Create(); + } + + void join() { cthread->Join(std::chrono::milliseconds::max()); } + + bool timed_join(std::chrono::milliseconds duration) { return cthread->Join(duration); } +}; + -- cgit v1.2.3