update for HEAD-2003091401
[reactos.git] / lib / kernel32 / synch / timer.c
1 /* $Id$
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 /*
20  * @implemented
21  */
22 HANDLE STDCALL
23 CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
24                      WINBOOL bManualReset,
25                      LPWSTR lpTimerName)
26 {
27    NTSTATUS Status;
28    HANDLE TimerHandle;
29    OBJECT_ATTRIBUTES ObjectAttributes;
30    UNICODE_STRING UnicodeName;
31    ULONG TimerType;
32    
33    if (bManualReset)
34      TimerType = NotificationTimer;
35    else
36      TimerType = SynchronizationTimer;
37
38    RtlInitUnicodeString(&UnicodeName, lpTimerName);
39    InitializeObjectAttributes(&ObjectAttributes,
40                               &UnicodeName,
41                               0,
42                               hBaseDir,
43                               NULL);
44    
45    Status = NtCreateTimer(&TimerHandle,
46                           TIMER_ALL_ACCESS,
47                           &ObjectAttributes,
48                           TimerType);
49    if (!NT_SUCCESS(Status))
50      {
51         SetLastErrorByStatus(Status);
52         return NULL;
53      }
54
55    return TimerHandle;
56 }
57
58
59 /*
60  * @implemented
61  */
62 HANDLE STDCALL
63 CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
64                      WINBOOL bManualReset,
65                      LPCSTR lpTimerName)
66 {
67         UNICODE_STRING TimerNameU;
68         ANSI_STRING TimerName;
69         HANDLE TimerHandle;
70
71         RtlInitAnsiString (&TimerName,
72                            (LPSTR)lpTimerName);
73         RtlAnsiStringToUnicodeString (&TimerNameU,
74                                       &TimerName,
75                                       TRUE);
76
77         TimerHandle = CreateWaitableTimerW (lpTimerAttributes,
78                                             bManualReset,
79                                             TimerNameU.Buffer);
80
81         RtlFreeUnicodeString (&TimerNameU);
82
83         return TimerHandle;
84 }
85
86
87 /*
88  * @implemented
89  */
90 HANDLE STDCALL
91 OpenWaitableTimerW(DWORD dwDesiredAccess,
92                    WINBOOL bInheritHandle,
93                    LPCWSTR lpTimerName)
94 {
95    NTSTATUS Status;
96    HANDLE TimerHandle;
97    OBJECT_ATTRIBUTES ObjectAttributes;
98    UNICODE_STRING UnicodeName;
99    ULONG Attributes = 0;
100
101    if (bInheritHandle)
102      {
103         Attributes = OBJ_INHERIT;
104      }
105
106    RtlInitUnicodeString(&UnicodeName,
107                         lpTimerName);
108    InitializeObjectAttributes(&ObjectAttributes,
109                               &UnicodeName,
110                               Attributes,
111                               hBaseDir,
112                               NULL);
113
114    Status = NtOpenTimer(&TimerHandle,
115                         dwDesiredAccess,
116                         &ObjectAttributes);
117    if (!NT_SUCCESS(Status))
118      {
119         SetLastErrorByStatus(Status);
120         return NULL;
121      }
122
123    return TimerHandle;
124 }
125
126
127 /*
128  * @implemented
129  */
130 HANDLE STDCALL
131 OpenWaitableTimerA(DWORD dwDesiredAccess,
132                    WINBOOL bInheritHandle,
133                    LPCSTR lpTimerName)
134 {
135         UNICODE_STRING TimerNameU;
136         ANSI_STRING TimerName;
137         HANDLE TimerHandle;
138
139         RtlInitAnsiString (&TimerName,
140                            (LPSTR)lpTimerName);
141         RtlAnsiStringToUnicodeString (&TimerNameU,
142                                       &TimerName,
143                                       TRUE);
144
145         TimerHandle = OpenWaitableTimerW (dwDesiredAccess,
146                                           bInheritHandle,
147                                           TimerNameU.Buffer);
148
149         RtlFreeUnicodeString (&TimerNameU);
150
151         return TimerHandle;
152 }
153
154
155 /*
156  * @implemented
157  */
158 WINBOOL STDCALL
159 SetWaitableTimer(HANDLE hTimer,
160                  const LARGE_INTEGER *pDueTime,
161                  LONG lPeriod,
162                  PTIMERAPCROUTINE pfnCompletionRoutine,
163                  LPVOID lpArgToCompletionRoutine,
164                  WINBOOL fResume)
165 {
166    NTSTATUS Status;
167    BOOLEAN pState;
168
169    Status = NtSetTimer(hTimer,
170                        (LARGE_INTEGER *)pDueTime,
171                        pfnCompletionRoutine,
172                        lpArgToCompletionRoutine,
173                        fResume,
174                        lPeriod,
175                        &pState);
176    if (!NT_SUCCESS(Status))
177      {
178         SetLastErrorByStatus(Status);
179         return FALSE;
180      }
181    return TRUE;
182 }
183
184
185 /*
186  * @implemented
187  */
188 WINBOOL STDCALL
189 CancelWaitableTimer(HANDLE hTimer)
190 {
191    NTSTATUS Status;
192    BOOLEAN CurrentState;
193
194    Status = NtCancelTimer(hTimer,
195                           &CurrentState);
196    if (!NT_SUCCESS(Status))
197      {
198         SetLastErrorByStatus(Status);
199         return FALSE;
200      }
201    return TRUE;
202 }
203
204 /* EOF */