d3d8: Add a dll global critical section.
[wine/multimedia.git] / server / timer.c
blobffa4b0354bddae765d362b300e85bca0275366c6
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 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 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 = 0;
91 timer->period = 0;
92 timer->timeout = NULL;
93 timer->thread = NULL;
96 return timer;
99 /* callback on timer expiration */
100 static void timer_callback( void *private )
102 struct timer *timer = (struct timer *)private;
104 /* queue an APC */
105 if (timer->thread)
107 apc_call_t data;
109 memset( &data, 0, sizeof(data) );
110 if (timer->callback)
112 data.type = APC_TIMER;
113 data.timer.func = timer->callback;
114 data.timer.time = timer->when;
115 data.timer.arg = timer->arg;
117 else data.type = APC_NONE; /* wake up only */
119 if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
121 release_object( timer->thread );
122 timer->thread = NULL;
126 if (timer->period) /* schedule the next expiration */
128 timer->when += (timeout_t)timer->period * 10000;
129 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
131 else timer->timeout = NULL;
133 /* wake up waiters */
134 timer->signaled = 1;
135 wake_up( &timer->obj, 0 );
138 /* cancel a running timer */
139 static int cancel_timer( struct timer *timer )
141 int signaled = timer->signaled;
143 if (timer->timeout)
145 remove_timeout_user( timer->timeout );
146 timer->timeout = NULL;
148 if (timer->thread)
150 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
151 release_object( timer->thread );
152 timer->thread = NULL;
154 return signaled;
157 /* set the timer expiration and period */
158 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
159 void *callback, void *arg )
161 int signaled = cancel_timer( timer );
162 if (timer->manual)
164 period = 0; /* period doesn't make any sense for a manual timer */
165 timer->signaled = 0;
167 if (expire > 0 && expire < current_time) expire = current_time;
168 timer->when = expire;
169 timer->period = period;
170 timer->callback = callback;
171 timer->arg = arg;
172 if (callback) timer->thread = (struct thread *)grab_object( current );
173 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
174 return signaled;
177 static void timer_dump( struct object *obj, int verbose )
179 struct timer *timer = (struct timer *)obj;
180 assert( obj->ops == &timer_ops );
181 fprintf( stderr, "Timer manual=%d when=%s period=%u ",
182 timer->manual, get_timeout_str(timer->when), timer->period );
183 dump_object_name( &timer->obj );
184 fputc( '\n', stderr );
187 static int timer_signaled( struct object *obj, struct thread *thread )
189 struct timer *timer = (struct timer *)obj;
190 assert( obj->ops == &timer_ops );
191 return timer->signaled;
194 static int timer_satisfied( struct object *obj, struct thread *thread )
196 struct timer *timer = (struct timer *)obj;
197 assert( obj->ops == &timer_ops );
198 if (!timer->manual) timer->signaled = 0;
199 return 0;
202 static unsigned int timer_map_access( struct object *obj, unsigned int access )
204 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
205 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
206 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
207 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
208 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
211 static void timer_destroy( struct object *obj )
213 struct timer *timer = (struct timer *)obj;
214 assert( obj->ops == &timer_ops );
216 if (timer->timeout) remove_timeout_user( timer->timeout );
217 if (timer->thread) release_object( timer->thread );
220 /* create a timer */
221 DECL_HANDLER(create_timer)
223 struct timer *timer;
224 struct unicode_str name;
225 struct directory *root = NULL;
227 reply->handle = 0;
228 get_req_unicode_str( &name );
229 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
230 return;
232 if ((timer = create_timer( root, &name, req->attributes, req->manual )))
234 reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
235 release_object( timer );
238 if (root) release_object( root );
241 /* open a handle to a timer */
242 DECL_HANDLER(open_timer)
244 struct unicode_str name;
245 struct directory *root = NULL;
246 struct timer *timer;
248 get_req_unicode_str( &name );
249 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
250 return;
252 if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
254 reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
255 release_object( timer );
258 if (root) release_object( root );
261 /* set a waitable timer */
262 DECL_HANDLER(set_timer)
264 struct timer *timer;
266 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
267 TIMER_MODIFY_STATE, &timer_ops )))
269 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
270 release_object( timer );
274 /* cancel a waitable timer */
275 DECL_HANDLER(cancel_timer)
277 struct timer *timer;
279 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
280 TIMER_MODIFY_STATE, &timer_ops )))
282 reply->signaled = cancel_timer( timer );
283 release_object( timer );
287 /* Get information on a waitable timer */
288 DECL_HANDLER(get_timer_info)
290 struct timer *timer;
292 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
293 TIMER_QUERY_STATE, &timer_ops )))
295 reply->when = timer->when;
296 reply->signaled = timer->signaled;
297 release_object( timer );