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
22 #include "wine/port.h"
28 #include <sys/types.h>
32 #define WIN32_NO_STATUS
42 struct object obj; /* object header */
43 int manual; /* manual reset */
44 int signaled; /* current signaled state */
45 unsigned int period; /* timer period in ms */
46 timeout_t 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 unsigned int timer_map_access( struct object *obj, unsigned int access );
57 static void timer_destroy( struct object *obj );
59 static const struct object_ops timer_ops =
61 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 */
67 no_signal, /* signal */
68 no_get_fd, /* get_fd */
69 timer_map_access, /* map_access */
70 default_get_sd, /* get_sd */
71 default_set_sd, /* set_sd */
72 no_lookup_name, /* lookup_name */
73 no_open_file, /* open_file */
74 no_close_handle, /* close_handle */
75 timer_destroy /* destroy */
79 /* create a timer object */
80 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
81 unsigned int attr, int manual )
85 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
87 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
89 /* initialize it if it didn't already exist */
90 timer->manual = manual;
94 timer->timeout = NULL;
101 /* callback on timer expiration */
102 static void timer_callback( void *private )
104 struct timer *timer = (struct timer *)private;
111 memset( &data, 0, sizeof(data) );
114 data.type = APC_TIMER;
115 data.timer.func = timer->callback;
116 data.timer.time = timer->when;
117 data.timer.arg = timer->arg;
119 else data.type = APC_NONE; /* wake up only */
121 if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
123 release_object( timer->thread );
124 timer->thread = NULL;
128 if (timer->period) /* schedule the next expiration */
130 timer->when += (timeout_t)timer->period * 10000;
131 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
133 else timer->timeout = NULL;
135 /* wake up waiters */
137 wake_up( &timer->obj, 0 );
140 /* cancel a running timer */
141 static int cancel_timer( struct timer *timer )
143 int signaled = timer->signaled;
147 remove_timeout_user( timer->timeout );
148 timer->timeout = NULL;
152 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
153 release_object( timer->thread );
154 timer->thread = NULL;
159 /* set the timer expiration and period */
160 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
161 void *callback, void *arg )
163 int signaled = cancel_timer( timer );
166 period = 0; /* period doesn't make any sense for a manual timer */
169 timer->when = (expire <= 0) ? current_time - expire : max( expire, current_time );
170 timer->period = period;
171 timer->callback = callback;
173 if (callback) timer->thread = (struct thread *)grab_object( current );
174 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
178 static void timer_dump( struct object *obj, int verbose )
180 struct timer *timer = (struct timer *)obj;
181 assert( obj->ops == &timer_ops );
182 fprintf( stderr, "Timer manual=%d when=%s period=%u ",
183 timer->manual, get_timeout_str(timer->when), timer->period );
184 dump_object_name( &timer->obj );
185 fputc( '\n', stderr );
188 static int timer_signaled( struct object *obj, struct thread *thread )
190 struct timer *timer = (struct timer *)obj;
191 assert( obj->ops == &timer_ops );
192 return timer->signaled;
195 static int timer_satisfied( struct object *obj, struct thread *thread )
197 struct timer *timer = (struct timer *)obj;
198 assert( obj->ops == &timer_ops );
199 if (!timer->manual) timer->signaled = 0;
203 static unsigned int timer_map_access( struct object *obj, unsigned int access )
205 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
206 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
207 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
208 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
209 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
212 static void timer_destroy( struct object *obj )
214 struct timer *timer = (struct timer *)obj;
215 assert( obj->ops == &timer_ops );
217 if (timer->timeout) remove_timeout_user( timer->timeout );
218 if (timer->thread) release_object( timer->thread );
222 DECL_HANDLER(create_timer)
225 struct unicode_str name;
226 struct directory *root = NULL;
229 get_req_unicode_str( &name );
230 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
233 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
235 reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
236 release_object( timer );
239 if (root) release_object( root );
242 /* open a handle to a timer */
243 DECL_HANDLER(open_timer)
245 struct unicode_str name;
246 struct directory *root = NULL;
249 get_req_unicode_str( &name );
250 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
253 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
255 reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
256 release_object( timer );
259 if (root) release_object( root );
262 /* set a waitable timer */
263 DECL_HANDLER(set_timer)
267 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
268 TIMER_MODIFY_STATE, &timer_ops )))
270 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
271 release_object( timer );
275 /* cancel a waitable timer */
276 DECL_HANDLER(cancel_timer)
280 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
281 TIMER_MODIFY_STATE, &timer_ops )))
283 reply->signaled = cancel_timer( timer );
284 release_object( timer );
288 /* Get information on a waitable timer */
289 DECL_HANDLER(get_timer_info)
293 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
294 TIMER_QUERY_STATE, &timer_ops )))
296 reply->when = timer->when;
297 reply->signaled = timer->signaled;
298 release_object( timer );