1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 <memory>
#include <mutex>
#include <gtest/gtest.h>
template<class E>
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<long>& 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<CCriticalSection> tmplock(sec); // kick any memory syncs
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return false;
}
class AtomicGuard
{
std::atomic<long>* val;
public:
inline AtomicGuard(std::atomic<long>* val_) : val(val_) { if (val) ++(*val); }
inline ~AtomicGuard() { if (val) --(*val); }
};
class thread
{
std::unique_ptr<CThread> cthread;
public:
inline explicit thread(IRunnable& runnable)
: cthread(std::make_unique<CThread>(&runnable, "DumbThread"))
{
cthread->Create();
}
void join() { cthread->Join(std::chrono::milliseconds::max()); }
bool timed_join(std::chrono::milliseconds duration) { return cthread->Join(duration); }
};
|