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
static gcc_const ThreadId Null() noexcept
Definition: Id.hxx:56
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:83
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:66
#define gcc_const
Definition: Compiler.h:109
constexpr ThreadId(pthread_t _id)
Definition: Id.hxx:52
static gcc_pure const ThreadId GetCurrent() noexcept
Return the current thread&#39;s id .
Definition: Id.hxx:74
#define gcc_pure
Definition: Compiler.h:116