Use waitable timers to implement service thread timers.
[wine/multimedia.git] / scheduler / timer.c
blob0992be412fb4616204020f1389778ddfc00eb294
1 /*
2 * Win32 waitable timers
4 * Copyright 1999 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include "winerror.h"
10 #include "file.h" /* for FILETIME routines */
11 #include "server.h"
14 /***********************************************************************
15 * CreateWaitableTimerA (KERNEL32.861)
17 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
19 struct create_timer_request *req = get_req_buffer();
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;
31 /***********************************************************************
32 * CreateWaitableTimerW (KERNEL32.862)
34 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
36 struct create_timer_request *req = get_req_buffer();
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;
48 /***********************************************************************
49 * OpenWaitableTimerA (KERNEL32.881)
51 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
53 struct open_timer_request *req = get_req_buffer();
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;
64 /***********************************************************************
65 * OpenWaitableTimerW (KERNEL32.882)
67 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
69 struct open_timer_request *req = get_req_buffer();
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;
80 /***********************************************************************
81 * SetWaitableTimer (KERNEL32.894)
83 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
84 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
86 FILETIME ft;
87 DWORD remainder;
88 struct set_timer_request *req = get_req_buffer();
90 if (when->s.HighPart < 0) /* relative time */
92 DWORD low;
93 GetSystemTimeAsFileTime( &ft );
94 low = ft.dwLowDateTime;
95 ft.dwLowDateTime -= when->s.LowPart;
96 ft.dwHighDateTime -= when->s.HighPart;
97 if (low < ft.dwLowDateTime) ft.dwHighDateTime--; /* overflow */
99 else /* absolute time */
101 ft.dwLowDateTime = when->s.LowPart;
102 ft.dwHighDateTime = when->s.HighPart;
105 if (!ft.dwLowDateTime && !ft.dwHighDateTime)
107 /* special case to start timeout on now+period without too many calculations */
108 req->sec = 0;
109 req->usec = 0;
111 else
113 req->sec = DOSFS_FileTimeToUnixTime( &ft, &remainder );
114 req->usec = remainder / 10; /* convert from 100-ns to us units */
116 req->handle = handle;
117 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 );
125 /***********************************************************************
126 * CancelWaitableTimer (KERNEL32.857)
128 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
130 struct cancel_timer_request *req = get_req_buffer();
131 req->handle = handle;
132 return !server_call( REQ_CANCEL_TIMER );