Merge pull request #26350 from jjd-uk/estuary_media_align
[xbmc.git] / xbmc / threads / test / TestHelpers.h
blob4d8752c682bc05c0e74def8b535fd50d97e6bda3
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "threads/Thread.h"
13 #include <memory>
14 #include <mutex>
16 #include <gtest/gtest.h>
18 template<class E>
19 inline static bool waitForWaiters(E& event, int numWaiters, std::chrono::milliseconds duration)
21 for (auto i = std::chrono::milliseconds::zero(); i < duration; i++)
23 if (event.getNumWaits() == numWaiters)
24 return true;
26 std::this_thread::sleep_for(std::chrono::milliseconds(1));
29 return false;
32 inline static bool waitForThread(std::atomic<long>& mutex,
33 int numWaiters,
34 std::chrono::milliseconds duration)
36 CCriticalSection sec;
37 for (auto i = std::chrono::milliseconds::zero(); i < duration; i++)
39 if (mutex == (long)numWaiters)
40 return true;
43 std::unique_lock<CCriticalSection> tmplock(sec); // kick any memory syncs
46 std::this_thread::sleep_for(std::chrono::milliseconds(1));
49 return false;
52 class AtomicGuard
54 std::atomic<long>* val;
55 public:
56 inline AtomicGuard(std::atomic<long>* val_) : val(val_) { if (val) ++(*val); }
57 inline ~AtomicGuard() { if (val) --(*val); }
60 class thread
62 std::unique_ptr<CThread> cthread;
64 public:
65 inline explicit thread(IRunnable& runnable)
66 : cthread(std::make_unique<CThread>(&runnable, "DumbThread"))
68 cthread->Create();
71 void join() { cthread->Join(std::chrono::milliseconds::max()); }
73 bool timed_join(std::chrono::milliseconds duration) { return cthread->Join(duration); }