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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 private: | 61 private: |
62 ThreadState(); | 62 ThreadState(); |
63 | 63 |
64 void AllocateSpace(); | 64 void AllocateSpace(); |
65 | 65 |
66 int id_; | 66 int id_; |
67 bool terminate_on_restore_; | 67 bool terminate_on_restore_; |
68 char* data_; | 68 char* data_; |
69 ThreadState* next_; | 69 ThreadState* next_; |
70 ThreadState* previous_; | 70 ThreadState* previous_; |
| 71 friend class ThreadManagerData; |
| 72 }; |
| 73 |
| 74 // The ContextSwitcher thread is used to schedule regular preemptions to |
| 75 // multiple running V8 threads. Generally it is necessary to call |
| 76 // StartPreemption if there is more than one thread running. If not, a single |
| 77 // JavaScript can take full control of V8 and not allow other threads to run. |
| 78 class ContextSwitcher: public Thread { |
| 79 public: |
| 80 // Set the preemption interval for the ContextSwitcher thread. |
| 81 static void StartPreemption(int every_n_ms); |
| 82 |
| 83 // Stop sending preemption requests to threads. |
| 84 static void StopPreemption(); |
| 85 |
| 86 // Preempted thread needs to call back to the ContextSwitcher to acknowledge |
| 87 // the handling of a preemption request. |
| 88 static void PreemptionReceived(); |
| 89 |
| 90 private: |
| 91 explicit ContextSwitcher(int every_n_ms); |
| 92 |
| 93 void Run(); |
| 94 |
| 95 bool keep_going_; |
| 96 int sleep_ms_; |
| 97 }; |
| 98 |
| 99 class ThreadManagerData { |
| 100 int last_id_; // V8 threads are identified through an integer. |
| 101 Mutex* mutex_; |
| 102 ThreadHandle mutex_owner_; |
| 103 ThreadHandle lazily_archived_thread_; |
| 104 ThreadState* lazily_archived_thread_state_; |
71 | 105 |
72 // In the following two lists there is always at least one object on the list. | 106 // 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 | 107 // The first object is a flying anchor that is only there to simplify linking |
74 // and unlinking. | 108 // and unlinking. |
75 // Head of linked list of free states. | 109 // Head of linked list of free states. |
76 static ThreadState* free_anchor_; | 110 ThreadState* free_anchor_; |
77 // Head of linked list of states in use. | 111 // Head of linked list of states in use. |
78 static ThreadState* in_use_anchor_; | 112 ThreadState* in_use_anchor_; |
| 113 |
| 114 // This is the ContextSwitcher singleton. There is at most a single thread |
| 115 // running which delivers preemption events to V8 threads. |
| 116 ContextSwitcher* singleton_; |
| 117 |
| 118 Thread::LocalStorageKey thread_state_key_; |
| 119 Thread::LocalStorageKey thread_id_key_; |
| 120 |
| 121 friend class ThreadManager; |
| 122 friend class ThreadState; |
| 123 friend class V8Context; |
| 124 friend class ContextSwitcher; |
| 125 |
| 126 ThreadManagerData(); |
| 127 DISALLOW_COPY_AND_ASSIGN(ThreadManagerData); |
79 }; | 128 }; |
80 | 129 |
81 | |
82 class ThreadManager : public AllStatic { | 130 class ThreadManager : public AllStatic { |
83 public: | 131 public: |
84 static void Lock(); | 132 static void Lock(); |
85 static void Unlock(); | 133 static void Unlock(); |
86 | 134 |
87 static void ArchiveThread(); | 135 static void ArchiveThread(); |
88 static bool RestoreThread(); | 136 static bool RestoreThread(); |
89 static void FreeThreadResources(); | 137 static void FreeThreadResources(); |
90 static bool IsArchived(); | 138 static bool IsArchived(); |
91 | 139 |
92 static void Iterate(ObjectVisitor* v); | 140 static void Iterate(ObjectVisitor* v); |
93 static void MarkCompactPrologue(bool is_compacting); | 141 static void MarkCompactPrologue(bool is_compacting); |
94 static void MarkCompactEpilogue(bool is_compacting); | 142 static void MarkCompactEpilogue(bool is_compacting); |
95 static bool IsLockedByCurrentThread() { return mutex_owner_.IsSelf(); } | 143 static bool IsLockedByCurrentThread() { |
| 144 return v8_context()->thread_manager_data_.mutex_owner_.IsSelf(); |
| 145 } |
96 | 146 |
97 static int CurrentId(); | 147 static int CurrentId(); |
98 static void AssignId(); | 148 static void AssignId(); |
99 static bool HasId(); | 149 static bool HasId(); |
100 | 150 |
101 static void TerminateExecution(int thread_id); | 151 static void TerminateExecution(int thread_id); |
102 | 152 |
103 static const int kInvalidId = -1; | 153 static const int kInvalidId = -1; |
104 private: | 154 private: |
105 static void EagerlyArchiveThread(); | 155 static void EagerlyArchiveThread(); |
106 | |
107 static int last_id_; // V8 threads are identified through an integer. | |
108 static Mutex* mutex_; | |
109 static ThreadHandle mutex_owner_; | |
110 static ThreadHandle lazily_archived_thread_; | |
111 static ThreadState* lazily_archived_thread_state_; | |
112 }; | |
113 | |
114 | |
115 // The ContextSwitcher thread is used to schedule regular preemptions to | |
116 // multiple running V8 threads. Generally it is necessary to call | |
117 // StartPreemption if there is more than one thread running. If not, a single | |
118 // JavaScript can take full control of V8 and not allow other threads to run. | |
119 class ContextSwitcher: public Thread { | |
120 public: | |
121 // Set the preemption interval for the ContextSwitcher thread. | |
122 static void StartPreemption(int every_n_ms); | |
123 | |
124 // Stop sending preemption requests to threads. | |
125 static void StopPreemption(); | |
126 | |
127 // Preempted thread needs to call back to the ContextSwitcher to acknowledge | |
128 // the handling of a preemption request. | |
129 static void PreemptionReceived(); | |
130 | |
131 private: | |
132 explicit ContextSwitcher(int every_n_ms); | |
133 | |
134 void Run(); | |
135 | |
136 bool keep_going_; | |
137 int sleep_ms_; | |
138 | |
139 static ContextSwitcher* singleton_; | |
140 }; | 156 }; |
141 | 157 |
142 } } // namespace v8::internal | 158 } } // namespace v8::internal |
143 | 159 |
144 #endif // V8_V8THREADS_H_ | 160 #endif // V8_V8THREADS_H_ |
OLD | NEW |