Thread.hxx 2.62 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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

23
#include "util/BindMethod.hxx"
24

25 26
#include <cassert>

27
#ifdef _WIN32
28
#include <processthreadsapi.h>
29 30 31 32 33
#else
#include <pthread.h>
#endif

class Thread {
Max Kellermann's avatar
Max Kellermann committed
34
	typedef BoundMethod<void() noexcept> Function;
35 36
	const Function f;

37
#ifdef _WIN32
38
	HANDLE handle = nullptr;
39 40
	DWORD id;
#else
41
	pthread_t handle = pthread_t();
42 43 44 45 46 47 48 49 50 51

#ifndef NDEBUG
	/**
	 * This handle is only used by IsInside(), and is set by the
	 * thread function.  Since #handle is set by pthread_create()
	 * which is racy, we need this attribute for early checks
	 * inside the thread function.
	 */
	pthread_t inside_handle = pthread_t();
#endif
52 53 54
#endif

public:
55
	explicit Thread(Function _f) noexcept:f(_f) {}
56 57

	Thread(const Thread &) = delete;
58
	Thread &operator=(const Thread &) = delete;
59 60

#ifndef NDEBUG
61
	~Thread() noexcept {
62 63 64 65 66 67
		/* all Thread objects must be destructed manually by calling
		   Join(), to clean up */
		assert(!IsDefined());
	}
#endif

68
	bool IsDefined() const noexcept {
69
#ifdef _WIN32
70 71
		return handle != nullptr;
#else
72
		return handle != pthread_t();
73
#endif
74
	}
75

76
#ifndef NDEBUG
77 78 79
	/**
	 * Check if this thread is the current thread.
	 */
80
	[[gnu::pure]]
81
	bool IsInside() const noexcept {
82
#ifdef _WIN32
83 84
		return GetCurrentThreadId() == id;
#else
85 86 87 88 89 90
		/* note: not using pthread_equal() because that
		   function "is undefined if either thread ID is not
		   valid so we can't safely use it on
		   default-constructed values" (comment from
		   libstdc++) - and if both libstdc++ and libc++ get
		   away with this, we can do it as well */
91
		return pthread_self() == inside_handle;
92 93
#endif
	}
94
#endif
95

96 97 98 99 100
	/**
	 * Start the thread.
	 *
	 * Throws on error.
	 */
101
	void Start();
102

103
	void Join() noexcept;
104 105

private:
106
	void Run() noexcept;
107

108
#ifdef _WIN32
109
	static DWORD WINAPI ThreadProc(LPVOID ctx) noexcept;
110
#else
111
	static void *ThreadProc(void *ctx) noexcept;
112 113 114 115 116
#endif

};

#endif