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
, unsigned int attr
,
76 if ((timer
= create_named_object( sync_namespace
, &timer_ops
, name
, len
, attr
)))
78 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
80 /* initialize it if it didn't already exist */
81 timer
->manual
= manual
;
83 timer
->when
.tv_sec
= 0;
84 timer
->when
.tv_usec
= 0;
86 timer
->timeout
= NULL
;
93 /* callback on timer expiration */
94 static void timer_callback( void *private )
96 struct timer
*timer
= (struct timer
*)private;
101 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0,
102 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
104 release_object( timer
->thread
);
105 timer
->thread
= NULL
;
109 if (timer
->period
) /* schedule the next expiration */
111 add_timeout( &timer
->when
, timer
->period
);
112 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
114 else timer
->timeout
= NULL
;
116 /* wake up waiters */
118 wake_up( &timer
->obj
, 0 );
121 /* cancel a running timer */
122 static int cancel_timer( struct timer
*timer
)
124 int signaled
= timer
->signaled
;
128 remove_timeout_user( timer
->timeout
);
129 timer
->timeout
= NULL
;
133 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
134 release_object( timer
->thread
);
135 timer
->thread
= NULL
;
140 /* set the timer expiration and period */
141 static int set_timer( struct timer
*timer
, const abs_time_t
*expire
, int period
,
142 void *callback
, void *arg
)
144 int signaled
= cancel_timer( timer
);
147 period
= 0; /* period doesn't make any sense for a manual timer */
150 if (!expire
->sec
&& !expire
->usec
)
152 /* special case: use now + period as first expiration */
153 gettimeofday( &timer
->when
, NULL
);
154 add_timeout( &timer
->when
, period
);
158 timer
->when
.tv_sec
= expire
->sec
;
159 timer
->when
.tv_usec
= expire
->usec
;
161 timer
->period
= period
;
162 timer
->callback
= callback
;
164 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
165 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
169 static void timer_dump( struct object
*obj
, int verbose
)
171 struct timer
*timer
= (struct timer
*)obj
;
172 assert( obj
->ops
== &timer_ops
);
173 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
174 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
175 dump_object_name( &timer
->obj
);
176 fputc( '\n', stderr
);
179 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
181 struct timer
*timer
= (struct timer
*)obj
;
182 assert( obj
->ops
== &timer_ops
);
183 return timer
->signaled
;
186 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
188 struct timer
*timer
= (struct timer
*)obj
;
189 assert( obj
->ops
== &timer_ops
);
190 if (!timer
->manual
) timer
->signaled
= 0;
194 static void timer_destroy( struct object
*obj
)
196 struct timer
*timer
= (struct timer
*)obj
;
197 assert( obj
->ops
== &timer_ops
);
199 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
200 if (timer
->thread
) release_object( timer
->thread
);
204 DECL_HANDLER(create_timer
)
209 if ((timer
= create_timer( get_req_data(), get_req_data_size(), req
->attributes
, req
->manual
)))
211 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
,
212 req
->attributes
& OBJ_INHERIT
);
213 release_object( timer
);
217 /* open a handle to a timer */
218 DECL_HANDLER(open_timer
)
220 reply
->handle
= open_object( sync_namespace
, get_req_data(), get_req_data_size(),
221 &timer_ops
, req
->access
, req
->attributes
);
224 /* set a waitable timer */
225 DECL_HANDLER(set_timer
)
229 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
230 TIMER_MODIFY_STATE
, &timer_ops
)))
232 reply
->signaled
= set_timer( timer
, &req
->expire
, req
->period
, req
->callback
, req
->arg
);
233 release_object( timer
);
237 /* cancel a waitable timer */
238 DECL_HANDLER(cancel_timer
)
242 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
243 TIMER_MODIFY_STATE
, &timer_ops
)))
245 reply
->signaled
= cancel_timer( timer
);
246 release_object( timer
);
250 /* Get information on a waitable timer */
251 DECL_HANDLER(get_timer_info
)
255 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
256 TIMER_QUERY_STATE
, &timer_ops
)))
258 reply
->when
.sec
= timer
->when
.tv_sec
;
259 reply
->when
.usec
= timer
->when
.tv_usec
;
260 reply
->signaled
= timer
->signaled
;
261 release_object( timer
);