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 |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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> |
| 29 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 30 | #include "winnt.h" |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 31 | #include "handle.h" |
| 32 | #include "request.h" |
| 33 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 34 | struct timer |
| 35 | { |
| 36 | struct object obj; /* object header */ |
| 37 | int manual; /* manual reset */ |
| 38 | int signaled; /* current signaled state */ |
| 39 | int period; /* timer period in ms */ |
| 40 | struct timeval when; /* next expiration */ |
| 41 | struct timeout_user *timeout; /* timeout user */ |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 42 | struct thread *thread; /* thread that set the APC function */ |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 43 | void *callback; /* callback APC function */ |
| 44 | void *arg; /* callback argument */ |
| 45 | }; |
| 46 | |
| 47 | static void timer_dump( struct object *obj, int verbose ); |
| 48 | static int timer_signaled( struct object *obj, struct thread *thread ); |
| 49 | static int timer_satisfied( struct object *obj, struct thread *thread ); |
| 50 | static void timer_destroy( struct object *obj ); |
| 51 | |
| 52 | static const struct object_ops timer_ops = |
| 53 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 54 | sizeof(struct timer), /* size */ |
| 55 | timer_dump, /* dump */ |
| 56 | add_queue, /* add_queue */ |
| 57 | remove_queue, /* remove_queue */ |
| 58 | timer_signaled, /* signaled */ |
| 59 | timer_satisfied, /* satisfied */ |
| 60 | NULL, /* get_poll_events */ |
| 61 | NULL, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 62 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 63 | no_flush, /* flush */ |
| 64 | no_get_file_info, /* get_file_info */ |
Mike McCormack | 6f011c0 | 2001-12-20 00:07:05 +0000 | [diff] [blame] | 65 | NULL, /* queue_async */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 66 | timer_destroy /* destroy */ |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | |
| 70 | /* create a timer object */ |
| 71 | static struct timer *create_timer( const WCHAR *name, size_t len, int manual ) |
| 72 | { |
| 73 | struct timer *timer; |
| 74 | |
| 75 | if ((timer = create_named_object( &timer_ops, name, len ))) |
| 76 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 77 | if (get_error() != STATUS_OBJECT_NAME_COLLISION) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 78 | { |
| 79 | /* initialize it if it didn't already exist */ |
| 80 | timer->manual = manual; |
| 81 | timer->signaled = 0; |
| 82 | timer->when.tv_sec = 0; |
| 83 | timer->when.tv_usec = 0; |
| 84 | timer->period = 0; |
| 85 | timer->timeout = NULL; |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 86 | timer->thread = NULL; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | return timer; |
| 90 | } |
| 91 | |
| 92 | /* callback on timer expiration */ |
| 93 | static void timer_callback( void *private ) |
| 94 | { |
| 95 | struct timer *timer = (struct timer *)private; |
| 96 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 97 | /* queue an APC */ |
| 98 | if (timer->thread) |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 99 | { |
| 100 | if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0, 3, |
| 101 | (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg)) |
| 102 | { |
| 103 | release_object( timer->thread ); |
| 104 | timer->thread = NULL; |
| 105 | } |
| 106 | } |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 107 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 108 | if (timer->period) /* schedule the next expiration */ |
| 109 | { |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 110 | add_timeout( &timer->when, timer->period ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 111 | timer->timeout = add_timeout_user( &timer->when, timer_callback, timer ); |
| 112 | } |
| 113 | else timer->timeout = NULL; |
| 114 | |
| 115 | /* wake up waiters */ |
| 116 | timer->signaled = 1; |
| 117 | wake_up( &timer->obj, 0 ); |
| 118 | } |
| 119 | |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 120 | /* cancel a running timer */ |
| 121 | static void cancel_timer( struct timer *timer ) |
| 122 | { |
| 123 | if (timer->timeout) |
| 124 | { |
| 125 | remove_timeout_user( timer->timeout ); |
| 126 | timer->timeout = NULL; |
| 127 | } |
| 128 | if (timer->thread) |
| 129 | { |
Alexandre Julliard | 2362380 | 2001-01-06 01:48:51 +0000 | [diff] [blame] | 130 | thread_cancel_apc( timer->thread, &timer->obj, 0 ); |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 131 | release_object( timer->thread ); |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 132 | timer->thread = NULL; |
| 133 | } |
| 134 | } |
| 135 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 136 | /* set the timer expiration and period */ |
| 137 | static void set_timer( struct timer *timer, int sec, int usec, int period, |
| 138 | void *callback, void *arg ) |
| 139 | { |
Alexandre Julliard | ea1afce | 2000-08-22 20:08:37 +0000 | [diff] [blame] | 140 | cancel_timer( timer ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 141 | if (timer->manual) |
| 142 | { |
| 143 | period = 0; /* period doesn't make any sense for a manual timer */ |
| 144 | timer->signaled = 0; |
| 145 | } |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 146 | if (!sec && !usec) |
| 147 | { |
| 148 | /* special case: use now + period as first expiration */ |
| 149 | gettimeofday( &timer->when, 0 ); |
| 150 | add_timeout( &timer->when, period ); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | timer->when.tv_sec = sec; |
| 155 | timer->when.tv_usec = usec; |
| 156 | } |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 157 | timer->period = period; |
| 158 | timer->callback = callback; |
| 159 | timer->arg = arg; |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 160 | if (callback) timer->thread = (struct thread *)grab_object( current ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 161 | timer->timeout = add_timeout_user( &timer->when, timer_callback, timer ); |
| 162 | } |
| 163 | |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 164 | static void timer_dump( struct object *obj, int verbose ) |
| 165 | { |
| 166 | struct timer *timer = (struct timer *)obj; |
| 167 | assert( obj->ops == &timer_ops ); |
| 168 | fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ", |
| 169 | timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period ); |
| 170 | dump_object_name( &timer->obj ); |
| 171 | fputc( '\n', stderr ); |
| 172 | } |
| 173 | |
| 174 | static int timer_signaled( struct object *obj, struct thread *thread ) |
| 175 | { |
| 176 | struct timer *timer = (struct timer *)obj; |
| 177 | assert( obj->ops == &timer_ops ); |
| 178 | return timer->signaled; |
| 179 | } |
| 180 | |
| 181 | static int timer_satisfied( struct object *obj, struct thread *thread ) |
| 182 | { |
| 183 | struct timer *timer = (struct timer *)obj; |
| 184 | assert( obj->ops == &timer_ops ); |
| 185 | if (!timer->manual) timer->signaled = 0; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static void timer_destroy( struct object *obj ) |
| 190 | { |
| 191 | struct timer *timer = (struct timer *)obj; |
| 192 | assert( obj->ops == &timer_ops ); |
| 193 | |
| 194 | if (timer->timeout) remove_timeout_user( timer->timeout ); |
Alexandre Julliard | 15979fd | 2002-03-23 18:50:04 +0000 | [diff] [blame] | 195 | if (timer->thread) release_object( timer->thread ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* create a timer */ |
| 199 | DECL_HANDLER(create_timer) |
| 200 | { |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 201 | struct timer *timer; |
| 202 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 203 | reply->handle = 0; |
| 204 | if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual ))) |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 205 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 206 | reply->handle = alloc_handle( current->process, timer, TIMER_ALL_ACCESS, req->inherit ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 207 | release_object( timer ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* open a handle to a timer */ |
| 212 | DECL_HANDLER(open_timer) |
| 213 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 214 | reply->handle = open_object( get_req_data(), get_req_data_size(), |
| 215 | &timer_ops, req->access, req->inherit ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* set a waitable timer */ |
| 219 | DECL_HANDLER(set_timer) |
| 220 | { |
| 221 | struct timer *timer; |
| 222 | |
| 223 | if ((timer = (struct timer *)get_handle_obj( current->process, req->handle, |
| 224 | TIMER_MODIFY_STATE, &timer_ops ))) |
| 225 | { |
| 226 | set_timer( timer, req->sec, req->usec, req->period, req->callback, req->arg ); |
| 227 | release_object( timer ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* cancel a waitable timer */ |
| 232 | DECL_HANDLER(cancel_timer) |
| 233 | { |
| 234 | struct timer *timer; |
| 235 | |
| 236 | if ((timer = (struct timer *)get_handle_obj( current->process, req->handle, |
| 237 | TIMER_MODIFY_STATE, &timer_ops ))) |
| 238 | { |
| 239 | cancel_timer( timer ); |
| 240 | release_object( timer ); |
| 241 | } |
| 242 | } |