wineconsole: Remove some unnecessary typecasts.
[wine/wine-gecko.git] / server / timer.c
blob436ed7cd3af5b7335735f410ff451e29e6827652
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 struct timer
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 */
48 struct thread *thread; /* thread that set the APC function */
49 void *callback; /* callback APC function */
50 void *arg; /* callback argument */
53 static void timer_dump( struct object *obj, int verbose );
54 static int timer_signaled( struct object *obj, struct thread *thread );
55 static int timer_satisfied( struct object *obj, struct thread *thread );
56 static void timer_destroy( struct object *obj );
58 static const struct object_ops timer_ops =
60 sizeof(struct timer), /* size */
61 timer_dump, /* dump */
62 add_queue, /* add_queue */
63 remove_queue, /* remove_queue */
64 timer_signaled, /* signaled */
65 timer_satisfied, /* satisfied */
66 no_signal, /* signal */
67 no_get_fd, /* get_fd */
68 no_lookup_name, /* lookup_name */
69 no_close_handle, /* close_handle */
70 timer_destroy /* destroy */
74 /* create a timer object */
75 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
76 unsigned int attr, int manual )
78 struct timer *timer;
80 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
82 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
84 /* initialize it if it didn't already exist */
85 timer->manual = manual;
86 timer->signaled = 0;
87 timer->when.tv_sec = 0;
88 timer->when.tv_usec = 0;
89 timer->period = 0;
90 timer->timeout = NULL;
91 timer->thread = NULL;
94 return timer;
97 /* callback on timer expiration */
98 static void timer_callback( void *private )
100 struct timer *timer = (struct timer *)private;
102 /* queue an APC */
103 if (timer->thread)
105 if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0,
106 (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
108 release_object( timer->thread );
109 timer->thread = NULL;
113 if (timer->period) /* schedule the next expiration */
115 add_timeout( &timer->when, timer->period );
116 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
118 else timer->timeout = NULL;
120 /* wake up waiters */
121 timer->signaled = 1;
122 wake_up( &timer->obj, 0 );
125 /* cancel a running timer */
126 static int cancel_timer( struct timer *timer )
128 int signaled = timer->signaled;
130 if (timer->timeout)
132 remove_timeout_user( timer->timeout );
133 timer->timeout = NULL;
135 if (timer->thread)
137 thread_cancel_apc( timer->thread, &timer->obj, 0 );
138 release_object( timer->thread );
139 timer->thread = NULL;
141 return signaled;
144 /* set the timer expiration and period */
145 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
146 void *callback, void *arg )
148 int signaled = cancel_timer( timer );
149 if (timer->manual)
151 period = 0; /* period doesn't make any sense for a manual timer */
152 timer->signaled = 0;
154 if (!expire->sec && !expire->usec)
156 /* special case: use now + period as first expiration */
157 gettimeofday( &timer->when, NULL );
158 add_timeout( &timer->when, period );
160 else
162 timer->when.tv_sec = expire->sec;
163 timer->when.tv_usec = expire->usec;
165 timer->period = period;
166 timer->callback = callback;
167 timer->arg = arg;
168 if (callback) timer->thread = (struct thread *)grab_object( current );
169 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
170 return signaled;
173 static void timer_dump( struct object *obj, int verbose )
175 struct timer *timer = (struct timer *)obj;
176 assert( obj->ops == &timer_ops );
177 fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
178 timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
179 dump_object_name( &timer->obj );
180 fputc( '\n', stderr );
183 static int timer_signaled( struct object *obj, struct thread *thread )
185 struct timer *timer = (struct timer *)obj;
186 assert( obj->ops == &timer_ops );
187 return timer->signaled;
190 static int timer_satisfied( struct object *obj, struct thread *thread )
192 struct timer *timer = (struct timer *)obj;
193 assert( obj->ops == &timer_ops );
194 if (!timer->manual) timer->signaled = 0;
195 return 0;
198 static void timer_destroy( struct object *obj )
200 struct timer *timer = (struct timer *)obj;
201 assert( obj->ops == &timer_ops );
203 if (timer->timeout) remove_timeout_user( timer->timeout );
204 if (timer->thread) release_object( timer->thread );
207 /* create a timer */
208 DECL_HANDLER(create_timer)
210 struct timer *timer;
211 struct unicode_str name;
212 struct directory *root = NULL;
214 reply->handle = 0;
215 get_req_unicode_str( &name );
216 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
217 return;
219 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
221 reply->handle = alloc_handle( current->process, timer, req->access,
222 req->attributes & OBJ_INHERIT );
223 release_object( timer );
226 if (root) release_object( root );
229 /* open a handle to a timer */
230 DECL_HANDLER(open_timer)
232 struct unicode_str name;
233 struct directory *root = NULL;
234 struct timer *timer;
236 get_req_unicode_str( &name );
237 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
238 return;
240 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
242 reply->handle = alloc_handle( current->process, &timer->obj, req->access,
243 req->attributes & OBJ_INHERIT );
244 release_object( timer );
247 if (root) release_object( root );
250 /* set a waitable timer */
251 DECL_HANDLER(set_timer)
253 struct timer *timer;
255 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
256 TIMER_MODIFY_STATE, &timer_ops )))
258 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
259 release_object( timer );
263 /* cancel a waitable timer */
264 DECL_HANDLER(cancel_timer)
266 struct timer *timer;
268 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
269 TIMER_MODIFY_STATE, &timer_ops )))
271 reply->signaled = cancel_timer( timer );
272 release_object( timer );
276 /* Get information on a waitable timer */
277 DECL_HANDLER(get_timer_info)
279 struct timer *timer;
281 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
282 TIMER_QUERY_STATE, &timer_ops )))
284 reply->when.sec = timer->when.tv_sec;
285 reply->when.usec = timer->when.tv_usec;
286 reply->signaled = timer->signaled;
287 release_object( timer );