Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: runtime/vm/os_thread.h

Issue 1439483003: - Add an OSThread structure which is the generic TLS structure for all C++ (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: tweak-comment Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef VM_OS_THREAD_H_ 5 #ifndef VM_OS_THREAD_H_
6 #define VM_OS_THREAD_H_ 6 #define VM_OS_THREAD_H_
7 7
8 #include "platform/globals.h" 8 #include "platform/globals.h"
9 #include "vm/allocation.h"
9 #include "vm/globals.h" 10 #include "vm/globals.h"
10 11
11 // Declare the OS-specific types ahead of defining the generic classes. 12 // Declare the OS-specific types ahead of defining the generic classes.
12 #if defined(TARGET_OS_ANDROID) 13 #if defined(TARGET_OS_ANDROID)
13 #include "vm/os_thread_android.h" 14 #include "vm/os_thread_android.h"
14 #elif defined(TARGET_OS_LINUX) 15 #elif defined(TARGET_OS_LINUX)
15 #include "vm/os_thread_linux.h" 16 #include "vm/os_thread_linux.h"
16 #elif defined(TARGET_OS_MACOS) 17 #elif defined(TARGET_OS_MACOS)
17 #include "vm/os_thread_macos.h" 18 #include "vm/os_thread_macos.h"
18 #elif defined(TARGET_OS_WINDOWS) 19 #elif defined(TARGET_OS_WINDOWS)
19 #include "vm/os_thread_win.h" 20 #include "vm/os_thread_win.h"
20 #else 21 #else
21 #error Unknown target os. 22 #error Unknown target os.
22 #endif 23 #endif
23 24
24 namespace dart { 25 namespace dart {
25 26
27 // Forward declarations.
28 class Log;
29 class Mutex;
30 class Thread;
31 class TimelineEventBlock;
32
33 class BaseThread {
34 public:
35 bool is_os_thread() const { return is_os_thread_; }
36
37 private:
38 explicit BaseThread(bool is_os_thread) : is_os_thread_(is_os_thread) {}
39 ~BaseThread() {}
40
41 bool is_os_thread_;
42
43 friend class Thread;
44 friend class OSThread;
45
46 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseThread);
47 };
48
49
26 // Low-level operations on OS platform threads. 50 // Low-level operations on OS platform threads.
27 // TODO(koda): Move to runtime/platform. 51 class OSThread : public BaseThread {
28 class OSThread {
29 public: 52 public:
30 static ThreadLocalKey kUnsetThreadLocalKey; 53 OSThread();
31 static ThreadId kInvalidThreadId; 54 ~OSThread();
32 static ThreadJoinId kInvalidThreadJoinId; 55
56 ThreadId id() const {
57 ASSERT(id_ != OSThread::kInvalidThreadId);
58 return id_;
59 }
60
61 ThreadId join_id() const {
62 ASSERT(join_id_ != OSThread::kInvalidThreadJoinId);
63 return join_id_;
64 }
65
66 ThreadId trace_id() const {
67 ASSERT(trace_id_ != OSThread::kInvalidThreadJoinId);
68 return trace_id_;
69 }
70
71 const char* name() const {
72 return name_;
73 }
74
75 void set_name(const char* name) {
76 ASSERT(OSThread::Current() == this);
77 ASSERT(name_ == NULL);
78 name_ = name;
79 }
80
81 Mutex* timeline_block_lock() const {
82 return timeline_block_lock_;
83 }
84
85 // Only safe to access when holding |timeline_block_lock_|.
86 TimelineEventBlock* timeline_block() const {
87 return timeline_block_;
88 }
89
90 // Only safe to access when holding |timeline_block_lock_|.
91 void set_timeline_block(TimelineEventBlock* block) {
92 timeline_block_ = block;
93 }
94
95 Log* log() const { return log_; }
96
97 uword stack_base() const { return stack_base_; }
98 void set_stack_base(uword stack_base) { stack_base_ = stack_base; }
99
100 // Retrieve the stack address bounds for profiler.
101 bool GetProfilerStackBounds(uword* lower, uword* upper) const {
102 uword stack_upper = stack_base_;
103 if (stack_upper == 0) {
104 return false;
105 }
106 uword stack_lower = stack_upper - GetSpecifiedStackSize();
107 *lower = stack_lower;
108 *upper = stack_upper;
109 return true;
110 }
111
112 // Used to temporarily disable or enable thread interrupts.
113 void DisableThreadInterrupts();
114 void EnableThreadInterrupts();
115 bool ThreadInterruptsEnabled();
116
117 // The currently executing thread, or NULL if not yet initialized.
118 static OSThread* Current() {
119 BaseThread* thread = GetCurrentTLS();
120 OSThread* os_thread = NULL;
121 if (thread != NULL) {
122 if (thread->is_os_thread()) {
123 os_thread = reinterpret_cast<OSThread*>(thread);
124 } else {
125 Thread* vm_thread = reinterpret_cast<Thread*>(thread);
126 os_thread = GetOSThreadFromThread(vm_thread);
127 }
128 }
129 return os_thread;
130 }
131 static void SetCurrent(OSThread* current);
132
133 // TODO(5411455): Use flag to override default value and Validate the
134 // stack size by querying OS.
135 static uword GetSpecifiedStackSize() {
136 ASSERT(OSThread::kStackSizeBuffer < OSThread::GetMaxStackSize());
137 uword stack_size = OSThread::GetMaxStackSize() - OSThread::kStackSizeBuffer;
138 return stack_size;
139 }
33 140
34 typedef void (*ThreadStartFunction) (uword parameter); 141 typedef void (*ThreadStartFunction) (uword parameter);
35 typedef void (*ThreadDestructor) (void* parameter); 142 typedef void (*ThreadDestructor) (void* parameter);
36 143
37 // Start a thread running the specified function. Returns 0 if the 144 // Start a thread running the specified function. Returns 0 if the
38 // thread started successfuly and a system specific error code if 145 // thread started successfuly and a system specific error code if
39 // the thread failed to start. 146 // the thread failed to start.
40 static int Start(ThreadStartFunction function, uword parameters); 147 static int Start(
148 const char* name, ThreadStartFunction function, uword parameter);
41 149
42 static ThreadLocalKey CreateThreadLocal(ThreadDestructor destructor = NULL); 150 static ThreadLocalKey CreateThreadLocal(ThreadDestructor destructor = NULL);
43 static void DeleteThreadLocal(ThreadLocalKey key); 151 static void DeleteThreadLocal(ThreadLocalKey key);
44 static uword GetThreadLocal(ThreadLocalKey key) { 152 static uword GetThreadLocal(ThreadLocalKey key) {
45 return ThreadInlineImpl::GetThreadLocal(key); 153 return ThreadInlineImpl::GetThreadLocal(key);
46 } 154 }
47 static void SetThreadLocal(ThreadLocalKey key, uword value); 155 static void SetThreadLocal(ThreadLocalKey key, uword value);
48 static intptr_t GetMaxStackSize(); 156 static intptr_t GetMaxStackSize();
49 static ThreadId GetCurrentThreadId(); 157 static ThreadId GetCurrentThreadId();
50 static ThreadId GetCurrentThreadTraceId(); 158 static ThreadId GetCurrentThreadTraceId();
51 static intptr_t CurrentCurrentThreadIdAsIntPtr() { 159 static intptr_t CurrentCurrentThreadIdAsIntPtr() {
52 return ThreadIdToIntPtr(GetCurrentThreadId()); 160 return ThreadIdToIntPtr(GetCurrentThreadId());
53 } 161 }
54 static ThreadJoinId GetCurrentThreadJoinId(); 162 static ThreadJoinId GetCurrentThreadJoinId();
55 static void Join(ThreadJoinId id); 163 static void Join(ThreadJoinId id);
56 static intptr_t ThreadIdToIntPtr(ThreadId id); 164 static intptr_t ThreadIdToIntPtr(ThreadId id);
57 static ThreadId ThreadIdFromIntPtr(intptr_t id); 165 static ThreadId ThreadIdFromIntPtr(intptr_t id);
58 static bool Compare(ThreadId a, ThreadId b); 166 static bool Compare(ThreadId a, ThreadId b);
59 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); 167 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage);
168
169 // Called at VM startup and shutdown.
170 static void InitOnce();
171 static void Cleanup();
172
173 static bool IsThreadInList(ThreadId join_id);
174
175 static const intptr_t kStackSizeBuffer = (4 * KB * kWordSize);
176
177 static ThreadLocalKey kUnsetThreadLocalKey;
178 static ThreadId kInvalidThreadId;
179 static ThreadJoinId kInvalidThreadJoinId;
180
181 private:
182 static BaseThread* GetCurrentTLS() {
183 return reinterpret_cast<BaseThread*>(OSThread::GetThreadLocal(thread_key_));
184 }
185 static OSThread* GetOSThreadFromThread(Thread* thread);
186 static void AddThreadToList(OSThread* thread);
187 static void RemoveThreadFromList(OSThread* thread);
188
189 static ThreadLocalKey thread_key_;
190
191 const ThreadId id_;
192 const ThreadId join_id_;
193 const ThreadId trace_id_; // Used to interface with tracing tools.
194 const char* name_; // A name for this thread.
195
196 Mutex* timeline_block_lock_;
197 TimelineEventBlock* timeline_block_;
198
199 // All |Thread|s are registered in the thread list.
200 OSThread* thread_list_next_;
201
202 uintptr_t thread_interrupt_disabled_;
203 Log* log_;
204
205 uword stack_base_;
206
207 static OSThread* thread_list_head_;
208 static Mutex* thread_list_lock_;
209
210 friend class Thread;
211 friend class OSThreadIterator;
60 }; 212 };
61 213
62 214
215 // Note that this takes the thread list lock, prohibiting threads from coming
216 // on- or off-line.
217 class OSThreadIterator : public ValueObject {
218 public:
219 OSThreadIterator();
220 ~OSThreadIterator();
221
222 // Returns false when there are no more threads left.
223 bool HasNext() const;
224
225 // Returns the current thread and moves forward.
226 OSThread* Next();
227
228 private:
229 OSThread* next_;
230 };
231
232
63 class Mutex { 233 class Mutex {
64 public: 234 public:
65 Mutex(); 235 Mutex();
66 ~Mutex(); 236 ~Mutex();
67 237
68 void Lock(); 238 void Lock();
69 bool TryLock(); 239 bool TryLock();
70 void Unlock(); 240 void Unlock();
71 241
72 #if defined(DEBUG) 242 #if defined(DEBUG)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 #endif // defined(DEBUG) 301 #endif // defined(DEBUG)
132 302
133 DISALLOW_COPY_AND_ASSIGN(Monitor); 303 DISALLOW_COPY_AND_ASSIGN(Monitor);
134 }; 304 };
135 305
136 306
137 } // namespace dart 307 } // namespace dart
138 308
139 309
140 #endif // VM_OS_THREAD_H_ 310 #endif // VM_OS_THREAD_H_
OLDNEW
« no previous file with comments | « runtime/vm/native_api_impl.cc ('k') | runtime/vm/os_thread.cc » ('j') | runtime/vm/os_thread.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698