Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // 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 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | |
| 35 | class ThreadState { |
| 36 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | // 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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | // Id of thread. |
| 46 | void set_id(int id) { id_ = id; } |
| 47 | int id() { return id_; } |
| 48 | |
| 49 | // 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 | |
| 55 | // Get data area for archiving a thread. |
| 56 | char* data() { return data_; } |
| 57 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 58 | explicit ThreadState(ThreadManager* thread_manager); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | |
| 60 | void AllocateSpace(); |
| 61 | |
| 62 | int id_; |
| 63 | bool terminate_on_restore_; |
| 64 | char* data_; |
| 65 | ThreadState* next_; |
| 66 | ThreadState* previous_; |
| 67 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 68 | ThreadManager* thread_manager_; |
| 69 | |
| 70 | friend class ThreadManager; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 74 | // Defined in top.h |
| 75 | class ThreadLocalTop; |
| 76 | |
| 77 | |
| 78 | class ThreadVisitor { |
| 79 | public: |
| 80 | // ThreadLocalTop may be only available during this call. |
| 81 | virtual void VisitThread(ThreadLocalTop* top) = 0; |
| 82 | |
| 83 | protected: |
| 84 | virtual ~ThreadVisitor() {} |
| 85 | }; |
| 86 | |
| 87 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 88 | class ThreadManager { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 90 | void Lock(); |
| 91 | void Unlock(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 93 | void ArchiveThread(); |
| 94 | bool RestoreThread(); |
| 95 | void FreeThreadResources(); |
| 96 | bool IsArchived(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 98 | void Iterate(ObjectVisitor* v); |
| 99 | void IterateArchivedThreads(ThreadVisitor* v); |
| 100 | bool IsLockedByCurrentThread() { return mutex_owner_.IsSelf(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 102 | int CurrentId(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 104 | void TerminateExecution(int thread_id); |
| 105 | |
| 106 | // Iterate over in-use states. |
| 107 | ThreadState* FirstThreadStateInUse(); |
| 108 | ThreadState* GetFreeThreadState(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | |
| 110 | static const int kInvalidId = -1; |
| 111 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 112 | ThreadManager(); |
| 113 | ~ThreadManager(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 115 | void EagerlyArchiveThread(); |
| 116 | |
| 117 | Mutex* mutex_; |
| 118 | ThreadHandle mutex_owner_; |
| 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; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
| 137 | // The ContextSwitcher thread is used to schedule regular preemptions to |
| 138 | // multiple running V8 threads. Generally it is necessary to call |
| 139 | // StartPreemption if there is more than one thread running. If not, a single |
| 140 | // JavaScript can take full control of V8 and not allow other threads to run. |
| 141 | class ContextSwitcher: public Thread { |
| 142 | public: |
| 143 | // Set the preemption interval for the ContextSwitcher thread. |
| 144 | static void StartPreemption(int every_n_ms); |
| 145 | |
| 146 | // Stop sending preemption requests to threads. |
| 147 | static void StopPreemption(); |
| 148 | |
| 149 | // Preempted thread needs to call back to the ContextSwitcher to acknowledge |
| 150 | // the handling of a preemption request. |
| 151 | static void PreemptionReceived(); |
| 152 | |
| 153 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 154 | explicit ContextSwitcher(Isolate* isolate, int every_n_ms); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | |
| 156 | void Run(); |
| 157 | |
| 158 | bool keep_going_; |
| 159 | int sleep_ms_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } } // namespace v8::internal |
| 163 | |
| 164 | #endif // V8_V8THREADS_H_ |