2013-10-17 18:42:14 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-10-17 18:42:14 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_THREAD_HXX
|
|
|
|
#define MPD_THREAD_HXX
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-02-10 22:41:11 +01:00
|
|
|
#include "util/BindMethod.hxx"
|
2013-10-17 18:42:14 +02:00
|
|
|
#include "Compiler.h"
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
class Thread {
|
2017-02-10 22:41:11 +01:00
|
|
|
typedef BoundMethod<void()> Function;
|
|
|
|
const Function f;
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2016-06-17 19:06:45 +02:00
|
|
|
HANDLE handle = nullptr;
|
2013-10-17 18:42:14 +02:00
|
|
|
DWORD id;
|
|
|
|
#else
|
|
|
|
pthread_t handle;
|
2016-06-17 19:06:45 +02:00
|
|
|
bool defined = false;
|
2013-10-17 18:42:14 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/**
|
|
|
|
* The thread is currently being created. This is a workaround for
|
|
|
|
* IsInside(), which may return false until pthread_create() has
|
|
|
|
* initialised the #handle.
|
|
|
|
*/
|
2016-06-17 19:06:45 +02:00
|
|
|
bool creating = false;
|
2013-10-17 18:42:14 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2017-11-26 11:58:53 +01:00
|
|
|
explicit Thread(Function _f) noexcept:f(_f) {}
|
2013-10-17 18:42:14 +02:00
|
|
|
|
|
|
|
Thread(const Thread &) = delete;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-11-26 11:58:53 +01:00
|
|
|
~Thread() noexcept {
|
2013-10-17 18:42:14 +02:00
|
|
|
/* all Thread objects must be destructed manually by calling
|
|
|
|
Join(), to clean up */
|
|
|
|
assert(!IsDefined());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-26 11:58:53 +01:00
|
|
|
bool IsDefined() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
return handle != nullptr;
|
|
|
|
#else
|
|
|
|
return defined;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this thread is the current thread.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsInside() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-10-17 18:42:14 +02:00
|
|
|
return GetCurrentThreadId() == id;
|
|
|
|
#else
|
|
|
|
#ifdef NDEBUG
|
|
|
|
constexpr bool creating = false;
|
|
|
|
#endif
|
|
|
|
return IsDefined() && (creating ||
|
|
|
|
pthread_equal(pthread_self(), handle));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
void Start();
|
2017-11-26 11:58:53 +01:00
|
|
|
void Join() noexcept;
|
2013-10-17 18:42:14 +02:00
|
|
|
|
|
|
|
private:
|
2017-11-26 11:58:53 +01:00
|
|
|
void Run() noexcept;
|
2017-02-10 22:43:55 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2017-11-26 11:58:53 +01:00
|
|
|
static DWORD WINAPI ThreadProc(LPVOID ctx) noexcept;
|
2013-10-17 18:42:14 +02:00
|
|
|
#else
|
2017-11-26 11:58:53 +01:00
|
|
|
static void *ThreadProc(void *ctx) noexcept;
|
2013-10-17 18:42:14 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|