blob: bc7eb4598b68fb806053b3ba6bac2544f09e365e [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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
29
Alexandre Julliard435e2e62002-12-10 22:56:43 +000030#include "windef.h"
Alexandre Julliarde66207e2003-02-19 00:33:32 +000031
32#include "file.h"
Alexandre Julliardad47a301999-11-29 01:58:35 +000033#include "handle.h"
34#include "request.h"
35
Alexandre Julliardad47a301999-11-29 01:58:35 +000036struct timer
37{
38 struct object obj; /* object header */
39 int manual; /* manual reset */
40 int signaled; /* current signaled state */
41 int period; /* timer period in ms */
42 struct timeval when; /* next expiration */
43 struct timeout_user *timeout; /* timeout user */
Alexandre Julliardea1afce2000-08-22 20:08:37 +000044 struct thread *thread; /* thread that set the APC function */
Alexandre Julliardad47a301999-11-29 01:58:35 +000045 void *callback; /* callback APC function */
46 void *arg; /* callback argument */
47};
48
49static void timer_dump( struct object *obj, int verbose );
50static int timer_signaled( struct object *obj, struct thread *thread );
51static int timer_satisfied( struct object *obj, struct thread *thread );
52static void timer_destroy( struct object *obj );
53
54static const struct object_ops timer_ops =
55{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000056 sizeof(struct timer), /* size */
57 timer_dump, /* dump */
58 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 timer_signaled, /* signaled */
61 timer_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000062 no_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000063 timer_destroy /* destroy */
Alexandre Julliardad47a301999-11-29 01:58:35 +000064};
65
66
67/* create a timer object */
68static struct timer *create_timer( const WCHAR *name, size_t len, int manual )
69{
70 struct timer *timer;
71
Alexandre Julliard526a28d2002-10-02 23:49:30 +000072 if ((timer = create_named_object( sync_namespace, &timer_ops, name, len )))
Alexandre Julliardad47a301999-11-29 01:58:35 +000073 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000074 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
Alexandre Julliardad47a301999-11-29 01:58:35 +000075 {
76 /* initialize it if it didn't already exist */
77 timer->manual = manual;
78 timer->signaled = 0;
79 timer->when.tv_sec = 0;
80 timer->when.tv_usec = 0;
81 timer->period = 0;
82 timer->timeout = NULL;
Alexandre Julliardea1afce2000-08-22 20:08:37 +000083 timer->thread = NULL;
Alexandre Julliardad47a301999-11-29 01:58:35 +000084 }
85 }
86 return timer;
87}
88
89/* callback on timer expiration */
90static void timer_callback( void *private )
91{
92 struct timer *timer = (struct timer *)private;
93
Alexandre Julliardea1afce2000-08-22 20:08:37 +000094 /* queue an APC */
95 if (timer->thread)
Alexandre Julliard15979fd2002-03-23 18:50:04 +000096 {
Alexandre Julliard088bcf92003-04-04 22:26:34 +000097 if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0,
Alexandre Julliard15979fd2002-03-23 18:50:04 +000098 (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
99 {
100 release_object( timer->thread );
101 timer->thread = NULL;
102 }
103 }
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000104
Alexandre Julliardad47a301999-11-29 01:58:35 +0000105 if (timer->period) /* schedule the next expiration */
106 {
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000107 add_timeout( &timer->when, timer->period );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000108 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
109 }
110 else timer->timeout = NULL;
111
112 /* wake up waiters */
113 timer->signaled = 1;
114 wake_up( &timer->obj, 0 );
115}
116
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000117/* cancel a running timer */
Eric Pouech4c591d42003-05-20 04:00:42 +0000118static int cancel_timer( struct timer *timer )
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000119{
Eric Pouech4c591d42003-05-20 04:00:42 +0000120 int signaled = timer->signaled;
121
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000122 if (timer->timeout)
123 {
124 remove_timeout_user( timer->timeout );
125 timer->timeout = NULL;
126 }
127 if (timer->thread)
128 {
Alexandre Julliard23623802001-01-06 01:48:51 +0000129 thread_cancel_apc( timer->thread, &timer->obj, 0 );
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000130 release_object( timer->thread );
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000131 timer->thread = NULL;
132 }
Eric Pouech4c591d42003-05-20 04:00:42 +0000133 return signaled;
Alexandre Julliardea1afce2000-08-22 20:08:37 +0000134}
135
Alexandre Julliardad47a301999-11-29 01:58:35 +0000136/* set the timer expiration and period */
Eric Pouech4c591d42003-05-20 04:00:42 +0000137static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
138 void *callback, void *arg )
Alexandre Julliardad47a301999-11-29 01:58:35 +0000139{
Eric Pouech4c591d42003-05-20 04:00:42 +0000140 int signaled = cancel_timer( timer );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000141 if (timer->manual)
142 {
143 period = 0; /* period doesn't make any sense for a manual timer */
144 timer->signaled = 0;
145 }
Alexandre Julliard462172a2003-04-02 22:48:59 +0000146 if (!expire->sec && !expire->usec)
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000147 {
148 /* special case: use now + period as first expiration */
149 gettimeofday( &timer->when, 0 );
150 add_timeout( &timer->when, period );
151 }
152 else
153 {
Alexandre Julliard462172a2003-04-02 22:48:59 +0000154 timer->when.tv_sec = expire->sec;
155 timer->when.tv_usec = expire->usec;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000156 }
Alexandre Julliardad47a301999-11-29 01:58:35 +0000157 timer->period = period;
158 timer->callback = callback;
159 timer->arg = arg;
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000160 if (callback) timer->thread = (struct thread *)grab_object( current );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000161 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
Eric Pouech4c591d42003-05-20 04:00:42 +0000162 return signaled;
Alexandre Julliardad47a301999-11-29 01:58:35 +0000163}
164
Alexandre Julliardad47a301999-11-29 01:58:35 +0000165static void timer_dump( struct object *obj, int verbose )
166{
167 struct timer *timer = (struct timer *)obj;
168 assert( obj->ops == &timer_ops );
169 fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
170 timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
171 dump_object_name( &timer->obj );
172 fputc( '\n', stderr );
173}
174
175static int timer_signaled( struct object *obj, struct thread *thread )
176{
177 struct timer *timer = (struct timer *)obj;
178 assert( obj->ops == &timer_ops );
179 return timer->signaled;
180}
181
182static int timer_satisfied( struct object *obj, struct thread *thread )
183{
184 struct timer *timer = (struct timer *)obj;
185 assert( obj->ops == &timer_ops );
186 if (!timer->manual) timer->signaled = 0;
187 return 0;
188}
189
190static void timer_destroy( struct object *obj )
191{
192 struct timer *timer = (struct timer *)obj;
193 assert( obj->ops == &timer_ops );
194
195 if (timer->timeout) remove_timeout_user( timer->timeout );
Alexandre Julliard15979fd2002-03-23 18:50:04 +0000196 if (timer->thread) release_object( timer->thread );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000197}
198
199/* create a timer */
200DECL_HANDLER(create_timer)
201{
Alexandre Julliardad47a301999-11-29 01:58:35 +0000202 struct timer *timer;
203
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000204 reply->handle = 0;
205 if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual )))
Alexandre Julliardad47a301999-11-29 01:58:35 +0000206 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000207 reply->handle = alloc_handle( current->process, timer, TIMER_ALL_ACCESS, req->inherit );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000208 release_object( timer );
209 }
210}
211
212/* open a handle to a timer */
213DECL_HANDLER(open_timer)
214{
Alexandre Julliard526a28d2002-10-02 23:49:30 +0000215 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000216 &timer_ops, req->access, req->inherit );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000217}
218
219/* set a waitable timer */
220DECL_HANDLER(set_timer)
221{
222 struct timer *timer;
223
224 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
225 TIMER_MODIFY_STATE, &timer_ops )))
226 {
Eric Pouech4c591d42003-05-20 04:00:42 +0000227 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000228 release_object( timer );
229 }
230}
231
232/* cancel a waitable timer */
233DECL_HANDLER(cancel_timer)
234{
235 struct timer *timer;
236
237 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
238 TIMER_MODIFY_STATE, &timer_ops )))
239 {
Eric Pouech4c591d42003-05-20 04:00:42 +0000240 reply->signaled = cancel_timer( timer );
Alexandre Julliardad47a301999-11-29 01:58:35 +0000241 release_object( timer );
242 }
243}