cee64322b9d6e0ddd3e8e43c11c6ef90020d47be
[reactos.git] / reactos / lib / kernel32 / synch / timer.c
1 /* $Id: timer.c,v 1.12 2003/01/15 21:24:36 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/kernel32/synch/timer.c
6 * PURPOSE: Implementing timer
7 * PROGRAMMER:
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <k32.h>
13
14 #define NDEBUG
15 #include <kernel32/kernel32.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19 HANDLE STDCALL
20 CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
21 WINBOOL bManualReset,
22 LPWSTR lpTimerName)
23 {
24 NTSTATUS Status;
25 HANDLE TimerHandle;
26 OBJECT_ATTRIBUTES ObjectAttributes;
27 UNICODE_STRING UnicodeName;
28 ULONG TimerType;
29
30 if (bManualReset)
31 TimerType = NotificationTimer;
32 else
33 TimerType = SynchronizationTimer;
34
35 RtlInitUnicodeString(&UnicodeName, lpTimerName);
36 InitializeObjectAttributes(&ObjectAttributes,
37 &UnicodeName,
38 0,
39 hBaseDir,
40 NULL);
41
42 Status = NtCreateTimer(&TimerHandle,
43 TIMER_ALL_ACCESS,
44 &ObjectAttributes,
45 TimerType);
46 if (!NT_SUCCESS(Status))
47 {
48 SetLastErrorByStatus(Status);
49 return NULL;
50 }
51
52 return TimerHandle;
53 }
54
55
56 HANDLE STDCALL
57 CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
58 WINBOOL bManualReset,
59 LPCSTR lpTimerName)
60 {
61 UNICODE_STRING TimerNameU;
62 ANSI_STRING TimerName;
63 HANDLE TimerHandle;
64
65 RtlInitAnsiString (&TimerName,
66 (LPSTR)lpTimerName);
67 RtlAnsiStringToUnicodeString (&TimerNameU,
68 &TimerName,
69 TRUE);
70
71 TimerHandle = CreateWaitableTimerW (lpTimerAttributes,
72 bManualReset,
73 TimerNameU.Buffer);
74
75 RtlFreeUnicodeString (&TimerNameU);
76
77 return TimerHandle;
78 }
79
80
81 HANDLE STDCALL
82 OpenWaitableTimerW(DWORD dwDesiredAccess,
83 WINBOOL bInheritHandle,
84 LPCWSTR lpTimerName)
85 {
86 NTSTATUS Status;
87 HANDLE TimerHandle;
88 OBJECT_ATTRIBUTES ObjectAttributes;
89 UNICODE_STRING UnicodeName;
90 ULONG Attributes = 0;
91
92 if (bInheritHandle)
93 {
94 Attributes = OBJ_INHERIT;
95 }
96
97 RtlInitUnicodeString(&UnicodeName,
98 lpTimerName);
99 InitializeObjectAttributes(&ObjectAttributes,
100 &UnicodeName,
101 Attributes,
102 hBaseDir,
103 NULL);
104
105 Status = NtOpenTimer(&TimerHandle,
106 dwDesiredAccess,
107 &ObjectAttributes);
108 if (!NT_SUCCESS(Status))
109 {
110 SetLastErrorByStatus(Status);
111 return NULL;
112 }
113
114 return TimerHandle;
115 }
116
117
118 HANDLE STDCALL
119 OpenWaitableTimerA(DWORD dwDesiredAccess,
120 WINBOOL bInheritHandle,
121 LPCSTR lpTimerName)
122 {
123 UNICODE_STRING TimerNameU;
124 ANSI_STRING TimerName;
125 HANDLE TimerHandle;
126
127 RtlInitAnsiString (&TimerName,
128 (LPSTR)lpTimerName);
129 RtlAnsiStringToUnicodeString (&TimerNameU,
130 &TimerName,
131 TRUE);
132
133 TimerHandle = OpenWaitableTimerW (dwDesiredAccess,
134 bInheritHandle,
135 TimerNameU.Buffer);
136
137 RtlFreeUnicodeString (&TimerNameU);
138
139 return TimerHandle;
140 }
141
142
143 WINBOOL STDCALL
144 SetWaitableTimer(HANDLE hTimer,
145 const LARGE_INTEGER *pDueTime,
146 LONG lPeriod,
147 PTIMERAPCROUTINE pfnCompletionRoutine,
148 LPVOID lpArgToCompletionRoutine,
149 WINBOOL fResume)
150 {
151 NTSTATUS Status;
152 BOOLEAN pState;
153
154 Status = NtSetTimer(hTimer,
155 (LARGE_INTEGER *)pDueTime,
156 pfnCompletionRoutine,
157 lpArgToCompletionRoutine,
158 fResume,
159 lPeriod,
160 &pState);
161 if (!NT_SUCCESS(Status))
162 {
163 SetLastErrorByStatus(Status);
164 return FALSE;
165 }
166 return TRUE;
167 }
168
169
170 WINBOOL STDCALL
171 CancelWaitableTimer(HANDLE hTimer)
172 {
173 NTSTATUS Status;
174 BOOLEAN CurrentState;
175
176 Status = NtCancelTimer(hTimer,
177 &CurrentState);
178 if (!NT_SUCCESS(Status))
179 {
180 SetLastErrorByStatus(Status);
181 return FALSE;
182 }
183 return TRUE;
184 }
185
186 /* EOF */