• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_V8THREADS_H_
6 #define V8_V8THREADS_H_
7 
8 #include "src/isolate.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class RootVisitor;
14 class ThreadLocalTop;
15 
16 class ThreadState {
17  public:
18   // Returns nullptr after the last one.
19   ThreadState* Next();
20 
21   enum List {FREE_LIST, IN_USE_LIST};
22 
23   void LinkInto(List list);
24   void Unlink();
25 
26   // Id of thread.
set_id(ThreadId id)27   void set_id(ThreadId id) { id_ = id; }
id()28   ThreadId id() { return id_; }
29 
30   // Should the thread be terminated when it is restored?
terminate_on_restore()31   bool terminate_on_restore() { return terminate_on_restore_; }
set_terminate_on_restore(bool terminate_on_restore)32   void set_terminate_on_restore(bool terminate_on_restore) {
33     terminate_on_restore_ = terminate_on_restore;
34   }
35 
36   // Get data area for archiving a thread.
data()37   char* data() { return data_; }
38 
39  private:
40   explicit ThreadState(ThreadManager* thread_manager);
41   ~ThreadState();
42 
43   void AllocateSpace();
44 
45   ThreadId id_;
46   bool terminate_on_restore_;
47   char* data_;
48   ThreadState* next_;
49   ThreadState* previous_;
50 
51   ThreadManager* thread_manager_;
52 
53   friend class ThreadManager;
54 };
55 
56 class ThreadVisitor {
57  public:
58   // ThreadLocalTop may be only available during this call.
59   virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
60 
61  protected:
~ThreadVisitor()62   virtual ~ThreadVisitor() {}
63 };
64 
65 class ThreadManager {
66  public:
67   void Lock();
68   void Unlock();
69 
70   void InitThread(const ExecutionAccess&);
71   void ArchiveThread();
72   bool RestoreThread();
73   void FreeThreadResources();
74   bool IsArchived();
75 
76   void Iterate(RootVisitor* v);
77   void IterateArchivedThreads(ThreadVisitor* v);
IsLockedByCurrentThread()78   bool IsLockedByCurrentThread() {
79     return mutex_owner_.Equals(ThreadId::Current());
80   }
81 
82   ThreadId CurrentId();
83 
84   void TerminateExecution(ThreadId thread_id);
85 
86   // Iterate over in-use states.
87   ThreadState* FirstThreadStateInUse();
88   ThreadState* GetFreeThreadState();
89 
90  private:
91   ThreadManager();
92   ~ThreadManager();
93 
94   void DeleteThreadStateList(ThreadState* anchor);
95 
96   void EagerlyArchiveThread();
97 
98   base::Mutex mutex_;
99   ThreadId mutex_owner_;
100   ThreadId lazily_archived_thread_;
101   ThreadState* lazily_archived_thread_state_;
102 
103   // In the following two lists there is always at least one object on the list.
104   // The first object is a flying anchor that is only there to simplify linking
105   // and unlinking.
106   // Head of linked list of free states.
107   ThreadState* free_anchor_;
108   // Head of linked list of states in use.
109   ThreadState* in_use_anchor_;
110 
111   Isolate* isolate_;
112 
113   friend class Isolate;
114   friend class ThreadState;
115 };
116 
117 
118 }  // namespace internal
119 }  // namespace v8
120 
121 #endif  // V8_V8THREADS_H_
122