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

Side by Side Diff: runtime/vm/os_thread_win.cc

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: code-review-comments 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 #include "platform/globals.h" // NOLINT 5 #include "platform/globals.h" // NOLINT
6 #if defined(TARGET_OS_WINDOWS) 6 #if defined(TARGET_OS_WINDOWS)
7 7
8 #include "vm/growable_array.h" 8 #include "vm/growable_array.h"
9 #include "vm/os_thread.h" 9 #include "vm/os_thread.h"
10 10
11 #include <process.h> // NOLINT 11 #include <process.h> // NOLINT
12 12
13 #include "platform/assert.h" 13 #include "platform/assert.h"
14 14
15 namespace dart { 15 namespace dart {
16 16
17 // This flag is flipped by platform_win.cc when the process is exiting. 17 // This flag is flipped by platform_win.cc when the process is exiting.
18 // TODO(zra): Remove once VM shuts down cleanly. 18 // TODO(zra): Remove once VM shuts down cleanly.
19 bool private_flag_windows_run_tls_destructors = true; 19 bool private_flag_windows_run_tls_destructors = true;
20 20
21 class ThreadStartData { 21 class ThreadStartData {
22 public: 22 public:
23 ThreadStartData(OSThread::ThreadStartFunction function, uword parameter) 23 ThreadStartData(const char* name,
24 : function_(function), parameter_(parameter) {} 24 OSThread::ThreadStartFunction function,
25 uword parameter)
26 : name_(name), function_(function), parameter_(parameter) {}
25 27
28 const char* name() const { return name_; }
26 OSThread::ThreadStartFunction function() const { return function_; } 29 OSThread::ThreadStartFunction function() const { return function_; }
27 uword parameter() const { return parameter_; } 30 uword parameter() const { return parameter_; }
28 31
29 private: 32 private:
33 const char* name_;
30 OSThread::ThreadStartFunction function_; 34 OSThread::ThreadStartFunction function_;
31 uword parameter_; 35 uword parameter_;
32 36
33 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); 37 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
34 }; 38 };
35 39
36 40
37 // Dispatch to the thread start function provided by the caller. This trampoline 41 // Dispatch to the thread start function provided by the caller. This trampoline
38 // is used to ensure that the thread is properly destroyed if the thread just 42 // is used to ensure that the thread is properly destroyed if the thread just
39 // exits. 43 // exits.
40 static unsigned int __stdcall ThreadEntry(void* data_ptr) { 44 static unsigned int __stdcall ThreadEntry(void* data_ptr) {
41 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); 45 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
42 46
47 const char* name = data->name();
43 OSThread::ThreadStartFunction function = data->function(); 48 OSThread::ThreadStartFunction function = data->function();
44 uword parameter = data->parameter(); 49 uword parameter = data->parameter();
45 delete data; 50 delete data;
46 51
47 MonitorData::GetMonitorWaitDataForThread(); 52 MonitorData::GetMonitorWaitDataForThread();
48 53
54 // Create new OSThread object and set as TLS for new thread.
55 OSThread* thread = new OSThread();
56 OSThread::SetCurrent(thread);
57 thread->set_name(name);
58
49 // Call the supplied thread start function handing it its parameters. 59 // Call the supplied thread start function handing it its parameters.
50 function(parameter); 60 function(parameter);
51 61
52 // Clean up the monitor wait data for this thread. 62 // Clean up the monitor wait data for this thread.
53 MonitorWaitData::ThreadExit(); 63 MonitorWaitData::ThreadExit();
54 64
55 return 0; 65 return 0;
56 } 66 }
57 67
58 68
59 int OSThread::Start(ThreadStartFunction function, uword parameter) { 69 int OSThread::Start(const char* name,
60 ThreadStartData* start_data = new ThreadStartData(function, parameter); 70 ThreadStartFunction function,
71 uword parameter) {
72 ThreadStartData* start_data = new ThreadStartData(name, function, parameter);
61 uint32_t tid; 73 uint32_t tid;
62 uintptr_t thread = _beginthreadex(NULL, OSThread::GetMaxStackSize(), 74 uintptr_t thread = _beginthreadex(NULL, OSThread::GetMaxStackSize(),
63 ThreadEntry, start_data, 0, &tid); 75 ThreadEntry, start_data, 0, &tid);
64 if (thread == -1L || thread == 0) { 76 if (thread == -1L || thread == 0) {
65 #ifdef DEBUG 77 #ifdef DEBUG
66 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); 78 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno));
67 #endif 79 #endif
68 return errno; 80 return errno;
69 } 81 }
70 82
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 return ::GetCurrentThreadId(); 120 return ::GetCurrentThreadId();
109 } 121 }
110 122
111 123
112 ThreadId OSThread::GetCurrentThreadTraceId() { 124 ThreadId OSThread::GetCurrentThreadTraceId() {
113 return ::GetCurrentThreadId(); 125 return ::GetCurrentThreadId();
114 } 126 }
115 127
116 128
117 ThreadJoinId OSThread::GetCurrentThreadJoinId() { 129 ThreadJoinId OSThread::GetCurrentThreadJoinId() {
118 return ::GetCurrentThreadId(); 130 return ::GetCurrentThreadId();
zra 2015/11/16 20:12:28 Now that the join_id is stored on the OSThread in
siva 2015/11/17 20:52:25 I have added a TODO(zra) here.
119 } 131 }
120 132
121 133
122 void OSThread::Join(ThreadJoinId id) { 134 void OSThread::Join(ThreadJoinId id) {
123 HANDLE handle = OpenThread(SYNCHRONIZE, false, id); 135 HANDLE handle = OpenThread(SYNCHRONIZE, false, id);
124 136
125 // TODO(zra): OSThread::Start() closes the handle to the thread. Thus, by the 137 // TODO(zra): OSThread::Start() closes the handle to the thread. Thus, by the
126 // time we try to join the thread, its resources may have already been 138 // time we try to join the thread, its resources may have already been
127 // reclaimed, and joining will fail. This can be avoided in a couple of ways. 139 // reclaimed, and joining will fail. This can be avoided in a couple of ways.
zra 2015/11/16 20:12:28 Stuff after "and joining will fail." is stale afte
siva 2015/11/17 20:52:25 Will let you change this comment after you make th
128 // First, GetCurrentThreadJoinId could call OpenThread and return a handle. 140 // First, GetCurrentThreadJoinId could call OpenThread and return a handle.
129 // This is bad, because each of those handles would have to be closed. 141 // This is bad, because each of those handles would have to be closed.
130 // Second OSThread could be refactored to no longer be AllStatic. Then the 142 // Second OSThread could be refactored to no longer be AllStatic. Then the
131 // handle could be cached in the object by the Start method. 143 // handle could be cached in the object by the Start method.
132 if (handle == NULL) { 144 if (handle == NULL) {
133 return; 145 return;
134 } 146 }
135 147
136 DWORD res = WaitForSingleObject(handle, INFINITE); 148 DWORD res = WaitForSingleObject(handle, INFINITE);
137 CloseHandle(handle); 149 CloseHandle(handle);
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 #pragma data_seg(".CRT$XLB") 696 #pragma data_seg(".CRT$XLB")
685 PIMAGE_TLS_CALLBACK p_thread_callback_dart = OnDartThreadExit; 697 PIMAGE_TLS_CALLBACK p_thread_callback_dart = OnDartThreadExit;
686 698
687 // Reset the default section. 699 // Reset the default section.
688 #pragma data_seg() 700 #pragma data_seg()
689 701
690 #endif // _WIN64 702 #endif // _WIN64
691 } // extern "C" 703 } // extern "C"
692 704
693 #endif // defined(TARGET_OS_WINDOWS) 705 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698