blob: db0ed070fae1f3ee6265e26ffa53479c79690751 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_V8THREADS_H_
6#define V8_V8THREADS_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/isolate.h"
9
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
13
14class ThreadState {
15 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000016 // Returns NULL after the last one.
17 ThreadState* Next();
18
19 enum List {FREE_LIST, IN_USE_LIST};
20
21 void LinkInto(List list);
22 void Unlink();
23
Steve Blocka7e24c12009-10-30 11:49:00 +000024 // Id of thread.
Ben Murdoch8b112d22011-06-08 16:22:53 +010025 void set_id(ThreadId id) { id_ = id; }
26 ThreadId id() { return id_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000027
28 // Should the thread be terminated when it is restored?
29 bool terminate_on_restore() { return terminate_on_restore_; }
30 void set_terminate_on_restore(bool terminate_on_restore) {
31 terminate_on_restore_ = terminate_on_restore;
32 }
33
34 // Get data area for archiving a thread.
35 char* data() { return data_; }
Ben Murdoch589d6972011-11-30 16:04:58 +000036
Steve Blocka7e24c12009-10-30 11:49:00 +000037 private:
Steve Block44f0eee2011-05-26 01:26:41 +010038 explicit ThreadState(ThreadManager* thread_manager);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 ~ThreadState();
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41 void AllocateSpace();
42
Ben Murdoch8b112d22011-06-08 16:22:53 +010043 ThreadId id_;
Steve Blocka7e24c12009-10-30 11:49:00 +000044 bool terminate_on_restore_;
45 char* data_;
46 ThreadState* next_;
47 ThreadState* previous_;
48
Steve Block44f0eee2011-05-26 01:26:41 +010049 ThreadManager* thread_manager_;
50
51 friend class ThreadManager;
Steve Blocka7e24c12009-10-30 11:49:00 +000052};
53
54
Ben Murdoch692be652012-01-10 18:47:50 +000055// Defined in isolate.h.
Steve Block6ded16b2010-05-10 14:33:55 +010056class ThreadLocalTop;
57
58
59class ThreadVisitor {
60 public:
61 // ThreadLocalTop may be only available during this call.
Ben Murdoch8b112d22011-06-08 16:22:53 +010062 virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
Steve Block6ded16b2010-05-10 14:33:55 +010063
64 protected:
65 virtual ~ThreadVisitor() {}
66};
67
68
Steve Block44f0eee2011-05-26 01:26:41 +010069class ThreadManager {
Steve Blocka7e24c12009-10-30 11:49:00 +000070 public:
Steve Block44f0eee2011-05-26 01:26:41 +010071 void Lock();
72 void Unlock();
Steve Blocka7e24c12009-10-30 11:49:00 +000073
Steve Block44f0eee2011-05-26 01:26:41 +010074 void ArchiveThread();
75 bool RestoreThread();
76 void FreeThreadResources();
77 bool IsArchived();
Steve Blocka7e24c12009-10-30 11:49:00 +000078
Steve Block44f0eee2011-05-26 01:26:41 +010079 void Iterate(ObjectVisitor* v);
80 void IterateArchivedThreads(ThreadVisitor* v);
Ben Murdoch8b112d22011-06-08 16:22:53 +010081 bool IsLockedByCurrentThread() {
82 return mutex_owner_.Equals(ThreadId::Current());
83 }
Steve Blocka7e24c12009-10-30 11:49:00 +000084
Ben Murdoch8b112d22011-06-08 16:22:53 +010085 ThreadId CurrentId();
Steve Blocka7e24c12009-10-30 11:49:00 +000086
Ben Murdoch8b112d22011-06-08 16:22:53 +010087 void TerminateExecution(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +010088
89 // Iterate over in-use states.
90 ThreadState* FirstThreadStateInUse();
91 ThreadState* GetFreeThreadState();
Steve Blocka7e24c12009-10-30 11:49:00 +000092
Steve Blocka7e24c12009-10-30 11:49:00 +000093 private:
Steve Block44f0eee2011-05-26 01:26:41 +010094 ThreadManager();
95 ~ThreadManager();
Steve Blocka7e24c12009-10-30 11:49:00 +000096
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 void DeleteThreadStateList(ThreadState* anchor);
98
Steve Block44f0eee2011-05-26 01:26:41 +010099 void EagerlyArchiveThread();
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 base::Mutex mutex_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100102 ThreadId mutex_owner_;
103 ThreadId lazily_archived_thread_;
Steve Block44f0eee2011-05-26 01:26:41 +0100104 ThreadState* lazily_archived_thread_state_;
105
106 // In the following two lists there is always at least one object on the list.
107 // The first object is a flying anchor that is only there to simplify linking
108 // and unlinking.
109 // Head of linked list of free states.
110 ThreadState* free_anchor_;
111 // Head of linked list of states in use.
112 ThreadState* in_use_anchor_;
113
114 Isolate* isolate_;
115
116 friend class Isolate;
117 friend class ThreadState;
Steve Blocka7e24c12009-10-30 11:49:00 +0000118};
119
120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121} // namespace internal
122} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
124#endif // V8_V8THREADS_H_