OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #ifndef V8_V8THREADS_H_ | 28 #ifndef V8_V8THREADS_H_ |
29 #define V8_V8THREADS_H_ | 29 #define V8_V8THREADS_H_ |
30 | 30 |
31 namespace v8 { | 31 namespace v8 { |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
34 | 34 |
35 class ThreadState { | 35 class ThreadState { |
36 public: | 36 public: |
37 // Iterate over in-use states. | |
38 static ThreadState* FirstInUse(); | |
39 // Returns NULL after the last one. | 37 // Returns NULL after the last one. |
40 ThreadState* Next(); | 38 ThreadState* Next(); |
41 | 39 |
42 enum List {FREE_LIST, IN_USE_LIST}; | 40 enum List {FREE_LIST, IN_USE_LIST}; |
43 | 41 |
44 void LinkInto(List list); | 42 void LinkInto(List list); |
45 void Unlink(); | 43 void Unlink(); |
46 | 44 |
47 static ThreadState* GetFree(); | |
48 | |
49 // Id of thread. | 45 // Id of thread. |
50 void set_id(int id) { id_ = id; } | 46 void set_id(int id) { id_ = id; } |
51 int id() { return id_; } | 47 int id() { return id_; } |
52 | 48 |
53 // Should the thread be terminated when it is restored? | 49 // Should the thread be terminated when it is restored? |
54 bool terminate_on_restore() { return terminate_on_restore_; } | 50 bool terminate_on_restore() { return terminate_on_restore_; } |
55 void set_terminate_on_restore(bool terminate_on_restore) { | 51 void set_terminate_on_restore(bool terminate_on_restore) { |
56 terminate_on_restore_ = terminate_on_restore; | 52 terminate_on_restore_ = terminate_on_restore; |
57 } | 53 } |
58 | 54 |
59 // Get data area for archiving a thread. | 55 // Get data area for archiving a thread. |
60 char* data() { return data_; } | 56 char* data() { return data_; } |
61 private: | 57 private: |
62 ThreadState(); | 58 explicit ThreadState(ThreadManager* thread_manager); |
63 | 59 |
64 void AllocateSpace(); | 60 void AllocateSpace(); |
65 | 61 |
66 int id_; | 62 int id_; |
67 bool terminate_on_restore_; | 63 bool terminate_on_restore_; |
68 char* data_; | 64 char* data_; |
69 ThreadState* next_; | 65 ThreadState* next_; |
70 ThreadState* previous_; | 66 ThreadState* previous_; |
71 | 67 |
72 // In the following two lists there is always at least one object on the list. | 68 ThreadManager* thread_manager_; |
73 // The first object is a flying anchor that is only there to simplify linking | 69 |
74 // and unlinking. | 70 friend class ThreadManager; |
75 // Head of linked list of free states. | |
76 static ThreadState* free_anchor_; | |
77 // Head of linked list of states in use. | |
78 static ThreadState* in_use_anchor_; | |
79 }; | 71 }; |
80 | 72 |
81 | 73 |
82 // Defined in top.h | 74 // Defined in top.h |
83 class ThreadLocalTop; | 75 class ThreadLocalTop; |
84 | 76 |
85 | 77 |
86 class ThreadVisitor { | 78 class ThreadVisitor { |
87 public: | 79 public: |
88 // ThreadLocalTop may be only available during this call. | 80 // ThreadLocalTop may be only available during this call. |
89 virtual void VisitThread(ThreadLocalTop* top) = 0; | 81 virtual void VisitThread(ThreadLocalTop* top) = 0; |
90 | 82 |
91 protected: | 83 protected: |
92 virtual ~ThreadVisitor() {} | 84 virtual ~ThreadVisitor() {} |
93 }; | 85 }; |
94 | 86 |
95 | 87 |
96 class ThreadManager : public AllStatic { | 88 class ThreadManager { |
97 public: | 89 public: |
98 static void Lock(); | 90 void Lock(); |
99 static void Unlock(); | 91 void Unlock(); |
100 | 92 |
101 static void ArchiveThread(); | 93 void ArchiveThread(); |
102 static bool RestoreThread(); | 94 bool RestoreThread(); |
103 static void FreeThreadResources(); | 95 void FreeThreadResources(); |
104 static bool IsArchived(); | 96 bool IsArchived(); |
105 | 97 |
106 static void Iterate(ObjectVisitor* v); | 98 void Iterate(ObjectVisitor* v); |
107 static void IterateArchivedThreads(ThreadVisitor* v); | 99 void IterateArchivedThreads(ThreadVisitor* v); |
108 static bool IsLockedByCurrentThread() { return mutex_owner_.IsSelf(); } | 100 bool IsLockedByCurrentThread() { return mutex_owner_.IsSelf(); } |
109 | 101 |
110 static int CurrentId(); | 102 int CurrentId(); |
111 static void AssignId(); | |
112 static bool HasId(); | |
113 | 103 |
114 static void TerminateExecution(int thread_id); | 104 void TerminateExecution(int thread_id); |
| 105 |
| 106 // Iterate over in-use states. |
| 107 ThreadState* FirstThreadStateInUse(); |
| 108 ThreadState* GetFreeThreadState(); |
115 | 109 |
116 static const int kInvalidId = -1; | 110 static const int kInvalidId = -1; |
117 private: | 111 private: |
118 static void EagerlyArchiveThread(); | 112 ThreadManager(); |
| 113 ~ThreadManager(); |
119 | 114 |
120 static int last_id_; // V8 threads are identified through an integer. | 115 void EagerlyArchiveThread(); |
121 static Mutex* mutex_; | 116 |
122 static ThreadHandle mutex_owner_; | 117 Mutex* mutex_; |
123 static ThreadHandle lazily_archived_thread_; | 118 ThreadHandle mutex_owner_; |
124 static ThreadState* lazily_archived_thread_state_; | 119 ThreadHandle lazily_archived_thread_; |
| 120 ThreadState* lazily_archived_thread_state_; |
| 121 |
| 122 // In the following two lists there is always at least one object on the list. |
| 123 // The first object is a flying anchor that is only there to simplify linking |
| 124 // and unlinking. |
| 125 // Head of linked list of free states. |
| 126 ThreadState* free_anchor_; |
| 127 // Head of linked list of states in use. |
| 128 ThreadState* in_use_anchor_; |
| 129 |
| 130 Isolate* isolate_; |
| 131 |
| 132 friend class Isolate; |
| 133 friend class ThreadState; |
125 }; | 134 }; |
126 | 135 |
127 | 136 |
128 // The ContextSwitcher thread is used to schedule regular preemptions to | 137 // The ContextSwitcher thread is used to schedule regular preemptions to |
129 // multiple running V8 threads. Generally it is necessary to call | 138 // multiple running V8 threads. Generally it is necessary to call |
130 // StartPreemption if there is more than one thread running. If not, a single | 139 // StartPreemption if there is more than one thread running. If not, a single |
131 // JavaScript can take full control of V8 and not allow other threads to run. | 140 // JavaScript can take full control of V8 and not allow other threads to run. |
132 class ContextSwitcher: public Thread { | 141 class ContextSwitcher: public Thread { |
133 public: | 142 public: |
134 // Set the preemption interval for the ContextSwitcher thread. | 143 // Set the preemption interval for the ContextSwitcher thread. |
135 static void StartPreemption(int every_n_ms); | 144 static void StartPreemption(int every_n_ms); |
136 | 145 |
137 // Stop sending preemption requests to threads. | 146 // Stop sending preemption requests to threads. |
138 static void StopPreemption(); | 147 static void StopPreemption(); |
139 | 148 |
140 // Preempted thread needs to call back to the ContextSwitcher to acknowledge | 149 // Preempted thread needs to call back to the ContextSwitcher to acknowledge |
141 // the handling of a preemption request. | 150 // the handling of a preemption request. |
142 static void PreemptionReceived(); | 151 static void PreemptionReceived(); |
143 | 152 |
144 private: | 153 private: |
145 explicit ContextSwitcher(int every_n_ms); | 154 explicit ContextSwitcher(Isolate* isolate, int every_n_ms); |
146 | 155 |
147 void Run(); | 156 void Run(); |
148 | 157 |
149 bool keep_going_; | 158 bool keep_going_; |
150 int sleep_ms_; | 159 int sleep_ms_; |
151 | |
152 static ContextSwitcher* singleton_; | |
153 }; | 160 }; |
154 | 161 |
155 } } // namespace v8::internal | 162 } } // namespace v8::internal |
156 | 163 |
157 #endif // V8_V8THREADS_H_ | 164 #endif // V8_V8THREADS_H_ |
OLD | NEW |