blob: 3ba823a7202007fdd84c23f61bcc041d05b6ce2d [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 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_; }
57 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000058 explicit ThreadState(ThreadManager* thread_manager);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059
60 void AllocateSpace();
61
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000062 ThreadId id_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +000063 bool terminate_on_restore_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064 char* data_;
65 ThreadState* next_;
66 ThreadState* previous_;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000067
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 ThreadManager* thread_manager_;
69
70 friend class ThreadManager;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071};
72
73
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000074// Defined in top.h
75class ThreadLocalTop;
76
77
78class ThreadVisitor {
79 public:
80 // ThreadLocalTop may be only available during this call.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000081 virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000082
83 protected:
84 virtual ~ThreadVisitor() {}
85};
86
87
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000088class ThreadManager {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 void Lock();
91 void Unlock();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093 void ArchiveThread();
94 bool RestoreThread();
95 void FreeThreadResources();
96 bool IsArchived();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000098 void Iterate(ObjectVisitor* v);
99 void IterateArchivedThreads(ThreadVisitor* v);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000100 bool IsLockedByCurrentThread() {
101 return mutex_owner_.Equals(ThreadId::Current());
102 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000103
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000104 ThreadId CurrentId();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000105
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000106 void TerminateExecution(ThreadId thread_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107
108 // Iterate over in-use states.
109 ThreadState* FirstThreadStateInUse();
110 ThreadState* GetFreeThreadState();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113 ThreadManager();
114 ~ThreadManager();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 void EagerlyArchiveThread();
117
118 Mutex* mutex_;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000119 ThreadId mutex_owner_;
120 ThreadId lazily_archived_thread_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 ThreadState* lazily_archived_thread_state_;
122
123 // In the following two lists there is always at least one object on the list.
124 // The first object is a flying anchor that is only there to simplify linking
125 // and unlinking.
126 // Head of linked list of free states.
127 ThreadState* free_anchor_;
128 // Head of linked list of states in use.
129 ThreadState* in_use_anchor_;
130
131 Isolate* isolate_;
132
133 friend class Isolate;
134 friend class ThreadState;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135};
136
137
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000138// The ContextSwitcher thread is used to schedule regular preemptions to
139// multiple running V8 threads. Generally it is necessary to call
140// StartPreemption if there is more than one thread running. If not, a single
141// JavaScript can take full control of V8 and not allow other threads to run.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142class ContextSwitcher: public Thread {
143 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000144 // Set the preemption interval for the ContextSwitcher thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 static void StartPreemption(int every_n_ms);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000146
147 // Stop sending preemption requests to threads.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 static void StopPreemption();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000149
ager@chromium.org32912102009-01-16 10:38:43 +0000150 // Preempted thread needs to call back to the ContextSwitcher to acknowledge
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000151 // the handling of a preemption request.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 static void PreemptionReceived();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000155 ContextSwitcher(Isolate* isolate, int every_n_ms);
156
157 Isolate* isolate() const { return isolate_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000158
159 void Run();
160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 bool keep_going_;
162 int sleep_ms_;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000163 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164};
165
166} } // namespace v8::internal
167
168#endif // V8_V8THREADS_H_