1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #include "base/platform_thread.h"
9 #include "base/logging.h"
10 #include "base/win_util.h"
12 #include "nsThreadUtils.h"
16 DWORD __stdcall
ThreadFunc(void* closure
) {
17 PlatformThread::Delegate
* delegate
=
18 static_cast<PlatformThread::Delegate
*>(closure
);
19 delegate
->ThreadMain();
26 PlatformThreadId
PlatformThread::CurrentId() { return GetCurrentThreadId(); }
29 void PlatformThread::YieldCurrentThread() { ::Sleep(0); }
32 void PlatformThread::Sleep(int duration_ms
) { ::Sleep(duration_ms
); }
35 void PlatformThread::SetName(const char* name
) {
36 // Using NS_SetCurrentThreadName, as opposed to using platform APIs directly,
37 // also sets the thread name on the PRThread wrapper, and allows us to
38 // retrieve it using PR_GetThreadName.
39 NS_SetCurrentThreadName(name
);
43 bool PlatformThread::Create(size_t stack_size
, Delegate
* delegate
,
44 PlatformThreadHandle
* thread_handle
) {
45 unsigned int flags
= 0;
47 flags
= STACK_SIZE_PARAM_IS_A_RESERVATION
;
52 // Using CreateThread here vs _beginthreadex makes thread creation a bit
53 // faster and doesn't require the loader lock to be available. Our code will
54 // have to work running on CreateThread() threads anyway, since we run code
55 // on the Windows thread pool, etc. For some background on the difference:
56 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
58 CreateThread(NULL
, stack_size
, ThreadFunc
, delegate
, flags
, NULL
);
59 return *thread_handle
!= NULL
;
63 bool PlatformThread::CreateNonJoinable(size_t stack_size
, Delegate
* delegate
) {
64 PlatformThreadHandle thread_handle
;
65 bool result
= Create(stack_size
, delegate
, &thread_handle
);
66 CloseHandle(thread_handle
);
71 void PlatformThread::Join(PlatformThreadHandle thread_handle
) {
72 DCHECK(thread_handle
);
74 // Wait for the thread to exit. It should already have terminated but make
75 // sure this assumption is valid.
76 DWORD result
= WaitForSingleObject(thread_handle
, INFINITE
);
77 DCHECK_EQ(WAIT_OBJECT_0
, result
);
79 CloseHandle(thread_handle
);