1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/waitable_timer_impl.h"
16 :
17 : #include <windows.h>
18 :
19 : #include "base/time/time.h"
20 : #include "syzygy/common/com_utils.h"
21 :
22 m : namespace kasko {
23 :
24 : // static
25 m : std::unique_ptr<WaitableTimer> WaitableTimerImpl::Create(
26 m : const base::TimeDelta& interval) {
27 m : std::unique_ptr<WaitableTimer> result;
28 m : base::win::ScopedHandle handle(::CreateWaitableTimer(NULL, TRUE, NULL));
29 m : if (handle.IsValid())
30 m : result.reset(new WaitableTimerImpl(std::move(handle), interval));
31 m : return std::move(result);
32 m : }
33 :
34 m : WaitableTimerImpl::~WaitableTimerImpl() {}
35 :
36 m : void WaitableTimerImpl::Start() {
37 m : if (!::SetWaitableTimer(handle_.Get(), &interval_, 0, NULL, NULL, FALSE))
38 m : LOG(ERROR) << "Unexpected failure to set a timer: " << ::common::LogWe();
39 m : }
40 :
41 m : HANDLE WaitableTimerImpl::GetHANDLE() {
42 m : return handle_.Get();
43 m : }
44 :
45 m : WaitableTimerImpl::WaitableTimerImpl(base::win::ScopedHandle handle,
46 m : const base::TimeDelta& interval)
47 m : : handle_(handle.Take()), interval_() {
48 m : interval_.QuadPart = (-interval.InMicroseconds()) *
49 m : (base::Time::kNanosecondsPerMicrosecond / 100);
50 m : }
51 :
52 m : } // namespace kasko
|