ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class ThreadState { |
| 36 | public: |
| 37 | // Iterate over in-use states. |
| 38 | static ThreadState* FirstInUse(); |
| 39 | // Returns NULL after the last one. |
| 40 | ThreadState* Next(); |
| 41 | |
| 42 | enum List {FREE_LIST, IN_USE_LIST}; |
| 43 | |
| 44 | void LinkInto(List list); |
| 45 | void Unlink(); |
| 46 | |
| 47 | static ThreadState* GetFree(); |
| 48 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 49 | // Id of thread. |
| 50 | void set_id(int id) { id_ = id; } |
| 51 | int id() { return id_; } |
| 52 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 53 | // Should the thread be terminated when it is restored? |
| 54 | bool terminate_on_restore() { return terminate_on_restore_; } |
| 55 | void set_terminate_on_restore(bool terminate_on_restore) { |
| 56 | terminate_on_restore_ = terminate_on_restore; |
| 57 | } |
| 58 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | // Get data area for archiving a thread. |
| 60 | char* data() { return data_; } |
| 61 | private: |
| 62 | ThreadState(); |
| 63 | |
| 64 | void AllocateSpace(); |
| 65 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 66 | int id_; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 67 | bool terminate_on_restore_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | char* data_; |
| 69 | ThreadState* next_; |
| 70 | ThreadState* previous_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 71 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | // In the following two lists there is always at least one object on the list. |
| 73 | // The first object is a flying anchor that is only there to simplify linking |
| 74 | // and unlinking. |
| 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 | }; |
| 80 | |
| 81 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 82 | // Defined in top.h |
| 83 | class ThreadLocalTop; |
| 84 | |
| 85 | |
| 86 | class ThreadVisitor { |
| 87 | public: |
| 88 | // ThreadLocalTop may be only available during this call. |
| 89 | virtual void VisitThread(ThreadLocalTop* top) = 0; |
| 90 | |
| 91 | protected: |
| 92 | virtual ~ThreadVisitor() {} |
| 93 | }; |
| 94 | |
| 95 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | class ThreadManager : public AllStatic { |
| 97 | public: |
| 98 | static void Lock(); |
| 99 | static void Unlock(); |
| 100 | |
| 101 | static void ArchiveThread(); |
| 102 | static bool RestoreThread(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 103 | static void FreeThreadResources(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 104 | static bool IsArchived(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | |
| 106 | static void Iterate(ObjectVisitor* v); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 107 | static void IterateArchivedThreads(ThreadVisitor* v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | static bool IsLockedByCurrentThread() { return mutex_owner_.IsSelf(); } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 109 | |
| 110 | static int CurrentId(); |
| 111 | static void AssignId(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 112 | static bool HasId(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 113 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 114 | static void TerminateExecution(int thread_id); |
| 115 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 116 | static const int kInvalidId = -1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | private: |
| 118 | static void EagerlyArchiveThread(); |
| 119 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 120 | static int last_id_; // V8 threads are identified through an integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | static Mutex* mutex_; |
| 122 | static ThreadHandle mutex_owner_; |
| 123 | static ThreadHandle lazily_archived_thread_; |
| 124 | static ThreadState* lazily_archived_thread_state_; |
| 125 | }; |
| 126 | |
| 127 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 128 | // The ContextSwitcher thread is used to schedule regular preemptions to |
| 129 | // multiple running V8 threads. Generally it is necessary to call |
| 130 | // 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | class ContextSwitcher: public Thread { |
| 133 | public: |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 134 | // Set the preemption interval for the ContextSwitcher thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | static void StartPreemption(int every_n_ms); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 136 | |
| 137 | // Stop sending preemption requests to threads. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | static void StopPreemption(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 139 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 140 | // Preempted thread needs to call back to the ContextSwitcher to acknowledge |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 141 | // the handling of a preemption request. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | static void PreemptionReceived(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | private: |
| 145 | explicit ContextSwitcher(int every_n_ms); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 146 | |
| 147 | void Run(); |
| 148 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | bool keep_going_; |
| 150 | int sleep_ms_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 151 | |
| 152 | static ContextSwitcher* singleton_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } } // namespace v8::internal |
| 156 | |
| 157 | #endif // V8_V8THREADS_H_ |