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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
* Copyright (c) 2002 Frodo
* Portions Copyright (c) by the authors of ffmpeg and xvid
* Copyright (C) 2002-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.
*/
#include "Thread.h"
#include "IRunnable.h"
#include "commons/Exception.h"
#include "threads/IThreadImpl.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include <atomic>
#include <inttypes.h>
#include <iostream>
#include <mutex>
#include <stdlib.h>
static thread_local CThread* currentThread;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CThread::CThread(const char* ThreadName)
:
m_bStop(false), m_StopEvent(true, true), m_StartEvent(true, true), m_pRunnable(nullptr)
{
if (ThreadName)
m_ThreadName = ThreadName;
}
CThread::CThread(IRunnable* pRunnable, const char* ThreadName)
:
m_bStop(false), m_StopEvent(true, true), m_StartEvent(true, true), m_pRunnable(pRunnable)
{
if (ThreadName)
m_ThreadName = ThreadName;
}
CThread::~CThread()
{
StopThread();
if (m_thread != nullptr)
{
m_thread->detach();
delete m_thread;
}
}
void CThread::Create(bool bAutoDelete)
{
if (m_thread != nullptr)
{
// if the thread exited on it's own, without a call to StopThread, then we can get here
// incorrectly. We should be able to determine this by checking the promise.
std::future_status stat = m_future.wait_for(std::chrono::milliseconds(0));
// a status of 'ready' means the future contains the value so the thread has exited
// since the thread can't exit without setting the future.
if (stat == std::future_status::ready) // this is an indication the thread has exited.
StopThread(true); // so let's just clean up
else
{ // otherwise we have a problem.
CLog::Log(LOGERROR, "{} - fatal error creating thread {} - old thread id not null",
__FUNCTION__, m_ThreadName);
exit(1);
}
}
m_bAutoDelete = bAutoDelete;
m_bStop = false;
m_StopEvent.Reset();
m_StartEvent.Reset();
// lock?
//std::unique_lock<CCriticalSection> l(m_CriticalSection);
std::promise<bool> prom;
m_future = prom.get_future();
{
// The std::thread internals must be set prior to the lambda doing
// any work. This will cause the lambda to wait until m_thread
// is fully initialized. Interestingly, using a std::atomic doesn't
// have the appropriate memory barrier behavior to accomplish the
// same thing so a full system mutex needs to be used.
std::unique_lock<CCriticalSection> blockLambdaTillDone(m_CriticalSection);
m_thread = new std::thread([](CThread* pThread, std::promise<bool> promise)
{
try
{
{
// Wait for the pThread->m_thread internals to be set. Otherwise we could
// get to a place where we're reading, say, the thread id inside this
// lambda's call stack prior to the thread that kicked off this lambda
// having it set. Once this lock is released, the CThread::Create function
// that kicked this off is done so everything should be set.
std::unique_lock<CCriticalSection> waitForThreadInternalsToBeSet(
pThread->m_CriticalSection);
}
// This is used in various helper methods like GetCurrentThread so it needs
// to be set before anything else is done.
currentThread = pThread;
std::string name;
bool autodelete;
if (pThread == nullptr)
{
CLog::Log(LOGERROR, "{}, sanity failed. thread is NULL.", __FUNCTION__);
promise.set_value(false);
return;
}
name = pThread->m_ThreadName;
std::stringstream ss;
ss << std::this_thread::get_id();
std::string id = ss.str();
autodelete = pThread->m_bAutoDelete;
pThread->m_impl = IThreadImpl::CreateThreadImpl(pThread->m_thread->native_handle());
pThread->m_impl->SetThreadInfo(name);
CLog::Log(LOGDEBUG, "Thread {} start, auto delete: {}", name,
(autodelete ? "true" : "false"));
pThread->m_StartEvent.Set();
pThread->Action();
if (autodelete)
{
CLog::Log(LOGDEBUG, "Thread {} {} terminating (autodelete)", name, id);
delete pThread;
pThread = NULL;
}
else
CLog::Log(LOGDEBUG, "Thread {} {} terminating", name, id);
}
catch (const std::exception& e)
{
CLog::Log(LOGDEBUG, "Thread Terminating with Exception: {}", e.what());
}
catch (...)
{
CLog::Log(LOGDEBUG,"Thread Terminating with Exception");
}
promise.set_value(true);
}, this, std::move(prom));
} // let the lambda proceed
m_StartEvent.Wait(); // wait for the thread just spawned to set its internals
}
bool CThread::IsRunning() const
{
if (m_thread != nullptr) {
// it's possible that the thread exited on it's own without a call to StopThread. If so then
// the promise should be fulfilled.
std::future_status stat = m_future.wait_for(std::chrono::milliseconds(0));
// a status of 'ready' means the future contains the value so the thread has exited
// since the thread can't exit without setting the future.
if (stat == std::future_status::ready) // this is an indication the thread has exited.
return false;
return true; // otherwise the thread is still active.
} else
return false;
}
bool CThread::SetPriority(const ThreadPriority& priority)
{
return m_impl->SetPriority(priority);
}
bool CThread::IsAutoDelete() const
{
return m_bAutoDelete;
}
void CThread::StopThread(bool bWait /*= true*/)
{
m_StartEvent.Wait();
m_bStop = true;
m_StopEvent.Set();
std::unique_lock<CCriticalSection> lock(m_CriticalSection);
std::thread* lthread = m_thread;
if (lthread != nullptr && bWait && !IsCurrentThread())
{
lock.unlock();
if (!Join(std::chrono::milliseconds::max())) // eh?
lthread->join();
m_thread = nullptr;
}
}
void CThread::Process()
{
if (m_pRunnable)
m_pRunnable->Run();
}
bool CThread::IsCurrentThread() const
{
CThread* pThread = currentThread;
if (pThread != nullptr)
return pThread == this;
else
return false;
}
CThread* CThread::GetCurrentThread()
{
return currentThread;
}
bool CThread::Join(std::chrono::milliseconds duration)
{
std::unique_lock<CCriticalSection> l(m_CriticalSection);
std::thread* lthread = m_thread;
if (lthread != nullptr)
{
if (IsCurrentThread())
return false;
{
CSingleExit exit(m_CriticalSection); // don't hold the thread lock while we're waiting
std::future_status stat = m_future.wait_for(duration);
if (stat != std::future_status::ready)
return false;
}
// it's possible it's already joined since we released the lock above.
if (lthread->joinable())
m_thread->join();
return true;
}
else
return false;
}
void CThread::Action()
{
try
{
OnStartup();
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("OnStartup");
if (IsAutoDelete())
return;
}
try
{
Process();
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("Process");
}
try
{
OnExit();
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("OnExit");
}
}
|