1 //  waitable_timer.hpp  --------------------------------------------------------------//
2 
3 //  Copyright 2013 Andrey Semashev
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 
9 #ifndef BOOST_DETAIL_WINAPI_WAITABLE_TIMER_HPP
10 #define BOOST_DETAIL_WINAPI_WAITABLE_TIMER_HPP
11 
12 #include <boost/detail/winapi/basic_types.hpp>
13 
14 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #pragma once
16 #endif
17 
18 #if !defined( BOOST_USE_WINDOWS_H )
19 extern "C" {
20 typedef boost::detail::winapi::VOID_
21 (WINAPI *PTIMERAPCROUTINE)(
22     boost::detail::winapi::LPVOID_ lpArgToCompletionRoutine,
23     boost::detail::winapi::DWORD_ dwTimerLowValue,
24     boost::detail::winapi::DWORD_ dwTimerHighValue);
25 
26 #if !defined( BOOST_NO_ANSI_APIS )
27 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
28 CreateWaitableTimerA(
29     ::_SECURITY_ATTRIBUTES* lpTimerAttributes,
30     boost::detail::winapi::BOOL_ bManualReset,
31     boost::detail::winapi::LPCSTR_ lpTimerName);
32 
33 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
34 OpenWaitableTimerA(
35     boost::detail::winapi::DWORD_ dwDesiredAccess,
36     boost::detail::winapi::BOOL_ bInheritHandle,
37     boost::detail::winapi::LPCSTR_ lpTimerName);
38 #endif
39 
40 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
41 CreateWaitableTimerW(
42     ::_SECURITY_ATTRIBUTES* lpTimerAttributes,
43     boost::detail::winapi::BOOL_ bManualReset,
44     boost::detail::winapi::LPCWSTR_ lpTimerName);
45 
46 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
47 OpenWaitableTimerW(
48     boost::detail::winapi::DWORD_ dwDesiredAccess,
49     boost::detail::winapi::BOOL_ bInheritHandle,
50     boost::detail::winapi::LPCWSTR_ lpTimerName);
51 
52 BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
53 SetWaitableTimer(
54     boost::detail::winapi::HANDLE_ hTimer,
55     const ::_LARGE_INTEGER* lpDueTime,
56     boost::detail::winapi::LONG_ lPeriod,
57     PTIMERAPCROUTINE pfnCompletionRoutine,
58     boost::detail::winapi::LPVOID_ lpArgToCompletionRoutine,
59     boost::detail::winapi::BOOL_ fResume);
60 
61 BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
62 CancelWaitableTimer(boost::detail::winapi::HANDLE_ hTimer);
63 }
64 #endif
65 
66 namespace boost {
67 namespace detail {
68 namespace winapi {
69 
70 typedef ::PTIMERAPCROUTINE PTIMERAPCROUTINE_;
71 
72 #if !defined( BOOST_NO_ANSI_APIS )
73 using ::OpenWaitableTimerA;
74 #endif
75 using ::OpenWaitableTimerW;
76 using ::CancelWaitableTimer;
77 
78 #if defined( BOOST_USE_WINDOWS_H )
79 
80 const DWORD_ TIMER_ALL_ACCESS_ = TIMER_ALL_ACCESS;
81 const DWORD_ TIMER_MODIFY_STATE_ = TIMER_MODIFY_STATE;
82 const DWORD_ TIMER_QUERY_STATE_ = TIMER_QUERY_STATE;
83 
84 #else // defined( BOOST_USE_WINDOWS_H )
85 
86 const DWORD_ TIMER_ALL_ACCESS_ = 0x001F0003;
87 const DWORD_ TIMER_MODIFY_STATE_ = 0x00000002;
88 const DWORD_ TIMER_QUERY_STATE_ = 0x00000001;
89 
90 #endif // defined( BOOST_USE_WINDOWS_H )
91 
92 const DWORD_ timer_all_access = TIMER_ALL_ACCESS_;
93 const DWORD_ timer_modify_state = TIMER_MODIFY_STATE_;
94 const DWORD_ timer_query_state = TIMER_QUERY_STATE_;
95 
96 
97 #if !defined( BOOST_NO_ANSI_APIS )
CreateWaitableTimerA(PSECURITY_ATTRIBUTES_ lpTimerAttributes,BOOL_ bManualReset,LPCSTR_ lpTimerName)98 BOOST_FORCEINLINE HANDLE_ CreateWaitableTimerA(PSECURITY_ATTRIBUTES_ lpTimerAttributes, BOOL_ bManualReset, LPCSTR_ lpTimerName)
99 {
100     return ::CreateWaitableTimerA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, lpTimerName);
101 }
102 #endif
103 
CreateWaitableTimerW(PSECURITY_ATTRIBUTES_ lpTimerAttributes,BOOL_ bManualReset,LPCWSTR_ lpTimerName)104 BOOST_FORCEINLINE HANDLE_ CreateWaitableTimerW(PSECURITY_ATTRIBUTES_ lpTimerAttributes, BOOL_ bManualReset, LPCWSTR_ lpTimerName)
105 {
106     return ::CreateWaitableTimerW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, lpTimerName);
107 }
108 
SetWaitableTimer(HANDLE_ hTimer,const LARGE_INTEGER_ * lpDueTime,LONG_ lPeriod,PTIMERAPCROUTINE_ pfnCompletionRoutine,LPVOID_ lpArgToCompletionRoutine,BOOL_ fResume)109 BOOST_FORCEINLINE BOOL_ SetWaitableTimer(
110     HANDLE_ hTimer,
111     const LARGE_INTEGER_* lpDueTime,
112     LONG_ lPeriod,
113     PTIMERAPCROUTINE_ pfnCompletionRoutine,
114     LPVOID_ lpArgToCompletionRoutine,
115     BOOL_ fResume)
116 {
117     return ::SetWaitableTimer(hTimer, reinterpret_cast< const ::_LARGE_INTEGER* >(lpDueTime), lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, fResume);
118 }
119 
120 #if !defined( BOOST_NO_ANSI_APIS )
create_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes,BOOL_ bManualReset,LPCSTR_ lpTimerName)121 BOOST_FORCEINLINE HANDLE_ create_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes, BOOL_ bManualReset, LPCSTR_ lpTimerName)
122 {
123     return ::CreateWaitableTimerA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, lpTimerName);
124 }
125 #endif
126 
create_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes,BOOL_ bManualReset,LPCWSTR_ lpTimerName)127 BOOST_FORCEINLINE HANDLE_ create_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes, BOOL_ bManualReset, LPCWSTR_ lpTimerName)
128 {
129     return ::CreateWaitableTimerW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, lpTimerName);
130 }
131 
create_anonymous_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes,BOOL_ bManualReset)132 BOOST_FORCEINLINE HANDLE_ create_anonymous_waitable_timer(PSECURITY_ATTRIBUTES_ lpTimerAttributes, BOOL_ bManualReset)
133 {
134 #ifdef BOOST_NO_ANSI_APIS
135     return ::CreateWaitableTimerW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, 0);
136 #else
137     return ::CreateWaitableTimerA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpTimerAttributes), bManualReset, 0);
138 #endif
139 }
140 
141 }
142 }
143 }
144 
145 #endif // BOOST_DETAIL_WINAPI_WAITABLE_TIMER_HPP
146