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>
39 struct object obj
; /* object header */
40 int manual
; /* manual reset */
41 int signaled
; /* current signaled state */
42 int period
; /* timer period in ms */
43 struct timeval when
; /* next expiration */
44 struct timeout_user
*timeout
; /* timeout user */
45 struct thread
*thread
; /* thread that set the APC function */
46 void *callback
; /* callback APC function */
47 void *arg
; /* callback argument */
50 static void timer_dump( struct object
*obj
, int verbose
);
51 static int timer_signaled( struct object
*obj
, struct thread
*thread
);
52 static int timer_satisfied( struct object
*obj
, struct thread
*thread
);
53 static void timer_destroy( struct object
*obj
);
55 static const struct object_ops timer_ops
=
57 sizeof(struct timer
), /* size */
58 timer_dump
, /* dump */
59 add_queue
, /* add_queue */
60 remove_queue
, /* remove_queue */
61 timer_signaled
, /* signaled */
62 timer_satisfied
, /* satisfied */
63 no_signal
, /* signal */
64 no_get_fd
, /* get_fd */
65 no_close_handle
, /* close_handle */
66 timer_destroy
/* destroy */
70 /* create a timer object */
71 static struct timer
*create_timer( const WCHAR
*name
, size_t len
, int manual
)
75 if ((timer
= create_named_object( sync_namespace
, &timer_ops
, name
, len
)))
77 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
79 /* initialize it if it didn't already exist */
80 timer
->manual
= manual
;
82 timer
->when
.tv_sec
= 0;
83 timer
->when
.tv_usec
= 0;
85 timer
->timeout
= NULL
;
92 /* callback on timer expiration */
93 static void timer_callback( void *private )
95 struct timer
*timer
= (struct timer
*)private;
100 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0,
101 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
103 release_object( timer
->thread
);
104 timer
->thread
= NULL
;
108 if (timer
->period
) /* schedule the next expiration */
110 add_timeout( &timer
->when
, timer
->period
);
111 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
113 else timer
->timeout
= NULL
;
115 /* wake up waiters */
117 wake_up( &timer
->obj
, 0 );
120 /* cancel a running timer */
121 static int cancel_timer( struct timer
*timer
)
123 int signaled
= timer
->signaled
;
127 remove_timeout_user( timer
->timeout
);
128 timer
->timeout
= NULL
;
132 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
133 release_object( timer
->thread
);
134 timer
->thread
= NULL
;
139 /* set the timer expiration and period */
140 static int set_timer( struct timer
*timer
, const abs_time_t
*expire
, int period
,
141 void *callback
, void *arg
)
143 int signaled
= cancel_timer( timer
);
146 period
= 0; /* period doesn't make any sense for a manual timer */
149 if (!expire
->sec
&& !expire
->usec
)
151 /* special case: use now + period as first expiration */
152 gettimeofday( &timer
->when
, NULL
);
153 add_timeout( &timer
->when
, period
);
157 timer
->when
.tv_sec
= expire
->sec
;
158 timer
->when
.tv_usec
= expire
->usec
;
160 timer
->period
= period
;
161 timer
->callback
= callback
;
163 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
164 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
168 static void timer_dump( struct object
*obj
, int verbose
)
170 struct timer
*timer
= (struct timer
*)obj
;
171 assert( obj
->ops
== &timer_ops
);
172 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
173 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
174 dump_object_name( &timer
->obj
);
175 fputc( '\n', stderr
);
178 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
180 struct timer
*timer
= (struct timer
*)obj
;
181 assert( obj
->ops
== &timer_ops
);
182 return timer
->signaled
;
185 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
187 struct timer
*timer
= (struct timer
*)obj
;
188 assert( obj
->ops
== &timer_ops
);
189 if (!timer
->manual
) timer
->signaled
= 0;
193 static void timer_destroy( struct object
*obj
)
195 struct timer
*timer
= (struct timer
*)obj
;
196 assert( obj
->ops
== &timer_ops
);
198 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
199 if (timer
->thread
) release_object( timer
->thread
);
203 DECL_HANDLER(create_timer
)
208 if ((timer
= create_timer( get_req_data(), get_req_data_size(), req
->manual
)))
210 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
,
211 req
->attributes
& OBJ_INHERIT
);
212 release_object( timer
);
216 /* open a handle to a timer */
217 DECL_HANDLER(open_timer
)
219 reply
->handle
= open_object( sync_namespace
, get_req_data(), get_req_data_size(),
220 &timer_ops
, req
->access
, req
->attributes
);
223 /* set a waitable timer */
224 DECL_HANDLER(set_timer
)
228 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
229 TIMER_MODIFY_STATE
, &timer_ops
)))
231 reply
->signaled
= set_timer( timer
, &req
->expire
, req
->period
, req
->callback
, req
->arg
);
232 release_object( timer
);
236 /* cancel a waitable timer */
237 DECL_HANDLER(cancel_timer
)
241 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
242 TIMER_MODIFY_STATE
, &timer_ops
)))
244 reply
->signaled
= cancel_timer( timer
);
245 release_object( timer
);
249 /* Get information on a waitable timer */
250 DECL_HANDLER(get_timer_info
)
254 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
255 TIMER_QUERY_STATE
, &timer_ops
)))
257 reply
->when
.sec
= timer
->when
.tv_sec
;
258 reply
->when
.usec
= timer
->when
.tv_usec
;
259 reply
->signaled
= timer
->signaled
;
260 release_object( timer
);