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

Side by Side Diff: base/platform_thread_win.cc

Issue 5242002: Reland 66791 (change was innocent)... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 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 | Annotate | Revision Log
« no previous file with comments | « base/platform_thread_posix.cc ('k') | base/singleton.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/platform_thread.h" 5 #include "base/platform_thread.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/thread_restrictions.h"
8 #include "base/win/windows_version.h" 9 #include "base/win/windows_version.h"
9 10
10 namespace { 11 namespace {
11 12
12 // The information on how to set the thread name comes from 13 // The information on how to set the thread name comes from
13 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx 14 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
14 const DWORD kVCThreadNameException = 0x406D1388; 15 const DWORD kVCThreadNameException = 0x406D1388;
15 16
16 typedef struct tagTHREADNAME_INFO { 17 typedef struct tagTHREADNAME_INFO {
17 DWORD dwType; // Must be 0x1000. 18 DWORD dwType; // Must be 0x1000.
18 LPCSTR szName; // Pointer to name (in user addr space). 19 LPCSTR szName; // Pointer to name (in user addr space).
19 DWORD dwThreadID; // Thread ID (-1=caller thread). 20 DWORD dwThreadID; // Thread ID (-1=caller thread).
20 DWORD dwFlags; // Reserved for future use, must be zero. 21 DWORD dwFlags; // Reserved for future use, must be zero.
21 } THREADNAME_INFO; 22 } THREADNAME_INFO;
22 23
23 DWORD __stdcall ThreadFunc(void* closure) { 24 struct ThreadParams {
24 PlatformThread::Delegate* delegate = 25 PlatformThread::Delegate* delegate;
25 static_cast<PlatformThread::Delegate*>(closure); 26 bool joinable;
27 };
28
29 DWORD __stdcall ThreadFunc(void* params) {
30 ThreadParams* thread_params = static_cast<ThreadParams*>(params);
31 PlatformThread::Delegate* delegate = thread_params->delegate;
32 if (!thread_params->joinable)
33 base::ThreadRestrictions::SetSingletonAllowed(false);
34 delete thread_params;
26 delegate->ThreadMain(); 35 delegate->ThreadMain();
27 return NULL; 36 return NULL;
28 } 37 }
29 38
39 // CreateThreadInternal() matches PlatformThread::Create(), except that
40 // |out_thread_handle| may be NULL, in which case a non-joinable thread is
41 // created.
42 bool CreateThreadInternal(size_t stack_size,
43 PlatformThread::Delegate* delegate,
44 PlatformThreadHandle* out_thread_handle) {
45 PlatformThreadHandle thread_handle;
46 unsigned int flags = 0;
47 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) {
48 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
49 } else {
50 stack_size = 0;
51 }
52
53 ThreadParams* params = new ThreadParams;
54 params->delegate = delegate;
55 params->joinable = out_thread_handle != NULL;
56
57 // Using CreateThread here vs _beginthreadex makes thread creation a bit
58 // faster and doesn't require the loader lock to be available. Our code will
59 // have to work running on CreateThread() threads anyway, since we run code
60 // on the Windows thread pool, etc. For some background on the difference:
61 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
62 thread_handle = CreateThread(
63 NULL, stack_size, ThreadFunc, params, flags, NULL);
64 if (!thread_handle) {
65 delete params;
66 return false;
67 }
68
69 if (out_thread_handle)
70 *out_thread_handle = thread_handle;
71 else
72 CloseHandle(thread_handle);
73 return true;
74 }
75
30 } // namespace 76 } // namespace
31 77
32 // static 78 // static
33 PlatformThreadId PlatformThread::CurrentId() { 79 PlatformThreadId PlatformThread::CurrentId() {
34 return GetCurrentThreadId(); 80 return GetCurrentThreadId();
35 } 81 }
36 82
37 // static 83 // static
38 void PlatformThread::YieldCurrentThread() { 84 void PlatformThread::YieldCurrentThread() {
39 ::Sleep(0); 85 ::Sleep(0);
(...skipping 20 matching lines...) Expand all
60 __try { 106 __try {
61 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), 107 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
62 reinterpret_cast<DWORD_PTR*>(&info)); 108 reinterpret_cast<DWORD_PTR*>(&info));
63 } __except(EXCEPTION_CONTINUE_EXECUTION) { 109 } __except(EXCEPTION_CONTINUE_EXECUTION) {
64 } 110 }
65 } 111 }
66 112
67 // static 113 // static
68 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, 114 bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
69 PlatformThreadHandle* thread_handle) { 115 PlatformThreadHandle* thread_handle) {
70 unsigned int flags = 0; 116 DCHECK(thread_handle);
71 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { 117 return CreateThreadInternal(stack_size, delegate, thread_handle);
72 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
73 } else {
74 stack_size = 0;
75 }
76
77 // Using CreateThread here vs _beginthreadex makes thread creation a bit
78 // faster and doesn't require the loader lock to be available. Our code will
79 // have to work running on CreateThread() threads anyway, since we run code
80 // on the Windows thread pool, etc. For some background on the difference:
81 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
82 *thread_handle = CreateThread(
83 NULL, stack_size, ThreadFunc, delegate, flags, NULL);
84 return *thread_handle != NULL;
85 } 118 }
86 119
87 // static 120 // static
88 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { 121 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
89 PlatformThreadHandle thread_handle; 122 return CreateThreadInternal(stack_size, delegate, NULL);
90 bool result = Create(stack_size, delegate, &thread_handle);
91 CloseHandle(thread_handle);
92 return result;
93 } 123 }
94 124
95 // static 125 // static
96 void PlatformThread::Join(PlatformThreadHandle thread_handle) { 126 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
97 DCHECK(thread_handle); 127 DCHECK(thread_handle);
98 128
99 // Wait for the thread to exit. It should already have terminated but make 129 // Wait for the thread to exit. It should already have terminated but make
100 // sure this assumption is valid. 130 // sure this assumption is valid.
101 DWORD result = WaitForSingleObject(thread_handle, INFINITE); 131 DWORD result = WaitForSingleObject(thread_handle, INFINITE);
102 DCHECK_EQ(WAIT_OBJECT_0, result); 132 DCHECK_EQ(WAIT_OBJECT_0, result);
103 133
104 CloseHandle(thread_handle); 134 CloseHandle(thread_handle);
105 } 135 }
OLDNEW
« no previous file with comments | « base/platform_thread_posix.cc ('k') | base/singleton.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698