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>
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 */
44 struct thread
*thread
; /* thread that set the APC function */
45 void *callback
; /* callback APC function */
46 void *arg
; /* callback argument */
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
);
54 static const struct object_ops timer_ops
=
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 */
62 no_signal
, /* signal */
63 no_get_fd
, /* get_fd */
64 no_close_handle
, /* close_handle */
65 timer_destroy
/* destroy */
69 /* create a timer object */
70 static struct timer
*create_timer( const WCHAR
*name
, size_t len
, int manual
)
74 if ((timer
= create_named_object( sync_namespace
, &timer_ops
, name
, len
)))
76 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
78 /* initialize it if it didn't already exist */
79 timer
->manual
= manual
;
81 timer
->when
.tv_sec
= 0;
82 timer
->when
.tv_usec
= 0;
84 timer
->timeout
= NULL
;
91 /* callback on timer expiration */
92 static void timer_callback( void *private )
94 struct timer
*timer
= (struct timer
*)private;
99 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0,
100 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
102 release_object( timer
->thread
);
103 timer
->thread
= NULL
;
107 if (timer
->period
) /* schedule the next expiration */
109 add_timeout( &timer
->when
, timer
->period
);
110 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
112 else timer
->timeout
= NULL
;
114 /* wake up waiters */
116 wake_up( &timer
->obj
, 0 );
119 /* cancel a running timer */
120 static int cancel_timer( struct timer
*timer
)
122 int signaled
= timer
->signaled
;
126 remove_timeout_user( timer
->timeout
);
127 timer
->timeout
= NULL
;
131 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
132 release_object( timer
->thread
);
133 timer
->thread
= NULL
;
138 /* set the timer expiration and period */
139 static int set_timer( struct timer
*timer
, const abs_time_t
*expire
, int period
,
140 void *callback
, void *arg
)
142 int signaled
= cancel_timer( timer
);
145 period
= 0; /* period doesn't make any sense for a manual timer */
148 if (!expire
->sec
&& !expire
->usec
)
150 /* special case: use now + period as first expiration */
151 gettimeofday( &timer
->when
, NULL
);
152 add_timeout( &timer
->when
, period
);
156 timer
->when
.tv_sec
= expire
->sec
;
157 timer
->when
.tv_usec
= expire
->usec
;
159 timer
->period
= period
;
160 timer
->callback
= callback
;
162 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
163 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
167 static void timer_dump( struct object
*obj
, int verbose
)
169 struct timer
*timer
= (struct timer
*)obj
;
170 assert( obj
->ops
== &timer_ops
);
171 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
172 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
173 dump_object_name( &timer
->obj
);
174 fputc( '\n', stderr
);
177 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
179 struct timer
*timer
= (struct timer
*)obj
;
180 assert( obj
->ops
== &timer_ops
);
181 return timer
->signaled
;
184 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
186 struct timer
*timer
= (struct timer
*)obj
;
187 assert( obj
->ops
== &timer_ops
);
188 if (!timer
->manual
) timer
->signaled
= 0;
192 static void timer_destroy( struct object
*obj
)
194 struct timer
*timer
= (struct timer
*)obj
;
195 assert( obj
->ops
== &timer_ops
);
197 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
198 if (timer
->thread
) release_object( timer
->thread
);
202 DECL_HANDLER(create_timer
)
207 if ((timer
= create_timer( get_req_data(), get_req_data_size(), req
->manual
)))
209 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
, req
->inherit
);
210 release_object( timer
);
214 /* open a handle to a timer */
215 DECL_HANDLER(open_timer
)
217 reply
->handle
= open_object( sync_namespace
, get_req_data(), get_req_data_size(),
218 &timer_ops
, req
->access
, req
->inherit
);
221 /* set a waitable timer */
222 DECL_HANDLER(set_timer
)
226 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
227 TIMER_MODIFY_STATE
, &timer_ops
)))
229 reply
->signaled
= set_timer( timer
, &req
->expire
, req
->period
, req
->callback
, req
->arg
);
230 release_object( timer
);
234 /* cancel a waitable timer */
235 DECL_HANDLER(cancel_timer
)
239 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
240 TIMER_MODIFY_STATE
, &timer_ops
)))
242 reply
->signaled
= cancel_timer( timer
);
243 release_object( timer
);
247 /* Get information on a waitable timer */
248 DECL_HANDLER(get_timer_info
)
252 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
253 TIMER_QUERY_STATE
, &timer_ops
)))
255 reply
->when
.sec
= timer
->when
.tv_sec
;
256 reply
->when
.usec
= timer
->when
.tv_usec
;
257 reply
->signaled
= timer
->signaled
;
258 release_object( timer
);