13#include "../General.h"
16#include <condition_variable>
107 if (m_thread !=
nullptr)
145 if (m_thread !=
nullptr)
149 std::future_status stat = m_future.wait_for(std::chrono::milliseconds(0));
152 if (stat == std::future_status::ready)
172 if (m_thread !=
nullptr)
176 std::future_status stat = m_future.wait_for(std::chrono::milliseconds(0));
179 if (stat == std::future_status::ready)
189 m_autoDelete = autoDelete;
191 m_startEvent.notify_all();
192 m_stopEvent.notify_all();
194 std::promise<bool> prom;
195 m_future = prom.get_future();
203 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
204 m_thread =
new std::thread(
205 [](
CThread* thread, std::promise<bool> promise)
215 std::unique_lock<std::recursive_mutex> lock(thread->m_threadMutex);
218 thread->m_threadId = std::this_thread::get_id();
219 std::stringstream ss;
220 ss << thread->m_threadId;
221 std::string
id = ss.str();
222 bool autodelete = thread->m_autoDelete;
225 (autodelete ?
"true" :
"false"));
227 thread->m_running =
true;
228 thread->m_startEvent.notify_one();
241 catch (
const std::exception& e)
250 promise.set_value(
true);
252 this, std::move(prom));
254 m_startEvent.wait(lock);
269 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
274 if (m_thread && !m_running)
275 m_startEvent.wait(lock);
278 m_stopEvent.notify_one();
280 std::thread* lthread = m_thread;
284 if (lthread->joinable())
288 m_threadId = std::thread::id();
310 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
311 m_stopEvent.wait_for(lock, std::chrono::milliseconds(milliseconds));
315 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
330 bool Join(
unsigned int milliseconds)
332 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
333 std::thread* lthread = m_thread;
334 if (lthread !=
nullptr)
340 m_threadMutex.unlock();
341 std::future_status stat = m_future.wait_for(std::chrono::milliseconds(milliseconds));
342 if (stat != std::future_status::ready)
344 m_threadMutex.lock();
348 if (lthread->joinable())
385 bool m_autoDelete =
false;
386 bool m_running =
false;
387 std::condition_variable_any m_stopEvent;
388 std::condition_variable_any m_startEvent;
389 std::recursive_mutex m_threadMutex;
390 std::thread::id m_threadId;
391 std::thread* m_thread =
nullptr;
392 std::future<bool> m_future;
@ ADDON_LOG_FATAL
4 : To notify fatal unrecoverable errors, which can may also indicate upcoming crashes.
Definition addon_base.h:197
@ ADDON_LOG_DEBUG
0 : To include debug information in the log file.
Definition addon_base.h:184
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1938