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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include <sys/types.h>
40 struct object obj
; /* object header */
41 int manual
; /* manual reset */
42 int signaled
; /* current signaled state */
43 int period
; /* timer period in ms */
44 struct timeval when
; /* next expiration */
45 struct timeout_user
*timeout
; /* timeout user */
46 struct thread
*thread
; /* thread that set the APC function */
47 void *callback
; /* callback APC function */
48 void *arg
; /* callback argument */
51 static void timer_dump( struct object
*obj
, int verbose
);
52 static int timer_signaled( struct object
*obj
, struct thread
*thread
);
53 static int timer_satisfied( struct object
*obj
, struct thread
*thread
);
54 static void timer_destroy( struct object
*obj
);
56 static const struct object_ops timer_ops
=
58 sizeof(struct timer
), /* size */
59 timer_dump
, /* dump */
60 add_queue
, /* add_queue */
61 remove_queue
, /* remove_queue */
62 timer_signaled
, /* signaled */
63 timer_satisfied
, /* satisfied */
64 no_signal
, /* signal */
65 no_get_fd
, /* get_fd */
66 no_lookup_name
, /* lookup_name */
67 no_close_handle
, /* close_handle */
68 timer_destroy
/* destroy */
72 /* create a timer object */
73 static struct timer
*create_timer( const struct unicode_str
*name
, unsigned int attr
,
78 if ((timer
= create_named_object( sync_namespace
, &timer_ops
, name
, attr
)))
80 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
82 /* initialize it if it didn't already exist */
83 timer
->manual
= manual
;
85 timer
->when
.tv_sec
= 0;
86 timer
->when
.tv_usec
= 0;
88 timer
->timeout
= NULL
;
95 /* callback on timer expiration */
96 static void timer_callback( void *private )
98 struct timer
*timer
= (struct timer
*)private;
103 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0,
104 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
106 release_object( timer
->thread
);
107 timer
->thread
= NULL
;
111 if (timer
->period
) /* schedule the next expiration */
113 add_timeout( &timer
->when
, timer
->period
);
114 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
116 else timer
->timeout
= NULL
;
118 /* wake up waiters */
120 wake_up( &timer
->obj
, 0 );
123 /* cancel a running timer */
124 static int cancel_timer( struct timer
*timer
)
126 int signaled
= timer
->signaled
;
130 remove_timeout_user( timer
->timeout
);
131 timer
->timeout
= NULL
;
135 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
136 release_object( timer
->thread
);
137 timer
->thread
= NULL
;
142 /* set the timer expiration and period */
143 static int set_timer( struct timer
*timer
, const abs_time_t
*expire
, int period
,
144 void *callback
, void *arg
)
146 int signaled
= cancel_timer( timer
);
149 period
= 0; /* period doesn't make any sense for a manual timer */
152 if (!expire
->sec
&& !expire
->usec
)
154 /* special case: use now + period as first expiration */
155 gettimeofday( &timer
->when
, NULL
);
156 add_timeout( &timer
->when
, period
);
160 timer
->when
.tv_sec
= expire
->sec
;
161 timer
->when
.tv_usec
= expire
->usec
;
163 timer
->period
= period
;
164 timer
->callback
= callback
;
166 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
167 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
171 static void timer_dump( struct object
*obj
, int verbose
)
173 struct timer
*timer
= (struct timer
*)obj
;
174 assert( obj
->ops
== &timer_ops
);
175 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
176 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
177 dump_object_name( &timer
->obj
);
178 fputc( '\n', stderr
);
181 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
183 struct timer
*timer
= (struct timer
*)obj
;
184 assert( obj
->ops
== &timer_ops
);
185 return timer
->signaled
;
188 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
190 struct timer
*timer
= (struct timer
*)obj
;
191 assert( obj
->ops
== &timer_ops
);
192 if (!timer
->manual
) timer
->signaled
= 0;
196 static void timer_destroy( struct object
*obj
)
198 struct timer
*timer
= (struct timer
*)obj
;
199 assert( obj
->ops
== &timer_ops
);
201 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
202 if (timer
->thread
) release_object( timer
->thread
);
206 DECL_HANDLER(create_timer
)
209 struct unicode_str name
;
212 get_req_unicode_str( &name
);
213 if ((timer
= create_timer( &name
, req
->attributes
, req
->manual
)))
215 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
,
216 req
->attributes
& OBJ_INHERIT
);
217 release_object( timer
);
221 /* open a handle to a timer */
222 DECL_HANDLER(open_timer
)
224 struct unicode_str name
;
226 get_req_unicode_str( &name
);
227 reply
->handle
= open_object( sync_namespace
, &name
, &timer_ops
, req
->access
, req
->attributes
);
230 /* set a waitable timer */
231 DECL_HANDLER(set_timer
)
235 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
236 TIMER_MODIFY_STATE
, &timer_ops
)))
238 reply
->signaled
= set_timer( timer
, &req
->expire
, req
->period
, req
->callback
, req
->arg
);
239 release_object( timer
);
243 /* cancel a waitable timer */
244 DECL_HANDLER(cancel_timer
)
248 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
249 TIMER_MODIFY_STATE
, &timer_ops
)))
251 reply
->signaled
= cancel_timer( timer
);
252 release_object( timer
);
256 /* Get information on a waitable timer */
257 DECL_HANDLER(get_timer_info
)
261 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
262 TIMER_QUERY_STATE
, &timer_ops
)))
264 reply
->when
.sec
= timer
->when
.tv_sec
;
265 reply
->when
.usec
= timer
->when
.tv_usec
;
266 reply
->signaled
= timer
->signaled
;
267 release_object( timer
);