Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_THREAD_ID_H_
6 : #define V8_THREAD_ID_H_
7 :
8 : namespace v8 {
9 : namespace internal {
10 :
11 : // Platform-independent, reliable thread identifier.
12 : class ThreadId {
13 : public:
14 : // Creates an invalid ThreadId.
15 : constexpr ThreadId() noexcept : ThreadId(kInvalidId) {}
16 :
17 337687 : bool operator==(const ThreadId& other) const { return id_ == other.id_; }
18 : bool operator!=(const ThreadId& other) const { return id_ != other.id_; }
19 :
20 : // Checks whether this ThreadId refers to any thread.
21 39855 : bool IsValid() const { return id_ != kInvalidId; }
22 :
23 : // Converts ThreadId to an integer representation.
24 434437 : constexpr int ToInteger() const { return id_; }
25 :
26 : // Returns ThreadId for current thread if it exists or invalid id.
27 : static ThreadId TryGetCurrent();
28 :
29 : // Returns ThreadId for current thread.
30 680455 : static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
31 :
32 : // Returns invalid ThreadId (guaranteed not to be equal to any thread).
33 : static constexpr ThreadId Invalid() { return ThreadId(kInvalidId); }
34 :
35 : // Converts ThreadId to an integer representation
36 : // (required for public API: V8::V8::TerminateExecution).
37 : static constexpr ThreadId FromInteger(int id) { return ThreadId(id); }
38 :
39 : private:
40 : static constexpr int kInvalidId = -1;
41 :
42 62534 : explicit constexpr ThreadId(int id) noexcept : id_(id) {}
43 :
44 : static int GetCurrentThreadId();
45 :
46 : int id_;
47 : };
48 :
49 : } // namespace internal
50 : } // namespace v8
51 :
52 : #endif // V8_THREAD_ID_H_
|