[V8] Introduce a QML compilation mode
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / v8threads.h
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:
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
45   // Id of thread.
46   void set_id(ThreadId id) { id_ = id; }
47   ThreadId 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
58  private:
59   explicit ThreadState(ThreadManager* thread_manager);
60
61   void AllocateSpace();
62
63   ThreadId id_;
64   bool terminate_on_restore_;
65   char* data_;
66   ThreadState* next_;
67   ThreadState* previous_;
68
69   ThreadManager* thread_manager_;
70
71   friend class ThreadManager;
72 };
73
74
75 // Defined in isolate.h.
76 class ThreadLocalTop;
77
78
79 class ThreadVisitor {
80  public:
81   // ThreadLocalTop may be only available during this call.
82   virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
83
84  protected:
85   virtual ~ThreadVisitor() {}
86 };
87
88
89 class ThreadManager {
90  public:
91   void Lock();
92   void Unlock();
93
94   void ArchiveThread();
95   bool RestoreThread();
96   void FreeThreadResources();
97   bool IsArchived();
98
99   void Iterate(ObjectVisitor* v);
100   void IterateArchivedThreads(ThreadVisitor* v);
101   bool IsLockedByCurrentThread() {
102     return mutex_owner_.Equals(ThreadId::Current());
103   }
104
105   ThreadId CurrentId();
106
107   void TerminateExecution(ThreadId thread_id);
108
109   // Iterate over in-use states.
110   ThreadState* FirstThreadStateInUse();
111   ThreadState* GetFreeThreadState();
112
113  private:
114   ThreadManager();
115   ~ThreadManager();
116
117   void EagerlyArchiveThread();
118
119   Mutex* mutex_;
120   ThreadId mutex_owner_;
121   ThreadId lazily_archived_thread_;
122   ThreadState* lazily_archived_thread_state_;
123
124   // In the following two lists there is always at least one object on the list.
125   // The first object is a flying anchor that is only there to simplify linking
126   // and unlinking.
127   // Head of linked list of free states.
128   ThreadState* free_anchor_;
129   // Head of linked list of states in use.
130   ThreadState* in_use_anchor_;
131
132   Isolate* isolate_;
133
134   friend class Isolate;
135   friend class ThreadState;
136 };
137
138
139 // The ContextSwitcher thread is used to schedule regular preemptions to
140 // multiple running V8 threads. Generally it is necessary to call
141 // StartPreemption if there is more than one thread running. If not, a single
142 // JavaScript can take full control of V8 and not allow other threads to run.
143 class ContextSwitcher: public Thread {
144  public:
145   // Set the preemption interval for the ContextSwitcher thread.
146   static void StartPreemption(int every_n_ms);
147
148   // Stop sending preemption requests to threads.
149   static void StopPreemption();
150
151   // Preempted thread needs to call back to the ContextSwitcher to acknowledge
152   // the handling of a preemption request.
153   static void PreemptionReceived();
154
155  private:
156   ContextSwitcher(Isolate* isolate, int every_n_ms);
157
158   Isolate* isolate() const { return isolate_; }
159
160   void Run();
161
162   bool keep_going_;
163   int sleep_ms_;
164   Isolate* isolate_;
165 };
166
167 } }  // namespace v8::internal
168
169 #endif  // V8_V8THREADS_H_