2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-10-17 18:42:14 +02:00
|
|
|
|
|
|
|
#include "Thread.hxx"
|
2016-06-17 19:06:30 +02:00
|
|
|
#include "system/Error.hxx"
|
2013-10-17 18:42:14 +02:00
|
|
|
|
2014-03-01 09:19:32 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "java/Global.hxx"
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 21:55:02 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <synchapi.h> // for WaitForSingleObject()
|
|
|
|
#include <windef.h> // for HWND (needed by winbase.h)
|
|
|
|
#include <winbase.h> // for INFINITE
|
|
|
|
#endif
|
|
|
|
|
2017-02-10 22:41:29 +01:00
|
|
|
void
|
2017-02-10 22:41:11 +01:00
|
|
|
Thread::Start()
|
2013-10-17 18:42:14 +02:00
|
|
|
{
|
|
|
|
assert(!IsDefined());
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
handle = ::CreateThread(nullptr, 0, ThreadProc, this, 0, &id);
|
2016-06-17 19:06:30 +02:00
|
|
|
if (handle == nullptr)
|
|
|
|
throw MakeLastError("Failed to create thread");
|
2013-10-17 18:42:14 +02:00
|
|
|
#else
|
|
|
|
int e = pthread_create(&handle, nullptr, ThreadProc, this);
|
|
|
|
|
2017-12-22 10:37:07 +01:00
|
|
|
if (e != 0)
|
2016-06-17 19:06:30 +02:00
|
|
|
throw MakeErrno(e, "Failed to create thread");
|
2013-10-17 18:42:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-26 11:58:53 +01:00
|
|
|
Thread::Join() noexcept
|
2013-10-17 18:42:14 +02:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
assert(!IsInside());
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
::WaitForSingleObject(handle, INFINITE);
|
|
|
|
::CloseHandle(handle);
|
|
|
|
handle = nullptr;
|
|
|
|
#else
|
|
|
|
pthread_join(handle, nullptr);
|
2017-12-22 10:37:07 +01:00
|
|
|
handle = pthread_t();
|
2013-10-17 18:42:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:43:55 +01:00
|
|
|
inline void
|
2017-11-26 11:58:53 +01:00
|
|
|
Thread::Run() noexcept
|
2017-02-10 22:43:55 +01:00
|
|
|
{
|
2017-02-10 22:41:11 +01:00
|
|
|
f();
|
2017-02-10 22:43:55 +01:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
Java::DetachCurrentThread();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
|
|
|
|
DWORD WINAPI
|
2017-11-26 11:58:53 +01:00
|
|
|
Thread::ThreadProc(LPVOID ctx) noexcept
|
2013-10-17 18:42:14 +02:00
|
|
|
{
|
|
|
|
Thread &thread = *(Thread *)ctx;
|
|
|
|
|
2017-02-10 22:43:55 +01:00
|
|
|
thread.Run();
|
2013-10-17 18:42:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
void *
|
2017-11-26 11:58:53 +01:00
|
|
|
Thread::ThreadProc(void *ctx) noexcept
|
2013-10-17 18:42:14 +02:00
|
|
|
{
|
|
|
|
Thread &thread = *(Thread *)ctx;
|
|
|
|
|
2018-01-08 09:58:18 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
thread.inside_handle = pthread_self();
|
|
|
|
#endif
|
|
|
|
|
2017-02-10 22:43:55 +01:00
|
|
|
thread.Run();
|
2014-03-01 09:19:32 +01:00
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|