v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
v8threads.h
Go to the documentation of this file.
1// Copyright 2012 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_EXECUTION_V8THREADS_H_
6#define V8_EXECUTION_V8THREADS_H_
7
8#include <atomic>
9
11
12namespace v8 {
13namespace internal {
14
15class RootVisitor;
16class ThreadLocalTop;
17
19 public:
20 // Returns nullptr after the last one.
22
24
25 void LinkInto(List list);
26 void Unlink();
27
28 // Id of thread.
29 void set_id(ThreadId id) { id_ = id; }
30 ThreadId id() { return id_; }
31
32 // Get data area for archiving a thread.
33 char* data() { return data_; }
34
35 private:
36 explicit ThreadState(ThreadManager* thread_manager);
38
39 void AllocateSpace();
40
42 char* data_;
45
47
48 friend class ThreadManager;
49};
50
52 public:
53 // ThreadLocalTop may be only available during this call.
54 virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
55
56 protected:
57 virtual ~ThreadVisitor() = default;
58};
59
61 public:
62 void Lock();
64
65 void InitThread(const ExecutionAccess&);
66 void ArchiveThread();
67 bool RestoreThread();
69 bool IsArchived();
70
71 void Iterate(RootVisitor* v);
74 return mutex_owner_.load(std::memory_order_relaxed) == ThreadId::Current();
75 }
76 bool IsLockedByThread(ThreadId id) const {
77 return mutex_owner_.load(std::memory_order_relaxed) == id;
78 }
79
81
82 // Iterate over in-use states.
85
86 private:
87 explicit ThreadManager(Isolate* isolate);
89
91
93
95 // {ThreadId} must be trivially copyable to be stored in {std::atomic}.
97 std::atomic<ThreadId> mutex_owner_;
100
101 // In the following two lists there is always at least one object on the list.
102 // The first object is a flying anchor that is only there to simplify linking
103 // and unlinking.
104 // Head of linked list of free states.
106 // Head of linked list of states in use.
108
110
111 friend class Isolate;
112 friend class ThreadState;
113};
114
115} // namespace internal
116} // namespace v8
117
118#endif // V8_EXECUTION_V8THREADS_H_
static ThreadId Current()
Definition thread-id.h:32
void Iterate(RootVisitor *v)
Definition v8threads.cc:302
void IterateArchivedThreads(ThreadVisitor *v)
Definition v8threads.cc:315
ThreadState * FirstThreadStateInUse()
Definition v8threads.cc:208
ThreadState * lazily_archived_thread_state_
Definition v8threads.h:99
ThreadState * in_use_anchor_
Definition v8threads.h:107
ThreadManager(Isolate *isolate)
Definition v8threads.cc:220
void InitThread(const ExecutionAccess &)
Definition v8threads.cc:87
std::atomic< ThreadId > mutex_owner_
Definition v8threads.h:97
void DeleteThreadStateList(ThreadState *anchor)
Definition v8threads.cc:236
bool IsLockedByCurrentThread() const
Definition v8threads.h:73
ASSERT_TRIVIALLY_COPYABLE(i::ThreadId)
V8_EXPORT_PRIVATE void Unlock()
Definition v8threads.cc:155
bool IsLockedByThread(ThreadId id) const
Definition v8threads.h:76
ThreadState * GetFreeThreadState()
Definition v8threads.cc:197
void set_id(ThreadId id)
Definition v8threads.h:29
ThreadState * previous_
Definition v8threads.h:44
void LinkInto(List list)
Definition v8threads.cc:187
ThreadManager * thread_manager_
Definition v8threads.h:46
ThreadState(ThreadManager *thread_manager)
Definition v8threads.cc:169
virtual void VisitThread(Isolate *isolate, ThreadLocalTop *top)=0
virtual ~ThreadVisitor()=default
#define V8_EXPORT_PRIVATE
Definition macros.h:460