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>
32 #define WIN32_NO_STATUS
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 */
48 struct thread
*thread
; /* thread that set the APC function */
49 void *callback
; /* callback APC function */
50 void *arg
; /* callback argument */
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
);
56 static void timer_destroy( struct object
*obj
);
58 static const struct object_ops timer_ops
=
60 sizeof(struct timer
), /* size */
61 timer_dump
, /* dump */
62 add_queue
, /* add_queue */
63 remove_queue
, /* remove_queue */
64 timer_signaled
, /* signaled */
65 timer_satisfied
, /* satisfied */
66 no_signal
, /* signal */
67 no_get_fd
, /* get_fd */
68 no_lookup_name
, /* lookup_name */
69 no_close_handle
, /* close_handle */
70 timer_destroy
/* destroy */
74 /* create a timer object */
75 static struct timer
*create_timer( struct directory
*root
, const struct unicode_str
*name
,
76 unsigned int attr
, int manual
)
80 if ((timer
= create_named_object_dir( root
, name
, attr
, &timer_ops
)))
82 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
84 /* initialize it if it didn't already exist */
85 timer
->manual
= manual
;
87 timer
->when
.tv_sec
= 0;
88 timer
->when
.tv_usec
= 0;
90 timer
->timeout
= NULL
;
97 /* callback on timer expiration */
98 static void timer_callback( void *private )
100 struct timer
*timer
= (struct timer
*)private;
105 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0,
106 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
108 release_object( timer
->thread
);
109 timer
->thread
= NULL
;
113 if (timer
->period
) /* schedule the next expiration */
115 add_timeout( &timer
->when
, timer
->period
);
116 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
118 else timer
->timeout
= NULL
;
120 /* wake up waiters */
122 wake_up( &timer
->obj
, 0 );
125 /* cancel a running timer */
126 static int cancel_timer( struct timer
*timer
)
128 int signaled
= timer
->signaled
;
132 remove_timeout_user( timer
->timeout
);
133 timer
->timeout
= NULL
;
137 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
138 release_object( timer
->thread
);
139 timer
->thread
= NULL
;
144 /* set the timer expiration and period */
145 static int set_timer( struct timer
*timer
, const abs_time_t
*expire
, int period
,
146 void *callback
, void *arg
)
148 int signaled
= cancel_timer( timer
);
151 period
= 0; /* period doesn't make any sense for a manual timer */
154 if (!expire
->sec
&& !expire
->usec
)
156 /* special case: use now + period as first expiration */
157 gettimeofday( &timer
->when
, NULL
);
158 add_timeout( &timer
->when
, period
);
162 timer
->when
.tv_sec
= expire
->sec
;
163 timer
->when
.tv_usec
= expire
->usec
;
165 timer
->period
= period
;
166 timer
->callback
= callback
;
168 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
169 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
173 static void timer_dump( struct object
*obj
, int verbose
)
175 struct timer
*timer
= (struct timer
*)obj
;
176 assert( obj
->ops
== &timer_ops
);
177 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
178 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
179 dump_object_name( &timer
->obj
);
180 fputc( '\n', stderr
);
183 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
185 struct timer
*timer
= (struct timer
*)obj
;
186 assert( obj
->ops
== &timer_ops
);
187 return timer
->signaled
;
190 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
192 struct timer
*timer
= (struct timer
*)obj
;
193 assert( obj
->ops
== &timer_ops
);
194 if (!timer
->manual
) timer
->signaled
= 0;
198 static void timer_destroy( struct object
*obj
)
200 struct timer
*timer
= (struct timer
*)obj
;
201 assert( obj
->ops
== &timer_ops
);
203 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
204 if (timer
->thread
) release_object( timer
->thread
);
208 DECL_HANDLER(create_timer
)
211 struct unicode_str name
;
212 struct directory
*root
= NULL
;
215 get_req_unicode_str( &name
);
216 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
219 if ((timer
= create_timer( root
, &name
, req
->attributes
, req
->manual
)))
221 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
,
222 req
->attributes
& OBJ_INHERIT
);
223 release_object( timer
);
226 if (root
) release_object( root
);
229 /* open a handle to a timer */
230 DECL_HANDLER(open_timer
)
232 struct unicode_str name
;
233 struct directory
*root
= NULL
;
235 get_req_unicode_str( &name
);
236 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
239 reply
->handle
= open_object_dir( root
, &name
, req
->attributes
, &timer_ops
, req
->access
);
241 if (root
) release_object( root
);
244 /* set a waitable timer */
245 DECL_HANDLER(set_timer
)
249 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
250 TIMER_MODIFY_STATE
, &timer_ops
)))
252 reply
->signaled
= set_timer( timer
, &req
->expire
, req
->period
, req
->callback
, req
->arg
);
253 release_object( timer
);
257 /* cancel a waitable timer */
258 DECL_HANDLER(cancel_timer
)
262 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
263 TIMER_MODIFY_STATE
, &timer_ops
)))
265 reply
->signaled
= cancel_timer( timer
);
266 release_object( timer
);
270 /* Get information on a waitable timer */
271 DECL_HANDLER(get_timer_info
)
275 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
276 TIMER_QUERY_STATE
, &timer_ops
)))
278 reply
->when
.sec
= timer
->when
.tv_sec
;
279 reply
->when
.usec
= timer
->when
.tv_usec
;
280 reply
->signaled
= timer
->signaled
;
281 release_object( timer
);