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