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 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 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 no_lookup_name, /* lookup_name */
71 no_close_handle, /* close_handle */
72 timer_destroy /* destroy */
76 /* create a timer object */
77 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
78 unsigned int attr, int manual )
82 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
84 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
86 /* initialize it if it didn't already exist */
87 timer->manual = manual;
89 timer->when.tv_sec = 0;
90 timer->when.tv_usec = 0;
92 timer->timeout = NULL;
99 /* callback on timer expiration */
100 static void timer_callback( void *private )
102 struct timer *timer = (struct timer *)private;
109 memset( &data, 0, sizeof(data) );
112 data.type = APC_TIMER;
113 data.timer.func = timer->callback;
114 data.timer.time.sec = timer->when.tv_sec;
115 data.timer.time.usec = timer->when.tv_usec;
116 data.timer.arg = timer->arg;
118 else data.type = APC_NONE; /* wake up only */
120 if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
122 release_object( timer->thread );
123 timer->thread = NULL;
127 if (timer->period) /* schedule the next expiration */
129 add_timeout( &timer->when, timer->period );
130 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
132 else timer->timeout = NULL;
134 /* wake up waiters */
136 wake_up( &timer->obj, 0 );
139 /* cancel a running timer */
140 static int cancel_timer( struct timer *timer )
142 int signaled = timer->signaled;
146 remove_timeout_user( timer->timeout );
147 timer->timeout = NULL;
151 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
152 release_object( timer->thread );
153 timer->thread = NULL;
158 /* set the timer expiration and period */
159 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
160 void *callback, void *arg )
162 int signaled = cancel_timer( timer );
165 period = 0; /* period doesn't make any sense for a manual timer */
168 if (!expire->sec && !expire->usec)
170 /* special case: use now + period as first expiration */
171 timer->when = current_time;
172 add_timeout( &timer->when, period );
176 timer->when.tv_sec = expire->sec;
177 timer->when.tv_usec = expire->usec;
179 timer->period = period;
180 timer->callback = callback;
182 if (callback) timer->thread = (struct thread *)grab_object( current );
183 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
187 static void timer_dump( struct object *obj, int verbose )
189 struct timer *timer = (struct timer *)obj;
190 assert( obj->ops == &timer_ops );
191 fprintf( stderr, "Timer manual=%d when=%ld.%06u period=%d ",
192 timer->manual, timer->when.tv_sec, (unsigned int)timer->when.tv_usec, timer->period );
193 dump_object_name( &timer->obj );
194 fputc( '\n', stderr );
197 static int timer_signaled( struct object *obj, struct thread *thread )
199 struct timer *timer = (struct timer *)obj;
200 assert( obj->ops == &timer_ops );
201 return timer->signaled;
204 static int timer_satisfied( struct object *obj, struct thread *thread )
206 struct timer *timer = (struct timer *)obj;
207 assert( obj->ops == &timer_ops );
208 if (!timer->manual) timer->signaled = 0;
212 static unsigned int timer_map_access( struct object *obj, unsigned int access )
214 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
215 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
216 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
217 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
218 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
221 static void timer_destroy( struct object *obj )
223 struct timer *timer = (struct timer *)obj;
224 assert( obj->ops == &timer_ops );
226 if (timer->timeout) remove_timeout_user( timer->timeout );
227 if (timer->thread) release_object( timer->thread );
231 DECL_HANDLER(create_timer)
234 struct unicode_str name;
235 struct directory *root = NULL;
238 get_req_unicode_str( &name );
239 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
242 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
244 reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
245 release_object( timer );
248 if (root) release_object( root );
251 /* open a handle to a timer */
252 DECL_HANDLER(open_timer)
254 struct unicode_str name;
255 struct directory *root = NULL;
258 get_req_unicode_str( &name );
259 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
262 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
264 reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
265 release_object( timer );
268 if (root) release_object( root );
271 /* set a waitable timer */
272 DECL_HANDLER(set_timer)
276 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
277 TIMER_MODIFY_STATE, &timer_ops )))
279 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
280 release_object( timer );
284 /* cancel a waitable timer */
285 DECL_HANDLER(cancel_timer)
289 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
290 TIMER_MODIFY_STATE, &timer_ops )))
292 reply->signaled = cancel_timer( timer );
293 release_object( timer );
297 /* Get information on a waitable timer */
298 DECL_HANDLER(get_timer_info)
302 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
303 TIMER_QUERY_STATE, &timer_ops )))
305 reply->when.sec = timer->when.tv_sec;
306 reply->when.usec = timer->when.tv_usec;
307 reply->signaled = timer->signaled;
308 release_object( timer );