MPD: thread/Thread.hxx Source File

MPD 0.20.18

Thread.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_THREAD_HXX
21 #define MPD_THREAD_HXX
22
23 #include "check.h"
24 #include "util/BindMethod.hxx"
25 #include "Compiler.h"
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #else
30 #include <pthread.h>
31 #endif
32
33 #include <assert.h>
34
35 class Thread {
36 typedef BoundMethod<void()> Function;
37 const Function f;
38
39 #ifdef _WIN32
40 HANDLE handle = nullptr;
41 DWORD id;
42 #else
43 pthread_t handle = pthread_t();
44
45 #ifndef NDEBUG
46
52 pthread_t inside_handle = pthread_t();
53 #endif
54 #endif
55
56 public:
57 explicit Thread(Function _f):f(_f) {}
58
59 Thread(const Thread &) = delete;
60
61 #ifndef NDEBUG
62 ~Thread() {
63 /* all Thread objects must be destructed manually by calling
64 Join(), to clean up */
65 assert(!IsDefined());
66 }
67 #endif
68
69 bool IsDefined() const {
70 #ifdef _WIN32
71 return handle != nullptr;
72 #else
73 return handle != pthread_t();
74 #endif
75 }
76
77 #ifndef NDEBUG
78
81 gcc_pure
82 bool IsInside() const noexcept {
83 #ifdef _WIN32
84 return GetCurrentThreadId() == id;
85 #else
86 /* note: not using pthread_equal() because that
87 function "is undefined if either thread ID is not
88 valid so we can't safely use it on
89 default-constructed values" (comment from
90 libstdc++) - and if both libstdc++ and libc++ get
91 away with this, we can do it as well */
92 return pthread_self() == inside_handle;
93 #endif
94 }
95 #endif
96
97 void Start();
98 void Join();
99
100 private:
101 void Run();
102
103 #ifdef _WIN32
104 static DWORD WINAPI ThreadProc(LPVOID ctx);
105 #else
106 static void *ThreadProc(void *ctx);
107 #endif
108
109 };
110
111 #endif
check.h
BoundMethod
This object stores a function pointer wrapping a method, and a reference to an instance of the method...
Definition: BindMethod.hxx:44
Thread::Start
void Start()
Thread::Thread
Thread(Function _f)
Definition: Thread.hxx:57
BindMethod.hxx
Compiler.h
Thread::~Thread
~Thread()
Definition: Thread.hxx:62
Thread::Join
void Join()
Thread
Definition: Thread.hxx:35
Thread::IsInside
gcc_pure bool IsInside() const noexcept
Check if this thread is the current thread.
Definition: Thread.hxx:82
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
Thread::IsDefined
bool IsDefined() const
Definition: Thread.hxx:69