Fix prototypes of MSVCRT___getmainargs and MSVCRT___wgetmainargs.
[wine.git] / scheduler / timer.c
blob62a9fe092d4fd39c3dbada7472b931846174379d
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 "winnls.h"
11 #include "wine/unicode.h"
12 #include "file.h" /* for FILETIME routines */
13 #include "server.h"
16 /***********************************************************************
17 * CreateWaitableTimerA (KERNEL32.861)
19 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
21 HANDLE ret;
22 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
23 if (len >= MAX_PATH)
25 SetLastError( ERROR_FILENAME_EXCED_RANGE );
26 return 0;
28 SERVER_START_REQ
30 struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
32 req->manual = manual;
33 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
34 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
35 SetLastError(0);
36 server_call( REQ_CREATE_TIMER );
37 ret = req->handle;
39 SERVER_END_REQ;
40 return ret;
44 /***********************************************************************
45 * CreateWaitableTimerW (KERNEL32.862)
47 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
49 HANDLE ret;
50 DWORD len = name ? strlenW(name) : 0;
51 if (len >= MAX_PATH)
53 SetLastError( ERROR_FILENAME_EXCED_RANGE );
54 return 0;
56 SERVER_START_REQ
58 struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
60 req->manual = manual;
61 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
62 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
63 SetLastError(0);
64 server_call( REQ_CREATE_TIMER );
65 ret = req->handle;
67 SERVER_END_REQ;
68 return ret;
72 /***********************************************************************
73 * OpenWaitableTimerA (KERNEL32.881)
75 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
77 HANDLE ret;
78 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
79 if (len >= MAX_PATH)
81 SetLastError( ERROR_FILENAME_EXCED_RANGE );
82 return 0;
84 SERVER_START_REQ
86 struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
88 req->access = access;
89 req->inherit = inherit;
90 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
91 server_call( REQ_OPEN_TIMER );
92 ret = req->handle;
94 SERVER_END_REQ;
95 return ret;
99 /***********************************************************************
100 * OpenWaitableTimerW (KERNEL32.882)
102 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
104 HANDLE ret;
105 DWORD len = name ? strlenW(name) : 0;
106 if (len >= MAX_PATH)
108 SetLastError( ERROR_FILENAME_EXCED_RANGE );
109 return 0;
111 SERVER_START_REQ
113 struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
115 req->access = access;
116 req->inherit = inherit;
117 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
118 server_call( REQ_OPEN_TIMER );
119 ret = req->handle;
121 SERVER_END_REQ;
122 return ret;
126 /***********************************************************************
127 * SetWaitableTimer (KERNEL32.894)
129 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
130 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
132 BOOL ret;
133 LARGE_INTEGER exp = *when;
135 if (exp.s.HighPart < 0) /* relative time */
137 LARGE_INTEGER now;
138 NtQuerySystemTime( &now );
139 exp.QuadPart = RtlLargeIntegerSubtract( now.QuadPart, exp.QuadPart );
142 SERVER_START_REQ
144 struct set_timer_request *req = server_alloc_req( sizeof(*req), 0 );
146 if (!exp.s.LowPart && !exp.s.HighPart)
148 /* special case to start timeout on now+period without too many calculations */
149 req->sec = 0;
150 req->usec = 0;
152 else
154 DWORD remainder;
155 req->sec = DOSFS_FileTimeToUnixTime( (FILETIME *)&exp, &remainder );
156 req->usec = remainder / 10; /* convert from 100-ns to us units */
158 req->handle = handle;
159 req->period = period;
160 req->callback = callback;
161 req->arg = arg;
162 if (resume) SetLastError( ERROR_NOT_SUPPORTED ); /* set error but can still succeed */
163 ret = !server_call( REQ_SET_TIMER );
165 SERVER_END_REQ;
166 return ret;
170 /***********************************************************************
171 * CancelWaitableTimer (KERNEL32.857)
173 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
175 BOOL ret;
176 SERVER_START_REQ
178 struct cancel_timer_request *req = server_alloc_req( sizeof(*req), 0 );
179 req->handle = handle;
180 ret = !server_call( REQ_CANCEL_TIMER );
182 SERVER_END_REQ;
183 return ret;