ReactOS  0.4.14-dev-991-g696cdc6
timerqueue.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Win32 Base API
3  * LICENSE: See COPYING in the top level directory
4  * FILE: dll/win32/kernel32/client/timerqueue.c
5  * PURPOSE: Timer Queue Functions
6  * PROGRAMMERS: Thomas Weidenmueller <w3seek@reactos.com>
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include <k32.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* GLOBALS ********************************************************************/
17 
19 
20 /* PRIVATE FUNCTIONS **********************************************************/
21 
22 /* FIXME - make this thread safe */
23 BOOL
24 WINAPI
26 {
28 
29  /* Create the timer queue */
31  if (!NT_SUCCESS(Status))
32  {
34  DPRINT1("Unable to create the default timer queue!\n");
35  return FALSE;
36  }
37 
38  return TRUE;
39 }
40 
41 /* PUBLIC FUNCTIONS ***********************************************************/
42 
43 
44 /*
45  * @implemented
46  */
47 BOOL
48 WINAPI
50  IN HANDLE Timer)
51 {
53 
54  if (!TimerQueue)
55  {
56  /* Use the default timer queue */
57  TimerQueue = BasepDefaultTimerQueue;
58  if (!TimerQueue)
59  {
60  /* A timer is being cancelled before one was created... fail */
62  return FALSE;
63  }
64  }
65 
66  /* Delete the timer */
67  Status = RtlDeleteTimer(TimerQueue, Timer, NULL);
68  if (!NT_SUCCESS(Status))
69  {
71  return FALSE;
72  }
73 
74  return TRUE;
75 }
76 
77 /*
78  * @implemented
79  */
80 BOOL
81 WINAPI
83  IN HANDLE Timer,
85  IN ULONG Period)
86 {
88 
89  if (!TimerQueue)
90  {
91  /* Use the default timer queue */
92  TimerQueue = BasepDefaultTimerQueue;
93  if (!TimerQueue)
94  {
95  /* A timer is being cancelled before one was created... fail */
97  return FALSE;
98  }
99  }
100 
101  /* Delete the timer */
102  Status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
103  if (!NT_SUCCESS(Status))
104  {
106  return FALSE;
107  }
108 
109  return TRUE;
110 }
111 
112 /*
113  * @implemented
114  */
115 HANDLE
116 WINAPI
118 {
119  HANDLE Handle;
121 
122  /* Create the timer queue */
124  if(!NT_SUCCESS(Status))
125  {
127  return NULL;
128  }
129 
130  return Handle;
131 }
132 
133 /*
134  * @implemented
135  */
136 BOOL
137 WINAPI
139  IN HANDLE TimerQueue,
142  IN DWORD DueTime,
143  IN DWORD Period,
144  IN ULONG Flags)
145 {
147 
148  /* Parameter is not optional -- clear it now */
149  *phNewTimer = NULL;
150 
151  /* Check if no queue was given */
152  if (!TimerQueue)
153  {
154  /* Create the queue if it didn't already exist */
156  {
157  return FALSE;
158  }
159 
160  /* Use the default queue */
161  TimerQueue = BasepDefaultTimerQueue;
162  }
163 
164  /* Create the timer. Note that no parameter checking is done here */
165  Status = RtlCreateTimer(TimerQueue,
166  phNewTimer,
167  Callback,
168  Parameter,
169  DueTime,
170  Period,
171  Flags);
172  if (!NT_SUCCESS(Status))
173  {
175  return FALSE;
176  }
177 
178  return TRUE;
179 }
180 
181 /*
182  * @implemented
183  */
184 BOOL
185 WINAPI
187 {
188  /* We don't allow the user to delete the default timer queue */
189  if (!TimerQueue)
190  {
192  return FALSE;
193  }
194 
195  /* Delete the timer queue */
196  RtlDeleteTimerQueueEx(TimerQueue, 0);
197  return TRUE;
198 }
199 
200 /*
201  * @implemented
202  */
203 BOOL
204 WINAPI
206  IN HANDLE CompletionEvent)
207 {
209 
210  /* We don't allow the user to delete the default timer queue */
211  if (!TimerQueue)
212  {
214  return FALSE;
215  }
216 
217  /* Delete the timer queue */
218  Status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
219  if (((CompletionEvent != INVALID_HANDLE_VALUE) && (Status == STATUS_PENDING)) ||
220  !(NT_SUCCESS(Status)))
221  {
222  /* In case CompletionEvent == NULL, RtlDeleteTimerQueueEx() returns before
223  all callback routines returned. We set the last error code to STATUS_PENDING
224  and return FALSE. In case CompletionEvent == INVALID_HANDLE_VALUE we only
225  can get here if another error occured. In case CompletionEvent is something
226  else, we get here and fail, even though it isn't really an error (if Status == STATUS_PENDING).
227  We also handle all other failures the same way. */
229  return FALSE;
230  }
231 
232  return TRUE;
233 }
234 
235 /*
236  * @implemented
237  */
238 BOOL
239 WINAPI
241  IN HANDLE Timer,
242  IN HANDLE CompletionEvent)
243 {
245 
246  if (!TimerQueue)
247  {
248  /* Use the default timer queue */
249  TimerQueue = BasepDefaultTimerQueue;
250  if (!TimerQueue)
251  {
252  /* A timer is being cancelled before one was created... fail */
254  return FALSE;
255  }
256  }
257 
258  /* Delete the timer */
259  Status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
260  if (((CompletionEvent != INVALID_HANDLE_VALUE) && (Status == STATUS_PENDING)) ||
261  !(NT_SUCCESS(Status)))
262  {
263  /* In case CompletionEvent == NULL, RtlDeleteTimerQueueEx() returns before
264  all callback routines returned. We set the last error code to STATUS_PENDING
265  and return FALSE. In case CompletionEvent == INVALID_HANDLE_VALUE we only
266  can get here if another error occured. In case CompletionEvent is something
267  else, we get here and fail, even though it isn't really an error (if Status == STATUS_PENDING).
268  We also handle all other failures the same way. */
270  return FALSE;
271  }
272 
273  return TRUE;
274 }
275 
276 /*
277  * @implemented
278  */
279 HANDLE
280 WINAPI
284  IN DWORD DueTime,
285  IN DWORD Period,
286  IN BOOL PreferIo)
287 {
288  HANDLE Timer;
290 
291  /* Check if no queue was given */
292  if (!TimerQueue)
293  {
294  /* Create the queue if it didn't already exist */
296  {
297  return FALSE;
298  }
299 
300  /* Use the default queue */
301  TimerQueue = BasepDefaultTimerQueue;
302  }
303 
304  /* Create the timer */
305  Status = RtlCreateTimer(TimerQueue,
306  &Timer,
307  Callback,
308  Parameter,
309  DueTime,
310  Period,
312  if (!NT_SUCCESS(Status))
313  {
315  return NULL;
316  }
317 
318  return Timer;
319 }
320 
321 /* EOF */
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define IN
Definition: typedefs.h:39
#define TRUE
Definition: types.h:120
HANDLE BasepDefaultTimerQueue
Definition: timerqueue.c:18
NTSTATUS WINAPI RtlDeleteTimer(HANDLE TimerQueue, HANDLE Timer, HANDLE CompletionEvent)
Definition: timerqueue.c:540
_In_ LARGE_INTEGER DueTime
Definition: kefuncs.h:511
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
LONG NTSTATUS
Definition: precomp.h:26
#define WT_EXECUTEINIOTHREAD
Definition: winnt_old.h:1072
BOOL WINAPI BasepCreateDefaultTimerQueue(VOID)
Definition: timerqueue.c:25
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
_In_ LARGE_INTEGER _In_ ULONG Period
Definition: kefuncs.h:1255
HANDLE WINAPI SetTimerQueueTimer(IN HANDLE TimerQueue, IN WAITORTIMERCALLBACK Callback, IN PVOID Parameter, IN DWORD DueTime, IN DWORD Period, IN BOOL PreferIo)
Definition: timerqueue.c:281
_In_ PVOID Parameter
Definition: ldrtypes.h:241
NTSTATUS WINAPI RtlDeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
Definition: timerqueue.c:331
NTSTATUS WINAPI RtlCreateTimerQueue(PHANDLE NewTimerQueue)
Definition: timerqueue.c:285
BOOL WINAPI DeleteTimerQueue(IN HANDLE TimerQueue)
Definition: timerqueue.c:186
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
NTSTATUS WINAPI RtlCreateTimer(HANDLE TimerQueue, PHANDLE NewTimer, WAITORTIMERCALLBACKFUNC Callback, PVOID Parameter, DWORD DueTime, DWORD Period, ULONG Flags)
Definition: timerqueue.c:425
unsigned int BOOL
Definition: ntddk_ex.h:94
HANDLE WINAPI CreateTimerQueue(VOID)
Definition: timerqueue.c:117
smooth NULL
Definition: ftsmooth.c:416
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
_In_ HANDLE Handle
Definition: extypes.h:390
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
BOOL WINAPI CancelTimerQueueTimer(IN HANDLE TimerQueue, IN HANDLE Timer)
Definition: timerqueue.c:49
#define WINAPI
Definition: msvc.h:6
BOOL WINAPI ChangeTimerQueueTimer(IN HANDLE TimerQueue, IN HANDLE Timer, IN ULONG DueTime, IN ULONG Period)
Definition: timerqueue.c:82
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:414
Status
Definition: gdiplustypes.h:24
UINT Timer
Definition: capclock.c:11
static WAITORTIMERCALLBACK
Definition: thread.c:86
BOOL WINAPI DeleteTimerQueueTimer(IN HANDLE TimerQueue, IN HANDLE Timer, IN HANDLE CompletionEvent)
Definition: timerqueue.c:240
#define DPRINT1
Definition: precomp.h:8
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
BOOL WINAPI DeleteTimerQueueEx(IN HANDLE TimerQueue, IN HANDLE CompletionEvent)
Definition: timerqueue.c:205
BOOL WINAPI CreateTimerQueueTimer(OUT PHANDLE phNewTimer, IN HANDLE TimerQueue, IN WAITORTIMERCALLBACK Callback, IN PVOID Parameter, IN DWORD DueTime, IN DWORD Period, IN ULONG Flags)
Definition: timerqueue.c:138
LPFNPSPCALLBACK Callback
Definition: desk.c:112
NTSTATUS WINAPI RtlUpdateTimer(HANDLE TimerQueue, HANDLE Timer, DWORD DueTime, DWORD Period)
Definition: timerqueue.c:504
#define WT_EXECUTEDEFAULT
Definition: winnt_old.h:1071