amstream: Increase parent IAMMediaStream refcount in IDirectDrawMediaStream::CreateSa...
[wine.git] / server / timer.c
blob6460acbf51963e19a9aaeaf4016c4605dee47596
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 abstime_t when; /* next expiration */
47 struct timeout_user *timeout; /* timeout user */
48 struct thread *thread; /* thread that set the APC function */
49 client_ptr_t callback; /* callback APC function */
50 client_ptr_t arg; /* callback argument */
53 static void timer_dump( struct object *obj, int verbose );
54 static struct object_type *timer_get_type( struct object *obj );
55 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
56 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
57 static unsigned int timer_map_access( struct object *obj, unsigned int access );
58 static void timer_destroy( struct object *obj );
60 static const struct object_ops timer_ops =
62 sizeof(struct timer), /* size */
63 timer_dump, /* dump */
64 timer_get_type, /* get_type */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 timer_signaled, /* signaled */
68 timer_satisfied, /* satisfied */
69 no_signal, /* signal */
70 no_get_fd, /* get_fd */
71 timer_map_access, /* map_access */
72 default_get_sd, /* get_sd */
73 default_set_sd, /* set_sd */
74 no_lookup_name, /* lookup_name */
75 directory_link_name, /* link_name */
76 default_unlink_name, /* unlink_name */
77 no_open_file, /* open_file */
78 no_kernel_obj_list, /* get_kernel_obj_list */
79 no_close_handle, /* close_handle */
80 timer_destroy /* destroy */
84 /* create a timer object */
85 static struct timer *create_timer( struct object *root, const struct unicode_str *name,
86 unsigned int attr, int manual, const struct security_descriptor *sd )
88 struct timer *timer;
90 if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
92 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
94 /* initialize it if it didn't already exist */
95 timer->manual = manual;
96 timer->signaled = 0;
97 timer->when = 0;
98 timer->period = 0;
99 timer->timeout = NULL;
100 timer->thread = NULL;
103 return timer;
106 /* callback on timer expiration */
107 static void timer_callback( void *private )
109 struct timer *timer = (struct timer *)private;
111 /* queue an APC */
112 if (timer->thread)
114 apc_call_t data;
116 memset( &data, 0, sizeof(data) );
117 if (timer->callback)
119 data.type = APC_TIMER;
120 data.user.timer.func = timer->callback;
121 data.user.timer.time = timer->when;
122 data.user.timer.arg = timer->arg;
124 else data.type = APC_NONE; /* wake up only */
126 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
128 release_object( timer->thread );
129 timer->thread = NULL;
133 if (timer->period) /* schedule the next expiration */
135 if (timer->when > 0) timer->when = -monotonic_time;
136 timer->when -= (abstime_t)timer->period * 10000;
137 timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
139 else timer->timeout = NULL;
141 /* wake up waiters */
142 timer->signaled = 1;
143 wake_up( &timer->obj, 0 );
146 /* cancel a running timer */
147 static int cancel_timer( struct timer *timer )
149 int signaled = timer->signaled;
151 if (timer->timeout)
153 remove_timeout_user( timer->timeout );
154 timer->timeout = NULL;
156 if (timer->thread)
158 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
159 release_object( timer->thread );
160 timer->thread = NULL;
162 return signaled;
165 /* set the timer expiration and period */
166 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
167 client_ptr_t callback, client_ptr_t arg )
169 int signaled = cancel_timer( timer );
170 if (timer->manual)
172 period = 0; /* period doesn't make any sense for a manual timer */
173 timer->signaled = 0;
175 timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
176 timer->period = period;
177 timer->callback = callback;
178 timer->arg = arg;
179 if (callback) timer->thread = (struct thread *)grab_object( current );
180 timer->timeout = add_timeout_user( expire, timer_callback, timer );
181 return signaled;
184 static void timer_dump( struct object *obj, int verbose )
186 struct timer *timer = (struct timer *)obj;
187 timeout_t timeout = abstime_to_timeout( timer->when );
188 assert( obj->ops == &timer_ops );
189 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
190 timer->manual, get_timeout_str(timeout), timer->period );
193 static struct object_type *timer_get_type( struct object *obj )
195 static const WCHAR name[] = {'T','i','m','e','r'};
196 static const struct unicode_str str = { name, sizeof(name) };
197 return get_object_type( &str );
200 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
202 struct timer *timer = (struct timer *)obj;
203 assert( obj->ops == &timer_ops );
204 return timer->signaled;
207 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
209 struct timer *timer = (struct timer *)obj;
210 assert( obj->ops == &timer_ops );
211 if (!timer->manual) timer->signaled = 0;
214 static unsigned int timer_map_access( struct object *obj, unsigned int access )
216 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
217 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
218 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
219 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
220 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
223 static void timer_destroy( struct object *obj )
225 struct timer *timer = (struct timer *)obj;
226 assert( obj->ops == &timer_ops );
228 if (timer->timeout) remove_timeout_user( timer->timeout );
229 if (timer->thread) release_object( timer->thread );
232 /* create a timer */
233 DECL_HANDLER(create_timer)
235 struct timer *timer;
236 struct unicode_str name;
237 struct object *root;
238 const struct security_descriptor *sd;
239 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
241 if (!objattr) return;
243 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
245 reply->handle = alloc_handle( current->process, timer, req->access, objattr->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 = get_req_unicode_str();
257 reply->handle = open_object( current->process, req->rootdir, req->access,
258 &timer_ops, &name, req->attributes );
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 );