1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "partition_alloc/partition_alloc_base/threading/platform_thread.h"
6 
7 #include <windows.h>
8 
9 #include <cstddef>
10 
11 #include "partition_alloc/partition_alloc_base/time/time_override.h"
12 
13 namespace partition_alloc::internal::base {
14 
15 // static
CurrentId()16 PlatformThreadId PlatformThread::CurrentId() {
17   return ::GetCurrentThreadId();
18 }
19 
20 // static
CurrentRef()21 PlatformThreadRef PlatformThread::CurrentRef() {
22   return PlatformThreadRef(::GetCurrentThreadId());
23 }
24 
25 // static
CurrentHandle()26 PlatformThreadHandle PlatformThread::CurrentHandle() {
27   return PlatformThreadHandle(::GetCurrentThread());
28 }
29 
30 // static
Sleep(TimeDelta duration)31 void PlatformThread::Sleep(TimeDelta duration) {
32   // When measured with a high resolution clock, Sleep() sometimes returns much
33   // too early. We may need to call it repeatedly to get the desired duration.
34   // PlatformThread::Sleep doesn't support mock-time, so this always uses
35   // real-time.
36   const TimeTicks end = subtle::TimeTicksNowIgnoringOverride() + duration;
37   for (TimeTicks now = subtle::TimeTicksNowIgnoringOverride(); now < end;
38        now = subtle::TimeTicksNowIgnoringOverride()) {
39     ::Sleep(static_cast<DWORD>((end - now).InMillisecondsRoundedUp()));
40   }
41 }
42 
43 }  // namespace partition_alloc::internal::base
44