blob: 0992be412fb4616204020f1389778ddfc00eb294 [file] [log] [blame]
Alexandre Julliardad47a301999-11-29 01:58:35 +00001/*
2 * Win32 waitable timers
3 *
4 * Copyright 1999 Alexandre Julliard
5 */
6
7#include <assert.h>
8#include <string.h>
9#include "winerror.h"
10#include "file.h" /* for FILETIME routines */
11#include "server.h"
12
13
14/***********************************************************************
15 * CreateWaitableTimerA (KERNEL32.861)
16 */
17HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
18{
19 struct create_timer_request *req = get_req_buffer();
20
21 req->manual = manual;
22 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
23 server_strcpyAtoW( req->name, name );
24 SetLastError(0);
25 server_call( REQ_CREATE_TIMER );
26 if (req->handle == -1) return 0;
27 return req->handle;
28}
29
30
31/***********************************************************************
32 * CreateWaitableTimerW (KERNEL32.862)
33 */
34HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
35{
36 struct create_timer_request *req = get_req_buffer();
37
38 req->manual = manual;
39 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
40 server_strcpyW( req->name, name );
41 SetLastError(0);
42 server_call( REQ_CREATE_TIMER );
43 if (req->handle == -1) return 0;
44 return req->handle;
45}
46
47
48/***********************************************************************
49 * OpenWaitableTimerA (KERNEL32.881)
50 */
51HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
52{
53 struct open_timer_request *req = get_req_buffer();
54
55 req->access = access;
56 req->inherit = inherit;
57 server_strcpyAtoW( req->name, name );
58 server_call( REQ_OPEN_TIMER );
59 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
60 return req->handle;
61}
62
63
64/***********************************************************************
65 * OpenWaitableTimerW (KERNEL32.882)
66 */
67HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
68{
69 struct open_timer_request *req = get_req_buffer();
70
71 req->access = access;
72 req->inherit = inherit;
73 server_strcpyW( req->name, name );
74 server_call( REQ_OPEN_TIMER );
75 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
76 return req->handle;
77}
78
79
80/***********************************************************************
81 * SetWaitableTimer (KERNEL32.894)
82 */
83BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
84 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
85{
86 FILETIME ft;
87 DWORD remainder;
88 struct set_timer_request *req = get_req_buffer();
89
90 if (when->s.HighPart < 0) /* relative time */
91 {
Alexandre Julliard000c9801999-12-13 01:42:03 +000092 DWORD low;
Alexandre Julliardad47a301999-11-29 01:58:35 +000093 GetSystemTimeAsFileTime( &ft );
Alexandre Julliard000c9801999-12-13 01:42:03 +000094 low = ft.dwLowDateTime;
Alexandre Julliardad47a301999-11-29 01:58:35 +000095 ft.dwLowDateTime -= when->s.LowPart;
96 ft.dwHighDateTime -= when->s.HighPart;
97 if (low < ft.dwLowDateTime) ft.dwHighDateTime--; /* overflow */
98 }
99 else /* absolute time */
100 {
101 ft.dwLowDateTime = when->s.LowPart;
102 ft.dwHighDateTime = when->s.HighPart;
103 }
Alexandre Julliard000c9801999-12-13 01:42:03 +0000104
105 if (!ft.dwLowDateTime && !ft.dwHighDateTime)
106 {
107 /* special case to start timeout on now+period without too many calculations */
108 req->sec = 0;
109 req->usec = 0;
110 }
111 else
112 {
113 req->sec = DOSFS_FileTimeToUnixTime( &ft, &remainder );
114 req->usec = remainder / 10; /* convert from 100-ns to us units */
115 }
Alexandre Julliardad47a301999-11-29 01:58:35 +0000116 req->handle = handle;
Alexandre Julliardad47a301999-11-29 01:58:35 +0000117 req->period = period;
118 req->callback = callback;
119 req->arg = arg;
120 if (resume) SetLastError( ERROR_NOT_SUPPORTED ); /* set error but can still succeed */
121 return !server_call( REQ_SET_TIMER );
122}
123
124
125/***********************************************************************
126 * CancelWaitableTimer (KERNEL32.857)
127 */
128BOOL WINAPI CancelWaitableTimer( HANDLE handle )
129{
130 struct cancel_timer_request *req = get_req_buffer();
131 req->handle = handle;
132 return !server_call( REQ_CANCEL_TIMER );
133}