blob: 2640dd87a5c5106d9cf63dbf7adbc3b6ea73eec0 [file] [log] [blame]
Alexandre Julliardad47a301999-11-29 01:58:35 +00001/*
2 * Waitable timers management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardad47a301999-11-29 01:58:35 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliardad47a301999-11-29 01:58:35 +000024#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/time.h>
28#include <sys/types.h>
Robert Lunnon95414ef2005-11-22 12:01:05 +000029#include <stdarg.h>
Alexandre Julliardad47a301999-11-29 01:58:35 +000030
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010031#include "ntstatus.h"
32#define WIN32_NO_STATUS
Alexandre Julliard435e2e62002-12-10 22:56:43 +000033#include "windef.h"
Vitaliy Margolena9960002005-10-27 18:30:37 +000034#include "winternl.h"
Alexandre Julliarde66207e2003-02-19 00:33:32 +000035
36#include "file.h"
Alexandre Julliardad47a301999-11-29 01:58:35 +000037#include "handle.h"
38#include "request.h"
39
Alexandre Julliardad47a301999-11-29 01:58:35 +000040struct timer
41{
42 struct object obj; /* object header */
43 int manual; /* manual reset */
44 int signaled; /* current signaled state */
45 int period; /* timer period in ms */
46 struct timeval when; /* next expiration */
47 struct timeout_user *timeout; /* timeout user */
Alexandre Julliardea1afce2000-08-22 20:08:37 +000048 struct thread *thread; /* thread that set the APC function */
Alexandre Julliardad47a301999-11-29 01:58:35 +000049 void *callback; /* callback APC function */
50 void *arg; /* callback argument */
51};
52
53static void timer_dump( struct object *obj, int verbose );
54static int timer_signaled( struct object *obj, struct thread *thread );
55static int timer_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard03f46e12005-12-12 14:58:44 +010056static unsigned int timer_map_access( struct object *obj, unsigned int access );
Alexandre Julliardad47a301999-11-29 01:58:35 +000057static void timer_destroy( struct object *obj );
58
59static const struct object_ops timer_ops =
60{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000061 sizeof(struct timer), /* size */
62 timer_dump, /* dump */
63 add_queue, /* add_queue */
64 remove_queue, /* remove_queue */
65 timer_signaled, /* signaled */
66 timer_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000067 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000068 no_get_fd, /* get_fd */
Alexandre Julliard03f46e12005-12-12 14:58:44 +010069 timer_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000070 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000071 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000072 timer_destroy /* destroy */
Alexandre Julliardad47a301999-11-29 01:58:35 +000073};
74
75
76/* create a timer object */
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +010077static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
78 unsigned int attr, int manual )
Alexandre Julliardad47a301999-11-29 01:58:35 +000079{
80 struct timer *timer;
81
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +010082 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
Alexandre Julliardad47a301999-11-29 01:58:35 +000083 {
Vitaliy Margolen893987b2005-11-21 16:27:03 +000084 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
Alexandre Julliardad47a301999-11-29 01:58:35 +000085 {
86 /* initialize it if it didn't already exist */
87 timer->manual = manual;
88 timer->signaled = 0;
89 timer->when.tv_sec = 0;
90 timer->when.tv_usec = 0;
91 timer->period = 0;
92 timer->timeout = NULL;
Alexandre Julliardea1afce2000-08-22 20:08:37 +000093 timer->thread = NULL;
Alexandre Julliardad47a301999-11-29 01:58:35 +000094 }
95 }
96 return timer;
97}
98
99/* callback on timer expiration */
100static void timer_callback( void *private )
101{
102 struct timer *timer = (struct timer *)private;
103
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000104 /* queue an APC */
105 if (timer->thread)
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000106 {
Alexandre Julliard088bcf92003-04-04 22:26:34 +0000107 if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0,
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000108 (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
109 {
110 release_object( timer->thread );
111 timer->thread = NULL;
112 }
113 }
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000114
Alexandre Julliardad47a301999-11-29 01:58:35 +0000115 if (timer->period) /* schedule the next expiration */
116 {
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000117 add_timeout( &timer->when, timer->period );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000118 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
119 }
120 else timer->timeout = NULL;
121
122 /* wake up waiters */
123 timer->signaled = 1;
124 wake_up( &timer->obj, 0 );
125}
126
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000127/* cancel a running timer */
Eric Pouech4c591d42003-05-20 04:00:42 +0000128static int cancel_timer( struct timer *timer )
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000129{
Eric Pouech4c591d42003-05-20 04:00:42 +0000130 int signaled = timer->signaled;
131
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000132 if (timer->timeout)
133 {
134 remove_timeout_user( timer->timeout );
135 timer->timeout = NULL;
136 }
137 if (timer->thread)
138 {
Alexandre Julliard23623802001-01-06 01:48:51 +0000139 thread_cancel_apc( timer->thread, &timer->obj, 0 );
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000140 release_object( timer->thread );
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000141 timer->thread = NULL;
142 }
Eric Pouech4c591d42003-05-20 04:00:42 +0000143 return signaled;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000144}
145
Alexandre Julliardad47a301999-11-29 01:58:35 +0000146/* set the timer expiration and period */
Eric Pouech4c591d42003-05-20 04:00:42 +0000147static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
148 void *callback, void *arg )
Alexandre Julliardad47a301999-11-29 01:58:35 +0000149{
Eric Pouech4c591d42003-05-20 04:00:42 +0000150 int signaled = cancel_timer( timer );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000151 if (timer->manual)
152 {
153 period = 0; /* period doesn't make any sense for a manual timer */
154 timer->signaled = 0;
155 }
Alexandre Julliard462172a2003-04-02 22:48:59 +0000156 if (!expire->sec && !expire->usec)
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000157 {
158 /* special case: use now + period as first expiration */
Alexandre Julliard753c8702006-08-10 16:42:09 +0200159 timer->when = current_time;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000160 add_timeout( &timer->when, period );
161 }
162 else
163 {
Alexandre Julliard462172a2003-04-02 22:48:59 +0000164 timer->when.tv_sec = expire->sec;
165 timer->when.tv_usec = expire->usec;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000166 }
Alexandre Julliardad47a301999-11-29 01:58:35 +0000167 timer->period = period;
168 timer->callback = callback;
169 timer->arg = arg;
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000170 if (callback) timer->thread = (struct thread *)grab_object( current );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000171 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
Eric Pouech4c591d42003-05-20 04:00:42 +0000172 return signaled;
Alexandre Julliardad47a301999-11-29 01:58:35 +0000173}
174
Alexandre Julliardad47a301999-11-29 01:58:35 +0000175static void timer_dump( struct object *obj, int verbose )
176{
177 struct timer *timer = (struct timer *)obj;
178 assert( obj->ops == &timer_ops );
Alexandre Julliard53496f82006-01-23 16:48:26 +0100179 fprintf( stderr, "Timer manual=%d when=%ld.%06u period=%d ",
180 timer->manual, timer->when.tv_sec, (unsigned int)timer->when.tv_usec, timer->period );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000181 dump_object_name( &timer->obj );
182 fputc( '\n', stderr );
183}
184
185static int timer_signaled( struct object *obj, struct thread *thread )
186{
187 struct timer *timer = (struct timer *)obj;
188 assert( obj->ops == &timer_ops );
189 return timer->signaled;
190}
191
192static int timer_satisfied( struct object *obj, struct thread *thread )
193{
194 struct timer *timer = (struct timer *)obj;
195 assert( obj->ops == &timer_ops );
196 if (!timer->manual) timer->signaled = 0;
197 return 0;
198}
199
Alexandre Julliard03f46e12005-12-12 14:58:44 +0100200static unsigned int timer_map_access( struct object *obj, unsigned int access )
201{
202 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
203 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
204 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
205 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
206 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
207}
208
Alexandre Julliardad47a301999-11-29 01:58:35 +0000209static void timer_destroy( struct object *obj )
210{
211 struct timer *timer = (struct timer *)obj;
212 assert( obj->ops == &timer_ops );
213
214 if (timer->timeout) remove_timeout_user( timer->timeout );
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000215 if (timer->thread) release_object( timer->thread );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000216}
217
218/* create a timer */
219DECL_HANDLER(create_timer)
220{
Alexandre Julliardad47a301999-11-29 01:58:35 +0000221 struct timer *timer;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000222 struct unicode_str name;
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100223 struct directory *root = NULL;
Alexandre Julliardad47a301999-11-29 01:58:35 +0000224
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000225 reply->handle = 0;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000226 get_req_unicode_str( &name );
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100227 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
228 return;
229
230 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
Alexandre Julliardad47a301999-11-29 01:58:35 +0000231 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100232 reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000233 release_object( timer );
234 }
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100235
236 if (root) release_object( root );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000237}
238
239/* open a handle to a timer */
240DECL_HANDLER(open_timer)
241{
Alexandre Julliardead9b062005-11-18 16:31:18 +0000242 struct unicode_str name;
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100243 struct directory *root = NULL;
Alexandre Julliard3764da62005-12-05 12:52:05 +0100244 struct timer *timer;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000245
246 get_req_unicode_str( &name );
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100247 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
248 return;
249
Alexandre Julliard3764da62005-12-05 12:52:05 +0100250 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
251 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100252 reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
Alexandre Julliard3764da62005-12-05 12:52:05 +0100253 release_object( timer );
254 }
Vitaliy Margolen7c5cb7a2005-12-02 16:05:54 +0100255
256 if (root) release_object( root );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000257}
258
259/* set a waitable timer */
260DECL_HANDLER(set_timer)
261{
262 struct timer *timer;
263
264 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
265 TIMER_MODIFY_STATE, &timer_ops )))
266 {
Eric Pouech4c591d42003-05-20 04:00:42 +0000267 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000268 release_object( timer );
269 }
270}
271
272/* cancel a waitable timer */
273DECL_HANDLER(cancel_timer)
274{
275 struct timer *timer;
276
277 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
278 TIMER_MODIFY_STATE, &timer_ops )))
279 {
Eric Pouech4c591d42003-05-20 04:00:42 +0000280 reply->signaled = cancel_timer( timer );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000281 release_object( timer );
282 }
283}
Robert Shearman7572b122004-12-13 21:10:58 +0000284
285/* Get information on a waitable timer */
286DECL_HANDLER(get_timer_info)
287{
288 struct timer *timer;
289
290 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
291 TIMER_QUERY_STATE, &timer_ops )))
292 {
293 reply->when.sec = timer->when.tv_sec;
294 reply->when.usec = timer->when.tv_usec;
295 reply->signaled = timer->signaled;
296 release_object( timer );
297 }
298}