cmd: Inline some simple extern WCHAR strings.
[wine.git] / server / timer.c
blobd4e5c56267ca94f8c4af5b30252e6b8380c723dc
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 default_get_full_name, /* get_full_name */
75 no_lookup_name, /* lookup_name */
76 directory_link_name, /* link_name */
77 default_unlink_name, /* unlink_name */
78 no_open_file, /* open_file */
79 no_kernel_obj_list, /* get_kernel_obj_list */
80 no_close_handle, /* close_handle */
81 timer_destroy /* destroy */
85 /* create a timer object */
86 static struct timer *create_timer( struct object *root, const struct unicode_str *name,
87 unsigned int attr, int manual, const struct security_descriptor *sd )
89 struct timer *timer;
91 if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
93 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
95 /* initialize it if it didn't already exist */
96 timer->manual = manual;
97 timer->signaled = 0;
98 timer->when = 0;
99 timer->period = 0;
100 timer->timeout = NULL;
101 timer->thread = NULL;
104 return timer;
107 /* callback on timer expiration */
108 static void timer_callback( void *private )
110 struct timer *timer = (struct timer *)private;
112 /* queue an APC */
113 if (timer->thread)
115 apc_call_t data;
117 memset( &data, 0, sizeof(data) );
118 if (timer->callback)
120 data.type = APC_TIMER;
121 data.user.timer.func = timer->callback;
122 data.user.timer.time = timer->when;
123 data.user.timer.arg = timer->arg;
125 else data.type = APC_NONE; /* wake up only */
127 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
129 release_object( timer->thread );
130 timer->thread = NULL;
134 if (timer->period) /* schedule the next expiration */
136 if (timer->when > 0) timer->when = -monotonic_time;
137 timer->when -= (abstime_t)timer->period * 10000;
138 timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
140 else timer->timeout = NULL;
142 /* wake up waiters */
143 timer->signaled = 1;
144 wake_up( &timer->obj, 0 );
147 /* cancel a running timer */
148 static int cancel_timer( struct timer *timer )
150 int signaled = timer->signaled;
152 if (timer->timeout)
154 remove_timeout_user( timer->timeout );
155 timer->timeout = NULL;
157 if (timer->thread)
159 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
160 release_object( timer->thread );
161 timer->thread = NULL;
163 return signaled;
166 /* set the timer expiration and period */
167 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
168 client_ptr_t callback, client_ptr_t arg )
170 int signaled = cancel_timer( timer );
171 if (timer->manual)
173 period = 0; /* period doesn't make any sense for a manual timer */
174 timer->signaled = 0;
176 timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
177 timer->period = period;
178 timer->callback = callback;
179 timer->arg = arg;
180 if (callback) timer->thread = (struct thread *)grab_object( current );
181 if (expire != TIMEOUT_INFINITE)
182 timer->timeout = add_timeout_user( expire, timer_callback, timer );
183 return signaled;
186 static void timer_dump( struct object *obj, int verbose )
188 struct timer *timer = (struct timer *)obj;
189 timeout_t timeout = abstime_to_timeout( timer->when );
190 assert( obj->ops == &timer_ops );
191 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
192 timer->manual, get_timeout_str(timeout), timer->period );
195 static struct object_type *timer_get_type( struct object *obj )
197 static const WCHAR name[] = {'T','i','m','e','r'};
198 static const struct unicode_str str = { name, sizeof(name) };
199 return get_object_type( &str );
202 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
204 struct timer *timer = (struct timer *)obj;
205 assert( obj->ops == &timer_ops );
206 return timer->signaled;
209 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
211 struct timer *timer = (struct timer *)obj;
212 assert( obj->ops == &timer_ops );
213 if (!timer->manual) timer->signaled = 0;
216 static unsigned int timer_map_access( struct object *obj, unsigned int access )
218 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
219 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
220 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
221 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
222 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
225 static void timer_destroy( struct object *obj )
227 struct timer *timer = (struct timer *)obj;
228 assert( obj->ops == &timer_ops );
230 if (timer->timeout) remove_timeout_user( timer->timeout );
231 if (timer->thread) release_object( timer->thread );
234 /* create a timer */
235 DECL_HANDLER(create_timer)
237 struct timer *timer;
238 struct unicode_str name;
239 struct object *root;
240 const struct security_descriptor *sd;
241 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
243 if (!objattr) return;
245 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
247 reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
248 release_object( timer );
251 if (root) release_object( root );
254 /* open a handle to a timer */
255 DECL_HANDLER(open_timer)
257 struct unicode_str name = get_req_unicode_str();
259 reply->handle = open_object( current->process, req->rootdir, req->access,
260 &timer_ops, &name, req->attributes );
263 /* set a waitable timer */
264 DECL_HANDLER(set_timer)
266 struct timer *timer;
268 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
269 TIMER_MODIFY_STATE, &timer_ops )))
271 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
272 release_object( timer );
276 /* cancel a waitable timer */
277 DECL_HANDLER(cancel_timer)
279 struct timer *timer;
281 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
282 TIMER_MODIFY_STATE, &timer_ops )))
284 reply->signaled = cancel_timer( timer );
285 release_object( timer );
289 /* Get information on a waitable timer */
290 DECL_HANDLER(get_timer_info)
292 struct timer *timer;
294 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
295 TIMER_QUERY_STATE, &timer_ops )))
297 reply->when = timer->when;
298 reply->signaled = timer->signaled;
299 release_object( timer );