ntdll/tests: Account for some timer resolution rounding.
[wine.git] / server / timer.c
blob5e265d2ddf61e64dc4113628b2584d66a9bd8eba
1 /*
2 * Waitable timers management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <stdarg.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "file.h"
37 #include "handle.h"
38 #include "request.h"
40 static const WCHAR timer_name[] = {'T','i','m','e','r'};
42 struct type_descr timer_type =
44 { timer_name, sizeof(timer_name) }, /* name */
45 TIMER_ALL_ACCESS, /* valid_access */
46 { /* mapping */
47 STANDARD_RIGHTS_READ | TIMER_QUERY_STATE,
48 STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE,
49 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
50 TIMER_ALL_ACCESS
54 struct timer
56 struct object obj; /* object header */
57 int manual; /* manual reset */
58 int signaled; /* current signaled state */
59 unsigned int period; /* timer period in ms */
60 abstime_t when; /* next expiration */
61 struct timeout_user *timeout; /* timeout user */
62 struct thread *thread; /* thread that set the APC function */
63 client_ptr_t callback; /* callback APC function */
64 client_ptr_t arg; /* callback argument */
67 static void timer_dump( struct object *obj, int verbose );
68 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
69 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
70 static void timer_destroy( struct object *obj );
72 static const struct object_ops timer_ops =
74 sizeof(struct timer), /* size */
75 &timer_type, /* type */
76 timer_dump, /* dump */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 timer_signaled, /* signaled */
80 timer_satisfied, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 default_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 default_get_full_name, /* get_full_name */
87 no_lookup_name, /* lookup_name */
88 directory_link_name, /* link_name */
89 default_unlink_name, /* unlink_name */
90 no_open_file, /* open_file */
91 no_kernel_obj_list, /* get_kernel_obj_list */
92 no_close_handle, /* close_handle */
93 timer_destroy /* destroy */
97 /* create a timer object */
98 static struct timer *create_timer( struct object *root, const struct unicode_str *name,
99 unsigned int attr, int manual, const struct security_descriptor *sd )
101 struct timer *timer;
103 if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
105 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
107 /* initialize it if it didn't already exist */
108 timer->manual = manual;
109 timer->signaled = 0;
110 timer->when = 0;
111 timer->period = 0;
112 timer->timeout = NULL;
113 timer->thread = NULL;
116 return timer;
119 /* callback on timer expiration */
120 static void timer_callback( void *private )
122 struct timer *timer = (struct timer *)private;
124 /* queue an APC */
125 if (timer->thread)
127 apc_call_t data;
129 assert (timer->callback);
130 memset( &data, 0, sizeof(data) );
131 data.type = APC_USER;
132 data.user.func = timer->callback;
133 data.user.args[0] = timer->arg;
134 data.user.args[1] = (unsigned int)timer->when;
135 data.user.args[2] = timer->when >> 32;
137 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
139 release_object( timer->thread );
140 timer->thread = NULL;
144 if (timer->period) /* schedule the next expiration */
146 if (timer->when > 0) timer->when = -monotonic_time;
147 timer->when -= (abstime_t)timer->period * 10000;
148 timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
150 else timer->timeout = NULL;
152 /* wake up waiters */
153 timer->signaled = 1;
154 wake_up( &timer->obj, 0 );
157 /* cancel a running timer */
158 static int cancel_timer( struct timer *timer )
160 int signaled = timer->signaled;
162 if (timer->timeout)
164 remove_timeout_user( timer->timeout );
165 timer->timeout = NULL;
167 if (timer->thread)
169 thread_cancel_apc( timer->thread, &timer->obj, APC_USER );
170 release_object( timer->thread );
171 timer->thread = NULL;
173 return signaled;
176 /* set the timer expiration and period */
177 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
178 client_ptr_t callback, client_ptr_t arg )
180 int signaled = cancel_timer( timer );
181 if (timer->manual)
183 period = 0; /* period doesn't make any sense for a manual timer */
184 timer->signaled = 0;
186 timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
187 timer->period = period;
188 timer->callback = callback;
189 timer->arg = arg;
190 if (callback) timer->thread = (struct thread *)grab_object( current );
191 if (expire != TIMEOUT_INFINITE)
192 timer->timeout = add_timeout_user( expire, timer_callback, timer );
193 return signaled;
196 static void timer_dump( struct object *obj, int verbose )
198 struct timer *timer = (struct timer *)obj;
199 timeout_t timeout = abstime_to_timeout( timer->when );
200 assert( obj->ops == &timer_ops );
201 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
202 timer->manual, get_timeout_str(timeout), timer->period );
205 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
207 struct timer *timer = (struct timer *)obj;
208 assert( obj->ops == &timer_ops );
209 return timer->signaled;
212 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
214 struct timer *timer = (struct timer *)obj;
215 assert( obj->ops == &timer_ops );
216 if (!timer->manual) timer->signaled = 0;
219 static void timer_destroy( struct object *obj )
221 struct timer *timer = (struct timer *)obj;
222 assert( obj->ops == &timer_ops );
224 if (timer->timeout) remove_timeout_user( timer->timeout );
225 if (timer->thread) release_object( timer->thread );
228 /* create a timer */
229 DECL_HANDLER(create_timer)
231 struct timer *timer;
232 struct unicode_str name;
233 struct object *root;
234 const struct security_descriptor *sd;
235 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
237 if (!objattr) return;
239 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
241 reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
242 release_object( timer );
245 if (root) release_object( root );
248 /* open a handle to a timer */
249 DECL_HANDLER(open_timer)
251 struct unicode_str name = get_req_unicode_str();
253 reply->handle = open_object( current->process, req->rootdir, req->access,
254 &timer_ops, &name, req->attributes );
257 /* set a waitable timer */
258 DECL_HANDLER(set_timer)
260 struct timer *timer;
262 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
263 TIMER_MODIFY_STATE, &timer_ops )))
265 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
266 release_object( timer );
270 /* cancel a waitable timer */
271 DECL_HANDLER(cancel_timer)
273 struct timer *timer;
275 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
276 TIMER_MODIFY_STATE, &timer_ops )))
278 reply->signaled = cancel_timer( timer );
279 release_object( timer );
283 /* Get information on a waitable timer */
284 DECL_HANDLER(get_timer_info)
286 struct timer *timer;
288 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
289 TIMER_QUERY_STATE, &timer_ops )))
291 reply->when = timer->when;
292 reply->signaled = timer->signaled;
293 release_object( timer );