MPD  0.20.18
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  static constexpr ThreadId Null() noexcept {
56 #ifdef _WIN32
57  return 0;
58 #else
59  return pthread_t();
60 #endif
61  }
62 
63  gcc_pure
64  bool IsNull() const noexcept {
65  return *this == Null();
66  }
67 
71  gcc_pure
72  static const ThreadId GetCurrent() noexcept {
73 #ifdef _WIN32
74  return ::GetCurrentThreadId();
75 #else
76  return pthread_self();
77 #endif
78  }
79 
80  gcc_pure
81  bool operator==(const ThreadId &other) const noexcept {
82  /* note: not using pthread_equal() because that
83  function "is undefined if either thread ID is not
84  valid so we can't safely use it on
85  default-constructed values" (comment from
86  libstdc++) - and if both libstdc++ and libc++ get
87  away with this, we can do it as well */
88  return id == other.id;
89  }
90 
94  bool IsInside() const noexcept {
95  return *this == GetCurrent();
96  }
97 };
98 
99 #endif
static constexpr ThreadId Null() noexcept
Definition: Id.hxx:55
A low-level identification for a thread.
Definition: Id.hxx:36
ThreadId()=default
No initialisation.
gcc_pure bool operator==(const ThreadId &other) const noexcept
Definition: Id.hxx:81
bool IsInside() const noexcept
Check if this thread is the current thread.
Definition: Id.hxx:94
gcc_pure bool IsNull() const noexcept
Definition: Id.hxx:64
constexpr ThreadId(pthread_t _id)
Definition: Id.hxx:52
static gcc_pure const ThreadId GetCurrent() noexcept
Return the current thread's id .
Definition: Id.hxx:72
#define gcc_pure
Definition: Compiler.h:116