jscript: Support passing EXPRVAL_JSVAL through JS stack.
[wine/zf.git] / server / timer.c
blob49483d9ae137f38474873193d08fbbc6a4748033
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 static const WCHAR timer_name[] = {'T','i','m','e','r'};
42 struct type_descr timer_type =
44 { timer_name, sizeof(timer_name) }, /* name */
45 TIMER_ALL_ACCESS, /* valid_access */
46 { /* mapping */
47 STANDARD_RIGHTS_READ | TIMER_QUERY_STATE,
48 STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE,
49 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
50 TIMER_ALL_ACCESS
54 struct timer
56 struct object obj; /* object header */
57 int manual; /* manual reset */
58 int signaled; /* current signaled state */
59 unsigned int period; /* timer period in ms */
60 abstime_t when; /* next expiration */
61 struct timeout_user *timeout; /* timeout user */
62 struct thread *thread; /* thread that set the APC function */
63 client_ptr_t callback; /* callback APC function */
64 client_ptr_t arg; /* callback argument */
67 static void timer_dump( struct object *obj, int verbose );
68 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
69 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
70 static void timer_destroy( struct object *obj );
72 static const struct object_ops timer_ops =
74 sizeof(struct timer), /* size */
75 &timer_type, /* type */
76 timer_dump, /* dump */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 timer_signaled, /* signaled */
80 timer_satisfied, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 default_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 default_get_full_name, /* get_full_name */
87 no_lookup_name, /* lookup_name */
88 directory_link_name, /* link_name */
89 default_unlink_name, /* unlink_name */
90 no_open_file, /* open_file */
91 no_kernel_obj_list, /* get_kernel_obj_list */
92 no_close_handle, /* close_handle */
93 timer_destroy /* destroy */
97 /* create a timer object */
98 static struct timer *create_timer( struct object *root, const struct unicode_str *name,
99 unsigned int attr, int manual, const struct security_descriptor *sd )
101 struct timer *timer;
103 if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
105 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
107 /* initialize it if it didn't already exist */
108 timer->manual = manual;
109 timer->signaled = 0;
110 timer->when = 0;
111 timer->period = 0;
112 timer->timeout = NULL;
113 timer->thread = NULL;
116 return timer;
119 /* callback on timer expiration */
120 static void timer_callback( void *private )
122 struct timer *timer = (struct timer *)private;
124 /* queue an APC */
125 if (timer->thread)
127 apc_call_t data;
129 memset( &data, 0, sizeof(data) );
130 if (timer->callback)
132 data.type = APC_TIMER;
133 data.user.timer.func = timer->callback;
134 data.user.timer.time = timer->when;
135 data.user.timer.arg = timer->arg;
137 else data.type = APC_NONE; /* wake up only */
139 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
141 release_object( timer->thread );
142 timer->thread = NULL;
146 if (timer->period) /* schedule the next expiration */
148 if (timer->when > 0) timer->when = -monotonic_time;
149 timer->when -= (abstime_t)timer->period * 10000;
150 timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
152 else timer->timeout = NULL;
154 /* wake up waiters */
155 timer->signaled = 1;
156 wake_up( &timer->obj, 0 );
159 /* cancel a running timer */
160 static int cancel_timer( struct timer *timer )
162 int signaled = timer->signaled;
164 if (timer->timeout)
166 remove_timeout_user( timer->timeout );
167 timer->timeout = NULL;
169 if (timer->thread)
171 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
172 release_object( timer->thread );
173 timer->thread = NULL;
175 return signaled;
178 /* set the timer expiration and period */
179 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
180 client_ptr_t callback, client_ptr_t arg )
182 int signaled = cancel_timer( timer );
183 if (timer->manual)
185 period = 0; /* period doesn't make any sense for a manual timer */
186 timer->signaled = 0;
188 timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
189 timer->period = period;
190 timer->callback = callback;
191 timer->arg = arg;
192 if (callback) timer->thread = (struct thread *)grab_object( current );
193 if (expire != TIMEOUT_INFINITE)
194 timer->timeout = add_timeout_user( expire, timer_callback, timer );
195 return signaled;
198 static void timer_dump( struct object *obj, int verbose )
200 struct timer *timer = (struct timer *)obj;
201 timeout_t timeout = abstime_to_timeout( timer->when );
202 assert( obj->ops == &timer_ops );
203 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
204 timer->manual, get_timeout_str(timeout), timer->period );
207 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
209 struct timer *timer = (struct timer *)obj;
210 assert( obj->ops == &timer_ops );
211 return timer->signaled;
214 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
216 struct timer *timer = (struct timer *)obj;
217 assert( obj->ops == &timer_ops );
218 if (!timer->manual) timer->signaled = 0;
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 );
230 /* create a timer */
231 DECL_HANDLER(create_timer)
233 struct timer *timer;
234 struct unicode_str name;
235 struct object *root;
236 const struct security_descriptor *sd;
237 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
239 if (!objattr) return;
241 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
243 reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
244 release_object( timer );
247 if (root) release_object( root );
250 /* open a handle to a timer */
251 DECL_HANDLER(open_timer)
253 struct unicode_str name = get_req_unicode_str();
255 reply->handle = open_object( current->process, req->rootdir, req->access,
256 &timer_ops, &name, req->attributes );
259 /* set a waitable timer */
260 DECL_HANDLER(set_timer)
262 struct timer *timer;
264 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
265 TIMER_MODIFY_STATE, &timer_ops )))
267 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
268 release_object( timer );
272 /* cancel a waitable timer */
273 DECL_HANDLER(cancel_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 = cancel_timer( timer );
281 release_object( timer );
285 /* Get information on a waitable timer */
286 DECL_HANDLER(get_timer_info)
288 struct timer *timer;
290 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
291 TIMER_QUERY_STATE, &timer_ops )))
293 reply->when = timer->when;
294 reply->signaled = timer->signaled;
295 release_object( timer );