Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Waitable timers management |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
Robert Lunnon | 95414ef | 2005-11-22 12:01:05 +0000 | [diff] [blame] | 29 | #include <stdarg.h> |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 30 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 31 | #include "ntstatus.h" |
| 32 | #define WIN32_NO_STATUS |
Alexandre Julliard | 435e2e6 | 2002-12-10 22:56:43 +0000 | [diff] [blame] | 33 | #include "windef.h" |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 34 | #include "winternl.h" |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 35 | |
| 36 | #include "file.h" |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 37 | #include "handle.h" |
| 38 | #include "request.h" |
| 39 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 40 | struct timer |
| 41 | { |
| 42 | struct object obj; /* object header */ |
| 43 | int manual; /* manual reset */ |
| 44 | int signaled; /* current signaled state */ |
| 45 | int period; /* timer period in ms */ |
| 46 | struct timeval when; /* next expiration */ |
| 47 | struct timeout_user *timeout; /* timeout user */ |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 48 | struct thread *thread; /* thread that set the APC function */ |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 49 | void *callback; /* callback APC function */ |
| 50 | void *arg; /* callback argument */ |
| 51 | }; |
| 52 | |
| 53 | static void timer_dump( struct object *obj, int verbose ); |
| 54 | static int timer_signaled( struct object *obj, struct thread *thread ); |
| 55 | static int timer_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 56 | static unsigned int timer_map_access( struct object *obj, unsigned int access ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 57 | static void timer_destroy( struct object *obj ); |
| 58 | |
| 59 | static const struct object_ops timer_ops = |
| 60 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 61 | sizeof(struct timer), /* size */ |
| 62 | timer_dump, /* dump */ |
| 63 | add_queue, /* add_queue */ |
| 64 | remove_queue, /* remove_queue */ |
| 65 | timer_signaled, /* signaled */ |
| 66 | timer_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 67 | no_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 68 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 69 | timer_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 70 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 71 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 72 | timer_destroy /* destroy */ |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | |
| 76 | /* create a timer object */ |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 77 | static struct timer *create_timer( struct directory *root, const struct unicode_str *name, |
| 78 | unsigned int attr, int manual ) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 79 | { |
| 80 | struct timer *timer; |
| 81 | |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 82 | if ((timer = create_named_object_dir( root, name, attr, &timer_ops ))) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 83 | { |
Vitaliy Margolen | 893987b | 2005-11-21 16:27:03 +0000 | [diff] [blame] | 84 | if (get_error() != STATUS_OBJECT_NAME_EXISTS) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 85 | { |
| 86 | /* initialize it if it didn't already exist */ |
| 87 | timer->manual = manual; |
| 88 | timer->signaled = 0; |
| 89 | timer->when.tv_sec = 0; |
| 90 | timer->when.tv_usec = 0; |
| 91 | timer->period = 0; |
| 92 | timer->timeout = NULL; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 93 | timer->thread = NULL; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | return timer; |
| 97 | } |
| 98 | |
| 99 | /* callback on timer expiration */ |
| 100 | static void timer_callback( void *private ) |
| 101 | { |
| 102 | struct timer *timer = (struct timer *)private; |
| 103 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 104 | /* queue an APC */ |
| 105 | if (timer->thread) |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 106 | { |
Alexandre Julliard | 088bcf9 | 2003-04-04 22:26:34 +0000 | [diff] [blame] | 107 | if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0, |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 108 | (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg)) |
| 109 | { |
| 110 | release_object( timer->thread ); |
| 111 | timer->thread = NULL; |
| 112 | } |
| 113 | } |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 114 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 115 | if (timer->period) /* schedule the next expiration */ |
| 116 | { |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 117 | add_timeout( &timer->when, timer->period ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 118 | timer->timeout = add_timeout_user( &timer->when, timer_callback, timer ); |
| 119 | } |
| 120 | else timer->timeout = NULL; |
| 121 | |
| 122 | /* wake up waiters */ |
| 123 | timer->signaled = 1; |
| 124 | wake_up( &timer->obj, 0 ); |
| 125 | } |
| 126 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 127 | /* cancel a running timer */ |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 128 | static int cancel_timer( struct timer *timer ) |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 129 | { |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 130 | int signaled = timer->signaled; |
| 131 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 132 | if (timer->timeout) |
| 133 | { |
| 134 | remove_timeout_user( timer->timeout ); |
| 135 | timer->timeout = NULL; |
| 136 | } |
| 137 | if (timer->thread) |
| 138 | { |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 139 | thread_cancel_apc( timer->thread, &timer->obj, 0 ); |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 140 | release_object( timer->thread ); |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 141 | timer->thread = NULL; |
| 142 | } |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 143 | return signaled; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 146 | /* set the timer expiration and period */ |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 147 | static int set_timer( struct timer *timer, const abs_time_t *expire, int period, |
| 148 | void *callback, void *arg ) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 149 | { |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 150 | int signaled = cancel_timer( timer ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 151 | if (timer->manual) |
| 152 | { |
| 153 | period = 0; /* period doesn't make any sense for a manual timer */ |
| 154 | timer->signaled = 0; |
| 155 | } |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 156 | if (!expire->sec && !expire->usec) |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 157 | { |
| 158 | /* special case: use now + period as first expiration */ |
Alexandre Julliard | 753c870 | 2006-08-10 16:42:09 +0200 | [diff] [blame] | 159 | timer->when = current_time; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 160 | add_timeout( &timer->when, period ); |
| 161 | } |
| 162 | else |
| 163 | { |
Alexandre Julliard | 462172a | 2003-04-02 22:48:59 +0000 | [diff] [blame] | 164 | timer->when.tv_sec = expire->sec; |
| 165 | timer->when.tv_usec = expire->usec; |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 166 | } |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 167 | timer->period = period; |
| 168 | timer->callback = callback; |
| 169 | timer->arg = arg; |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 170 | if (callback) timer->thread = (struct thread *)grab_object( current ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 171 | timer->timeout = add_timeout_user( &timer->when, timer_callback, timer ); |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 172 | return signaled; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 175 | static void timer_dump( struct object *obj, int verbose ) |
| 176 | { |
| 177 | struct timer *timer = (struct timer *)obj; |
| 178 | assert( obj->ops == &timer_ops ); |
Alexandre Julliard | 53496f8 | 2006-01-23 16:48:26 +0100 | [diff] [blame] | 179 | fprintf( stderr, "Timer manual=%d when=%ld.%06u period=%d ", |
| 180 | timer->manual, timer->when.tv_sec, (unsigned int)timer->when.tv_usec, timer->period ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 181 | dump_object_name( &timer->obj ); |
| 182 | fputc( '\n', stderr ); |
| 183 | } |
| 184 | |
| 185 | static int timer_signaled( struct object *obj, struct thread *thread ) |
| 186 | { |
| 187 | struct timer *timer = (struct timer *)obj; |
| 188 | assert( obj->ops == &timer_ops ); |
| 189 | return timer->signaled; |
| 190 | } |
| 191 | |
| 192 | static int timer_satisfied( struct object *obj, struct thread *thread ) |
| 193 | { |
| 194 | struct timer *timer = (struct timer *)obj; |
| 195 | assert( obj->ops == &timer_ops ); |
| 196 | if (!timer->manual) timer->signaled = 0; |
| 197 | return 0; |
| 198 | } |
| 199 | |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 200 | static unsigned int timer_map_access( struct object *obj, unsigned int access ) |
| 201 | { |
| 202 | if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE; |
| 203 | if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE; |
| 204 | if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE; |
| 205 | if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS; |
| 206 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 207 | } |
| 208 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 209 | static void timer_destroy( struct object *obj ) |
| 210 | { |
| 211 | struct timer *timer = (struct timer *)obj; |
| 212 | assert( obj->ops == &timer_ops ); |
| 213 | |
| 214 | if (timer->timeout) remove_timeout_user( timer->timeout ); |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 215 | if (timer->thread) release_object( timer->thread ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* create a timer */ |
| 219 | DECL_HANDLER(create_timer) |
| 220 | { |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 221 | struct timer *timer; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 222 | struct unicode_str name; |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 223 | struct directory *root = NULL; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 224 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 225 | reply->handle = 0; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 226 | get_req_unicode_str( &name ); |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 227 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 228 | return; |
| 229 | |
| 230 | if ((timer = create_timer( root, &name, req->attributes, req->manual ))) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 231 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 232 | reply->handle = alloc_handle( current->process, timer, req->access, req->attributes ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 233 | release_object( timer ); |
| 234 | } |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 235 | |
| 236 | if (root) release_object( root ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* open a handle to a timer */ |
| 240 | DECL_HANDLER(open_timer) |
| 241 | { |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 242 | struct unicode_str name; |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 243 | struct directory *root = NULL; |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 244 | struct timer *timer; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 245 | |
| 246 | get_req_unicode_str( &name ); |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 247 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 248 | return; |
| 249 | |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 250 | if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops ))) |
| 251 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 252 | reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes ); |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 253 | release_object( timer ); |
| 254 | } |
Vitaliy Margolen | 7c5cb7a | 2005-12-02 16:05:54 +0100 | [diff] [blame] | 255 | |
| 256 | if (root) release_object( root ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* set a waitable timer */ |
| 260 | DECL_HANDLER(set_timer) |
| 261 | { |
| 262 | struct timer *timer; |
| 263 | |
| 264 | if ((timer = (struct timer *)get_handle_obj( current->process, req->handle, |
| 265 | TIMER_MODIFY_STATE, &timer_ops ))) |
| 266 | { |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 267 | reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 268 | release_object( timer ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* cancel a waitable timer */ |
| 273 | DECL_HANDLER(cancel_timer) |
| 274 | { |
| 275 | struct timer *timer; |
| 276 | |
| 277 | if ((timer = (struct timer *)get_handle_obj( current->process, req->handle, |
| 278 | TIMER_MODIFY_STATE, &timer_ops ))) |
| 279 | { |
Eric Pouech | 4c591d4 | 2003-05-20 04:00:42 +0000 | [diff] [blame] | 280 | reply->signaled = cancel_timer( timer ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 281 | release_object( timer ); |
| 282 | } |
| 283 | } |
Robert Shearman | 7572b12 | 2004-12-13 21:10:58 +0000 | [diff] [blame] | 284 | |
| 285 | /* Get information on a waitable timer */ |
| 286 | DECL_HANDLER(get_timer_info) |
| 287 | { |
| 288 | struct timer *timer; |
| 289 | |
| 290 | if ((timer = (struct timer *)get_handle_obj( current->process, req->handle, |
| 291 | TIMER_QUERY_STATE, &timer_ops ))) |
| 292 | { |
| 293 | reply->when.sec = timer->when.tv_sec; |
| 294 | reply->when.usec = timer->when.tv_usec; |
| 295 | reply->signaled = timer->signaled; |
| 296 | release_object( timer ); |
| 297 | } |
| 298 | } |