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

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: Removed Thread::Join as suggested by iposva@ 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 class ThreadStartData { 13 class ThreadStartData {
14 public: 14 public:
15 ThreadStartData(Thread::ThreadStartFunction function, 15 ThreadStartData(Thread::ThreadStartFunction function,
16 uword parameter, 16 uword parameter,
17 Thread* thread) 17 bool joinable)
Ivan Posva 2012/01/24 02:04:01 joinable is a thing of the past.
Søren Gjesse 2012/01/24 12:07:55 Sorry, did not test this change on Windows.
18 : function_(function), parameter_(parameter), thread_(thread) {} 18 : function_(function), parameter_(parameter), joinable_(joinable) {}
19 19
20 Thread::ThreadStartFunction function() const { return function_; } 20 Thread::ThreadStartFunction function() const { return function_; }
21 uword parameter() const { return parameter_; } 21 uword parameter() const { return parameter_; }
22 Thread* thread() const { return thread_; }
23 22
24 private: 23 private:
25 Thread::ThreadStartFunction function_; 24 Thread::ThreadStartFunction function_;
26 uword parameter_; 25 uword parameter_;
27 Thread* thread_; 26 bool joinable_;
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 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(
54 function, parameter, (flags & kJoinable) != 0);
56 uint32_t tid; 55 uint32_t tid;
57 data_.thread_handle_ = 56 uintptr_t thread =
58 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); 57 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid);
59 if (data_.thread_handle_ == -1) { 58 if (thread == kInvalidThreadHandle) {
60 FATAL("Thread creation failed"); 59 FATAL("Thread creation failed");
61 } 60 }
62 data_.tid_ = tid;
63 } 61 }
64 62
65 63
66 Thread::~Thread() {
67 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_));
68 }
69
70
71 Mutex::Mutex() { 64 Mutex::Mutex() {
72 // Allocate unnamed semaphore with initial count 1 and max count 1. 65 // Allocate unnamed semaphore with initial count 1 and max count 1.
73 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); 66 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL);
74 if (data_.semaphore_ == NULL) { 67 if (data_.semaphore_ == NULL) {
75 FATAL("Mutex allocation failed"); 68 FATAL("Mutex allocation failed");
76 } 69 }
77 } 70 }
78 71
79 72
80 Mutex::~Mutex() { 73 Mutex::~Mutex() {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 void Monitor::Notify() { 156 void Monitor::Notify() {
164 WakeConditionVariable(&data_.cond_); 157 WakeConditionVariable(&data_.cond_);
165 } 158 }
166 159
167 160
168 void Monitor::NotifyAll() { 161 void Monitor::NotifyAll() {
169 WakeAllConditionVariable(&data_.cond_); 162 WakeAllConditionVariable(&data_.cond_);
170 } 163 }
171 164
172 } // namespace dart 165 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698