vbscript: Fix memory leak in Split().
[wine.git] / server / timer.c
blob96dc9d00ca1dbdb422fb57b9d55dd885d7eb1e40
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"
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <stdarg.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
35 #include "file.h"
36 #include "handle.h"
37 #include "request.h"
39 static const WCHAR timer_name[] = {'T','i','m','e','r'};
41 struct type_descr timer_type =
43 { timer_name, sizeof(timer_name) }, /* name */
44 TIMER_ALL_ACCESS, /* valid_access */
45 { /* mapping */
46 STANDARD_RIGHTS_READ | TIMER_QUERY_STATE,
47 STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE,
48 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
49 TIMER_ALL_ACCESS
53 struct timer
55 struct object obj; /* object header */
56 int manual; /* manual reset */
57 int signaled; /* current signaled state */
58 unsigned int period; /* timer period in ms */
59 abstime_t when; /* next expiration */
60 struct timeout_user *timeout; /* timeout user */
61 struct thread *thread; /* thread that set the APC function */
62 client_ptr_t callback; /* callback APC function */
63 client_ptr_t arg; /* callback argument */
66 static void timer_dump( struct object *obj, int verbose );
67 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
68 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
69 static void timer_destroy( struct object *obj );
71 static const struct object_ops timer_ops =
73 sizeof(struct timer), /* size */
74 &timer_type, /* type */
75 timer_dump, /* dump */
76 add_queue, /* add_queue */
77 remove_queue, /* remove_queue */
78 timer_signaled, /* signaled */
79 timer_satisfied, /* satisfied */
80 no_signal, /* signal */
81 no_get_fd, /* get_fd */
82 default_map_access, /* map_access */
83 default_get_sd, /* get_sd */
84 default_set_sd, /* set_sd */
85 default_get_full_name, /* get_full_name */
86 no_lookup_name, /* lookup_name */
87 directory_link_name, /* link_name */
88 default_unlink_name, /* unlink_name */
89 no_open_file, /* open_file */
90 no_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 timer_destroy /* destroy */
96 /* create a timer object */
97 static struct timer *create_timer( struct object *root, const struct unicode_str *name,
98 unsigned int attr, int manual, const struct security_descriptor *sd )
100 struct timer *timer;
102 if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
104 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
106 /* initialize it if it didn't already exist */
107 timer->manual = manual;
108 timer->signaled = 0;
109 timer->when = 0;
110 timer->period = 0;
111 timer->timeout = NULL;
112 timer->thread = NULL;
115 return timer;
118 /* callback on timer expiration */
119 static void timer_callback( void *private )
121 struct timer *timer = (struct timer *)private;
123 /* queue an APC */
124 if (timer->thread)
126 apc_call_t data;
128 assert (timer->callback);
129 memset( &data, 0, sizeof(data) );
130 data.type = APC_USER;
131 data.user.func = timer->callback;
132 data.user.args[0] = timer->arg;
133 data.user.args[1] = (unsigned int)timer->when;
134 data.user.args[2] = timer->when >> 32;
136 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
138 release_object( timer->thread );
139 timer->thread = NULL;
143 if (timer->period) /* schedule the next expiration */
145 if (timer->when > 0) timer->when = -monotonic_time;
146 timer->when -= (abstime_t)timer->period * 10000;
147 timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
149 else timer->timeout = NULL;
151 /* wake up waiters */
152 timer->signaled = 1;
153 wake_up( &timer->obj, 0 );
156 /* cancel a running timer */
157 static int cancel_timer( struct timer *timer )
159 int signaled = timer->signaled;
161 if (timer->timeout)
163 remove_timeout_user( timer->timeout );
164 timer->timeout = NULL;
166 if (timer->thread)
168 thread_cancel_apc( timer->thread, &timer->obj, APC_USER );
169 release_object( timer->thread );
170 timer->thread = NULL;
172 return signaled;
175 /* set the timer expiration and period */
176 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
177 client_ptr_t callback, client_ptr_t arg )
179 int signaled = cancel_timer( timer );
180 if (timer->manual)
182 period = 0; /* period doesn't make any sense for a manual timer */
183 timer->signaled = 0;
185 timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
186 timer->period = period;
187 timer->callback = callback;
188 timer->arg = arg;
189 if (callback) timer->thread = (struct thread *)grab_object( current );
190 if (expire != TIMEOUT_INFINITE)
191 timer->timeout = add_timeout_user( expire, timer_callback, timer );
192 return signaled;
195 static void timer_dump( struct object *obj, int verbose )
197 struct timer *timer = (struct timer *)obj;
198 timeout_t timeout = abstime_to_timeout( timer->when );
199 assert( obj->ops == &timer_ops );
200 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
201 timer->manual, get_timeout_str(timeout), timer->period );
204 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
206 struct timer *timer = (struct timer *)obj;
207 assert( obj->ops == &timer_ops );
208 return timer->signaled;
211 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
213 struct timer *timer = (struct timer *)obj;
214 assert( obj->ops == &timer_ops );
215 if (!timer->manual) timer->signaled = 0;
218 static void timer_destroy( struct object *obj )
220 struct timer *timer = (struct timer *)obj;
221 assert( obj->ops == &timer_ops );
223 if (timer->timeout) remove_timeout_user( timer->timeout );
224 if (timer->thread) release_object( timer->thread );
227 /* create a timer */
228 DECL_HANDLER(create_timer)
230 struct timer *timer;
231 struct unicode_str name;
232 struct object *root;
233 const struct security_descriptor *sd;
234 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
236 if (!objattr) return;
238 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
240 reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
241 release_object( timer );
244 if (root) release_object( root );
247 /* open a handle to a timer */
248 DECL_HANDLER(open_timer)
250 struct unicode_str name = get_req_unicode_str();
252 reply->handle = open_object( current->process, req->rootdir, req->access,
253 &timer_ops, &name, req->attributes );
256 /* set a waitable timer */
257 DECL_HANDLER(set_timer)
259 struct timer *timer;
261 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
262 TIMER_MODIFY_STATE, &timer_ops )))
264 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
265 release_object( timer );
269 /* cancel a waitable timer */
270 DECL_HANDLER(cancel_timer)
272 struct timer *timer;
274 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
275 TIMER_MODIFY_STATE, &timer_ops )))
277 reply->signaled = cancel_timer( timer );
278 release_object( timer );
282 /* Get information on a waitable timer */
283 DECL_HANDLER(get_timer_info)
285 struct timer *timer;
287 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
288 TIMER_QUERY_STATE, &timer_ops )))
290 reply->when = timer->when;
291 reply->signaled = timer->signaled;
292 release_object( timer );