Thread.hxx 2.62 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "util/Compiler.h"
25

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

#include <assert.h>

class Thread {
35 36 37
	typedef BoundMethod<void()> Function;
	const Function f;

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

#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
53 54 55
#endif

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

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

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

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

77
#ifndef NDEBUG
78 79 80 81
	/**
	 * Check if this thread is the current thread.
	 */
	gcc_pure
82
	bool IsInside() const noexcept {
83
#ifdef _WIN32
84 85
		return GetCurrentThreadId() == id;
#else
86 87 88 89 90 91
		/* 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 */
92
		return pthread_self() == inside_handle;
93 94
#endif
	}
95
#endif
96

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

104
	void Join() noexcept;
105 106

private:
107
	void Run() noexcept;
108

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

};

#endif