msvcrt: Fix write() behaviour on "no console" file descriptors.
[wine.git] / server / timer.c
blob3a786fb4fca1f9b154d7a2b0df3faed42c65af7a
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 object *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( root, &timer_ops, name, attr, sd )))
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;
102 return timer;
105 /* callback on timer expiration */
106 static void timer_callback( void *private )
108 struct timer *timer = (struct timer *)private;
110 /* queue an APC */
111 if (timer->thread)
113 apc_call_t data;
115 memset( &data, 0, sizeof(data) );
116 if (timer->callback)
118 data.type = APC_TIMER;
119 data.timer.func = timer->callback;
120 data.timer.time = timer->when;
121 data.timer.arg = timer->arg;
123 else data.type = APC_NONE; /* wake up only */
125 if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
127 release_object( timer->thread );
128 timer->thread = NULL;
132 if (timer->period) /* schedule the next expiration */
134 timer->when += (timeout_t)timer->period * 10000;
135 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
137 else timer->timeout = NULL;
139 /* wake up waiters */
140 timer->signaled = 1;
141 wake_up( &timer->obj, 0 );
144 /* cancel a running timer */
145 static int cancel_timer( struct timer *timer )
147 int signaled = timer->signaled;
149 if (timer->timeout)
151 remove_timeout_user( timer->timeout );
152 timer->timeout = NULL;
154 if (timer->thread)
156 thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
157 release_object( timer->thread );
158 timer->thread = NULL;
160 return signaled;
163 /* set the timer expiration and period */
164 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
165 client_ptr_t callback, client_ptr_t arg )
167 int signaled = cancel_timer( timer );
168 if (timer->manual)
170 period = 0; /* period doesn't make any sense for a manual timer */
171 timer->signaled = 0;
173 timer->when = (expire <= 0) ? current_time - expire : max( expire, current_time );
174 timer->period = period;
175 timer->callback = callback;
176 timer->arg = arg;
177 if (callback) timer->thread = (struct thread *)grab_object( current );
178 timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
179 return signaled;
182 static void timer_dump( struct object *obj, int verbose )
184 struct timer *timer = (struct timer *)obj;
185 assert( obj->ops == &timer_ops );
186 fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
187 timer->manual, get_timeout_str(timer->when), timer->period );
190 static struct object_type *timer_get_type( struct object *obj )
192 static const WCHAR name[] = {'T','i','m','e','r'};
193 static const struct unicode_str str = { name, sizeof(name) };
194 return get_object_type( &str );
197 static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
199 struct timer *timer = (struct timer *)obj;
200 assert( obj->ops == &timer_ops );
201 return timer->signaled;
204 static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
206 struct timer *timer = (struct timer *)obj;
207 assert( obj->ops == &timer_ops );
208 if (!timer->manual) timer->signaled = 0;
211 static unsigned int timer_map_access( struct object *obj, unsigned int access )
213 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
214 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
215 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
216 if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
217 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
220 static void timer_destroy( struct object *obj )
222 struct timer *timer = (struct timer *)obj;
223 assert( obj->ops == &timer_ops );
225 if (timer->timeout) remove_timeout_user( timer->timeout );
226 if (timer->thread) release_object( timer->thread );
229 /* create a timer */
230 DECL_HANDLER(create_timer)
232 struct timer *timer;
233 struct unicode_str name;
234 struct object *root;
235 const struct security_descriptor *sd;
236 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
238 if (!objattr) return;
240 if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
242 reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
243 release_object( timer );
246 if (root) release_object( root );
249 /* open a handle to a timer */
250 DECL_HANDLER(open_timer)
252 struct unicode_str name = get_req_unicode_str();
254 reply->handle = open_object( current->process, req->rootdir, req->access,
255 &timer_ops, &name, req->attributes );
258 /* set a waitable timer */
259 DECL_HANDLER(set_timer)
261 struct timer *timer;
263 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
264 TIMER_MODIFY_STATE, &timer_ops )))
266 reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
267 release_object( timer );
271 /* cancel a waitable timer */
272 DECL_HANDLER(cancel_timer)
274 struct timer *timer;
276 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
277 TIMER_MODIFY_STATE, &timer_ops )))
279 reply->signaled = cancel_timer( timer );
280 release_object( timer );
284 /* Get information on a waitable timer */
285 DECL_HANDLER(get_timer_info)
287 struct timer *timer;
289 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
290 TIMER_QUERY_STATE, &timer_ops )))
292 reply->when = timer->when;
293 reply->signaled = timer->signaled;
294 release_object( timer );