server: Use the link_name operation to create named objects.
[wine.git] / server / timer.c
blob0d81d90f36242cee57bc4ec777fcb9d023020144
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 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_close_handle, /* close_handle */
79 timer_destroy /* destroy */
83 /* create a timer object */
84 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
85 unsigned int attr, int manual, const struct security_descriptor *sd )
87 struct timer *timer;
89 if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
91 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
93 /* initialize it if it didn't already exist */
94 timer->manual = manual;
95 timer->signaled = 0;
96 timer->when = 0;
97 timer->period = 0;
98 timer->timeout = NULL;
99 timer->thread = NULL;
100 if (sd) default_set_sd( &timer->obj, sd,
101 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
102 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
105 return timer;
108 /* callback on timer expiration */
109 static void timer_callback( void *private )
111 struct timer *timer = (struct timer *)private;
113 /* queue an APC */
114 if (timer->thread)
116 apc_call_t data;
118 memset( &data, 0, sizeof(data) );
119 if (timer->callback)
121 data.type = APC_TIMER;
122 data.timer.func = timer->callback;
123 data.timer.time = timer->when;
124 data.timer.arg = timer->arg;
126 else data.type = APC_NONE; /* wake up only */
128 if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
130 release_object( timer->thread );
131 timer->thread = NULL;
135 if (timer->period) /* schedule the next expiration */
137 timer->when += (timeout_t)timer->period * 10000;
138 timer->timeout = add_timeout_user( 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) ? current_time - expire : 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 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
182 return signaled;
185 static void timer_dump( struct object *obj, int verbose )
187 struct timer *timer = (struct timer *)obj;
188 assert( obj->ops == &timer_ops );
189 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
190 timer->manual, get_timeout_str(timer->when), 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 directory *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 );