blob: 8dce8602f66488668e94274ccf0a628fc5e2d9a0 [file] [log] [blame]
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_V8THREADS_H_
29#define V8_V8THREADS_H_
30
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
34
35class ThreadState {
36 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037 // Returns NULL after the last one.
38 ThreadState* Next();
39
40 enum List {FREE_LIST, IN_USE_LIST};
41
42 void LinkInto(List list);
43 void Unlink();
44
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000045 // Id of thread.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000046 void set_id(ThreadId id) { id_ = id; }
47 ThreadId id() { return id_; }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000048
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +000049 // Should the thread be terminated when it is restored?
50 bool terminate_on_restore() { return terminate_on_restore_; }
51 void set_terminate_on_restore(bool terminate_on_restore) {
52 terminate_on_restore_ = terminate_on_restore;
53 }
54
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 // Get data area for archiving a thread.
56 char* data() { return data_; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +000057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000059 explicit ThreadState(ThreadManager* thread_manager);
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +000060 ~ThreadState();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061
62 void AllocateSpace();
63
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000064 ThreadId id_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +000065 bool terminate_on_restore_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 char* data_;
67 ThreadState* next_;
68 ThreadState* previous_;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000070 ThreadManager* thread_manager_;
71
72 friend class ThreadManager;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073};
74
75
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +000076// Defined in isolate.h.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000077class ThreadLocalTop;
78
79
80class ThreadVisitor {
81 public:
82 // ThreadLocalTop may be only available during this call.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000083 virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000084
85 protected:
86 virtual ~ThreadVisitor() {}
87};
88
89
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090class ThreadManager {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 void Lock();
93 void Unlock();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 void ArchiveThread();
96 bool RestoreThread();
97 void FreeThreadResources();
98 bool IsArchived();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000100 void Iterate(ObjectVisitor* v);
101 void IterateArchivedThreads(ThreadVisitor* v);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000102 bool IsLockedByCurrentThread() {
103 return mutex_owner_.Equals(ThreadId::Current());
104 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000105
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000106 ThreadId CurrentId();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000107
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000108 void TerminateExecution(ThreadId thread_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109
110 // Iterate over in-use states.
111 ThreadState* FirstThreadStateInUse();
112 ThreadState* GetFreeThreadState();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000115 ThreadManager();
116 ~ThreadManager();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000118 void DeleteThreadStateList(ThreadState* anchor);
119
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 void EagerlyArchiveThread();
121
122 Mutex* mutex_;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000123 ThreadId mutex_owner_;
124 ThreadId lazily_archived_thread_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 ThreadState* lazily_archived_thread_state_;
126
127 // In the following two lists there is always at least one object on the list.
128 // The first object is a flying anchor that is only there to simplify linking
129 // and unlinking.
130 // Head of linked list of free states.
131 ThreadState* free_anchor_;
132 // Head of linked list of states in use.
133 ThreadState* in_use_anchor_;
134
135 Isolate* isolate_;
136
137 friend class Isolate;
138 friend class ThreadState;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139};
140
141
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000142// The ContextSwitcher thread is used to schedule regular preemptions to
143// multiple running V8 threads. Generally it is necessary to call
144// StartPreemption if there is more than one thread running. If not, a single
145// JavaScript can take full control of V8 and not allow other threads to run.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146class ContextSwitcher: public Thread {
147 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000148 // Set the preemption interval for the ContextSwitcher thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 static void StartPreemption(int every_n_ms);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000150
151 // Stop sending preemption requests to threads.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 static void StopPreemption();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000153
ager@chromium.org32912102009-01-16 10:38:43 +0000154 // Preempted thread needs to call back to the ContextSwitcher to acknowledge
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000155 // the handling of a preemption request.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 static void PreemptionReceived();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000159 ContextSwitcher(Isolate* isolate, int every_n_ms);
160
161 Isolate* isolate() const { return isolate_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000162
163 void Run();
164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 bool keep_going_;
166 int sleep_ms_;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000167 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168};
169
170} } // namespace v8::internal
171
172#endif // V8_V8THREADS_H_