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

Side by Side Diff: runtime/platform/thread_win.cc

Issue 9141005: Change the thread interface in runtime/platform and use it starting all threads (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from ager@ Created 8 years, 11 months 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 | Annotate | Revision Log
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/thread.h" 5 #include "platform/thread.h"
6 6
7 #include <process.h> 7 #include <process.h>
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 10
11 namespace dart { 11 namespace dart {
12 12
13 const ThreadHandle Thread::kInvalidThreadHandle = 0;
14
13 class ThreadStartData { 15 class ThreadStartData {
14 public: 16 public:
15 ThreadStartData(Thread::ThreadStartFunction function, 17 ThreadStartData(Thread::ThreadStartFunction function,
16 uword parameter, 18 uword parameter)
17 Thread* thread) 19 : function_(function), parameter_(parameter) {}
18 : function_(function), parameter_(parameter), thread_(thread) {}
19 20
20 Thread::ThreadStartFunction function() const { return function_; } 21 Thread::ThreadStartFunction function() const { return function_; }
21 uword parameter() const { return parameter_; } 22 uword parameter() const { return parameter_; }
22 Thread* thread() const { return thread_; }
23 23
24 private: 24 private:
25 Thread::ThreadStartFunction function_; 25 Thread::ThreadStartFunction function_;
26 uword parameter_; 26 uword parameter_;
27 Thread* thread_;
28 27
29 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); 28 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
30 }; 29 };
31 30
32 31
33 // Dispatch to the thread start function provided by the caller. This trampoline 32 // Dispatch to the thread start function provided by the caller. This trampoline
34 // is used to ensure that the thread is properly destroyed if the thread just 33 // is used to ensure that the thread is properly destroyed if the thread just
35 // exits. 34 // exits.
36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { 35 static unsigned int __stdcall ThreadEntry(void* data_ptr) {
37 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); 36 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
38 37
39 Thread::ThreadStartFunction function = data->function(); 38 Thread::ThreadStartFunction function = data->function();
40 uword parameter = data->parameter(); 39 uword parameter = data->parameter();
41 Thread* thread = data->thread();
42 delete data; 40 delete data;
43 41
44 // Call the supplied thread start function handing it its parameters. 42 // Call the supplied thread start function handing it its parameters.
45 function(parameter); 43 function(parameter);
46 44
47 // When the function returns here, make sure that the thread is deleted. 45 // When the function returns here, make sure to close the handle.
48 delete thread; 46 CloseHandle(GetCurrentThread());
49 47
50 return 0; 48 return 0;
51 } 49 }
52 50
53 51
54 Thread::Thread(ThreadStartFunction function, uword parameter) { 52 ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) {
55 ThreadStartData* start_data = new ThreadStartData(function, parameter, this); 53 ThreadStartData* start_data = new ThreadStartData(function, parameter);
56 uint32_t tid; 54 uint32_t tid;
57 data_.thread_handle_ = 55 ThreadHandle thread =
58 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); 56 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid);
59 if (data_.thread_handle_ == -1) { 57 if (thread == kInvalidThreadHandle) {
60 FATAL("Thread creation failed"); 58 FATAL("Thread creation failed");
61 } 59 }
62 data_.tid_ = tid; 60 return thread;
63 } 61 }
64 62
65 63
66 Thread::~Thread() { 64 void Thread::Join(ThreadHandle thread) {
67 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); 65 DWORD result = WaitForSingleObject(reinterpret_cast<HANDLE>(thread),
66 INFINITE);
67 if (result != WAIT_OBJECT_0) {
68 FATAL("Thread join failed");
69 }
68 } 70 }
69 71
70 72
71 Mutex::Mutex() { 73 Mutex::Mutex() {
72 // Allocate unnamed semaphore with initial count 1 and max count 1. 74 // Allocate unnamed semaphore with initial count 1 and max count 1.
73 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); 75 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL);
74 if (data_.semaphore_ == NULL) { 76 if (data_.semaphore_ == NULL) {
75 FATAL("Mutex allocation failed"); 77 FATAL("Mutex allocation failed");
76 } 78 }
77 } 79 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 void Monitor::Notify() { 165 void Monitor::Notify() {
164 WakeConditionVariable(&data_.cond_); 166 WakeConditionVariable(&data_.cond_);
165 } 167 }
166 168
167 169
168 void Monitor::NotifyAll() { 170 void Monitor::NotifyAll() {
169 WakeAllConditionVariable(&data_.cond_); 171 WakeAllConditionVariable(&data_.cond_);
170 } 172 }
171 173
172 } // namespace dart 174 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698