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