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) {
214 std::unique_lock<std::recursive_mutex> lock(thread->m_threadMutex);
217 thread->m_threadId = std::this_thread::get_id();
218 std::stringstream ss;
219 ss << thread->m_threadId;
220 std::string
id = ss.str();
221 bool autodelete = thread->m_autoDelete;
224 (autodelete ?
"true" :
"false"));
226 thread->m_running =
true;
227 thread->m_startEvent.notify_one();
240 catch (
const std::exception& e)
249 promise.set_value(
true);
251 this, std::move(prom));
253 m_startEvent.wait(lock);
268 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
273 if (m_thread && !m_running)
274 m_startEvent.wait(lock);
277 m_stopEvent.notify_one();
279 std::thread* lthread = m_thread;
283 if (lthread->joinable())
287 m_threadId = std::thread::id();
309 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
310 m_stopEvent.wait_for(lock, std::chrono::milliseconds(milliseconds));
314 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
329 bool Join(
unsigned int milliseconds)
331 std::unique_lock<std::recursive_mutex> lock(m_threadMutex);
332 std::thread* lthread = m_thread;
333 if (lthread !=
nullptr)
339 m_threadMutex.unlock();
340 std::future_status stat = m_future.wait_for(std::chrono::milliseconds(milliseconds));
341 if (stat != std::future_status::ready)
343 m_threadMutex.lock();
347 if (lthread->joinable())
384 bool m_autoDelete =
false;
385 bool m_running =
false;
386 std::condition_variable_any m_stopEvent;
387 std::condition_variable_any m_startEvent;
388 std::recursive_mutex m_threadMutex;
389 std::thread::id m_threadId;
390 std::thread* m_thread =
nullptr;
391 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:164
@ ADDON_LOG_DEBUG
0 : To include debug information in the log file.
Definition: addon_base.h:151
void ATTRIBUTE_HIDDEN Log(const AddonLog loglevel, const char *format,...)
Add a message to Kodi's log.
Definition: AddonBase.h:749