MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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() {
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 {
67 return *this == Null();
68 }
69
73 gcc_pure
74 static const ThreadId GetCurrent() {
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 {
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 {
95 return *this == GetCurrent();
96 }
97 };
98
99 #endif
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
Definition: Id.hxx:83
Compiler.h
ThreadId::GetCurrent
static gcc_pure const ThreadId GetCurrent()
Return the current thread's id .
Definition: Id.hxx:74
gcc_const
#define gcc_const
Definition: Compiler.h:109
ThreadId::IsInside
bool IsInside() const
Check if this thread is the current thread.
Definition: Id.hxx:94
ThreadId::ThreadId
constexpr ThreadId(pthread_t _id)
Definition: Id.hxx:52
ThreadId::Null
static gcc_const ThreadId Null()
Definition: Id.hxx:56
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
ThreadId::IsNull
gcc_pure bool IsNull() const
Definition: Id.hxx:66