push fff380ca74633e3441fcec4153e8b8fd9a4ac57f
[wine/hacks.git] / server / timer.c
blobdd53020b44586f8ecd14882a42fe5f108461994e
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 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_open_file, /* open_file */
72 no_close_handle, /* close_handle */
73 timer_destroy /* destroy */
77 /* create a timer object */
78 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
79 unsigned int attr, int manual )
81 struct timer *timer;
83 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
85 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
87 /* initialize it if it didn't already exist */
88 timer->manual = manual;
89 timer->signaled = 0;
90 timer->when.tv_sec = 0;
91 timer->when.tv_usec = 0;
92 timer->period = 0;
93 timer->timeout = NULL;
94 timer->thread = NULL;
97 return timer;
100 /* callback on timer expiration */
101 static void timer_callback( void *private )
103 struct timer *timer = (struct timer *)private;
105 /* queue an APC */
106 if (timer->thread)
108 apc_call_t data;
110 memset( &data, 0, sizeof(data) );
111 if (timer->callback)
113 data.type = APC_TIMER;
114 data.timer.func = timer->callback;
115 data.timer.time.sec = timer->when.tv_sec;
116 data.timer.time.usec = timer->when.tv_usec;
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 add_timeout( &timer->when, timer->period );
131 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
133 else timer->timeout = NULL;
135 /* wake up waiters */
136 timer->signaled = 1;
137 wake_up( &timer->obj, 0 );
140 /* cancel a running timer */
141 static int cancel_timer( struct timer *timer )
143 int signaled = timer->signaled;
145 if (timer->timeout)
147 remove_timeout_user( timer->timeout );
148 timer->timeout = NULL;
150 if (timer->thread)
152 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
153 release_object( timer->thread );
154 timer->thread = NULL;
156 return signaled;
159 /* set the timer expiration and period */
160 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
161 void *callback, void *arg )
163 int signaled = cancel_timer( timer );
164 if (timer->manual)
166 period = 0; /* period doesn't make any sense for a manual timer */
167 timer->signaled = 0;
169 if (!expire->sec && !expire->usec)
171 /* special case: use now + period as first expiration */
172 timer->when = current_time;
173 add_timeout( &timer->when, period );
175 else
177 timer->when.tv_sec = expire->sec;
178 timer->when.tv_usec = expire->usec;
180 timer->period = period;
181 timer->callback = callback;
182 timer->arg = arg;
183 if (callback) timer->thread = (struct thread *)grab_object( current );
184 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
185 return signaled;
188 static void timer_dump( struct object *obj, int verbose )
190 struct timer *timer = (struct timer *)obj;
191 assert( obj->ops == &timer_ops );
192 fprintf( stderr, "Timer manual=%d when=%ld.%06u period=%d ",
193 timer->manual, timer->when.tv_sec, (unsigned int)timer->when.tv_usec, timer->period );
194 dump_object_name( &timer->obj );
195 fputc( '\n', stderr );
198 static int timer_signaled( struct object *obj, struct thread *thread )
200 struct timer *timer = (struct timer *)obj;
201 assert( obj->ops == &timer_ops );
202 return timer->signaled;
205 static int timer_satisfied( struct object *obj, struct thread *thread )
207 struct timer *timer = (struct timer *)obj;
208 assert( obj->ops == &timer_ops );
209 if (!timer->manual) timer->signaled = 0;
210 return 0;
213 static unsigned int timer_map_access( struct object *obj, unsigned int access )
215 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
216 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
217 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
218 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
219 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
222 static void timer_destroy( struct object *obj )
224 struct timer *timer = (struct timer *)obj;
225 assert( obj->ops == &timer_ops );
227 if (timer->timeout) remove_timeout_user( timer->timeout );
228 if (timer->thread) release_object( timer->thread );
231 /* create a timer */
232 DECL_HANDLER(create_timer)
234 struct timer *timer;
235 struct unicode_str name;
236 struct directory *root = NULL;
238 reply->handle = 0;
239 get_req_unicode_str( &name );
240 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
241 return;
243 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
245 reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
246 release_object( timer );
249 if (root) release_object( root );
252 /* open a handle to a timer */
253 DECL_HANDLER(open_timer)
255 struct unicode_str name;
256 struct directory *root = NULL;
257 struct timer *timer;
259 get_req_unicode_str( &name );
260 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
261 return;
263 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
265 reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
266 release_object( timer );
269 if (root) release_object( root );
272 /* set a waitable timer */
273 DECL_HANDLER(set_timer)
275 struct timer *timer;
277 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
278 TIMER_MODIFY_STATE, &timer_ops )))
280 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
281 release_object( timer );
285 /* cancel a waitable timer */
286 DECL_HANDLER(cancel_timer)
288 struct timer *timer;
290 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
291 TIMER_MODIFY_STATE, &timer_ops )))
293 reply->signaled = cancel_timer( timer );
294 release_object( timer );
298 /* Get information on a waitable timer */
299 DECL_HANDLER(get_timer_info)
301 struct timer *timer;
303 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
304 TIMER_QUERY_STATE, &timer_ops )))
306 reply->when.sec = timer->when.tv_sec;
307 reply->when.usec = timer->when.tv_usec;
308 reply->signaled = timer->signaled;
309 release_object( timer );