MPD: thread/Id.hxx Source File

MPD 0.20.15

Id.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_ID_HXX
21 #define MPD_THREAD_ID_HXX
22
23 #include "Compiler.h"
24
25 #ifdef _WIN32
26 #include <windows.h>
27 #else
28 #include <pthread.h>
29 #endif
30
36 class ThreadId {
37 #ifdef _WIN32
38 DWORD id;
39 #else
40 pthread_t id;
41 #endif
42
43 public:
47 ThreadId() = default;
48
49 #ifdef _WIN32
50 constexpr ThreadId(DWORD _id):id(_id) {}
51 #else
52 constexpr ThreadId(pthread_t _id):id(_id) {}
53 #endif
54
55 gcc_const
56 static ThreadId Null() noexcept {
57 #ifdef _WIN32
58 return 0;
59 #else
60 static ThreadId null;
61 return null;
62 #endif
63 }
64
65 gcc_pure
66 bool IsNull() const noexcept {
67 return *this == Null();
68 }
69
73 gcc_pure
74 static const ThreadId GetCurrent() noexcept {
75 #ifdef _WIN32
76 return ::GetCurrentThreadId();
77 #else
78 return pthread_self();
79 #endif
80 }
81
82 gcc_pure
83 bool operator==(const ThreadId &other) const noexcept {
84 #ifdef _WIN32
85 return id == other.id;
86 #else
87 return pthread_equal(id, other.id);
88 #endif
89 }
90
94 bool IsInside() const noexcept {
95 return *this == GetCurrent();
96 }
97 };
98
99 #endif
ThreadId::Null
static gcc_const ThreadId Null() noexcept
Definition: Id.hxx:56
ThreadId
A low-level identification for a thread.
Definition: Id.hxx:36
ThreadId::ThreadId
ThreadId()=default
No initialisation.
ThreadId::operator==
gcc_pure bool operator==(const ThreadId &other) const noexcept
Definition: Id.hxx:83
ThreadId::IsInside
bool IsInside() const noexcept
Check if this thread is the current thread.
Definition: Id.hxx:94
ThreadId::IsNull
gcc_pure bool IsNull() const noexcept
Definition: Id.hxx:66
Compiler.h
gcc_const
#define gcc_const
Definition: Compiler.h:109
ThreadId::ThreadId
constexpr ThreadId(pthread_t _id)
Definition: Id.hxx:52
ThreadId::GetCurrent
static gcc_pure const ThreadId GetCurrent() noexcept
Return the current thread&#39;s id .
Definition: Id.hxx:74
gcc_pure
#define gcc_pure
Definition: Compiler.h:116