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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 unsigned int period
; /* timer period in ms */
46 timeout_t 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 unsigned int timer_map_access( struct object
*obj
, unsigned int access
);
57 static void timer_destroy( struct object
*obj
);
59 static const struct object_ops timer_ops
=
61 sizeof(struct timer
), /* size */
62 timer_dump
, /* dump */
63 add_queue
, /* add_queue */
64 remove_queue
, /* remove_queue */
65 timer_signaled
, /* signaled */
66 timer_satisfied
, /* satisfied */
67 no_signal
, /* signal */
68 no_get_fd
, /* get_fd */
69 timer_map_access
, /* map_access */
70 no_lookup_name
, /* lookup_name */
71 no_open_file
, /* open_file */
72 no_close_handle
, /* close_handle */
73 timer_destroy
/* destroy */
77 /* create a timer object */
78 static struct timer
*create_timer( struct directory
*root
, const struct unicode_str
*name
,
79 unsigned int attr
, int manual
)
83 if ((timer
= create_named_object_dir( root
, name
, attr
, &timer_ops
)))
85 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
87 /* initialize it if it didn't already exist */
88 timer
->manual
= manual
;
92 timer
->timeout
= NULL
;
99 /* callback on timer expiration */
100 static void timer_callback( void *private )
102 struct timer
*timer
= (struct timer
*)private;
109 memset( &data
, 0, sizeof(data
) );
112 data
.type
= APC_TIMER
;
113 data
.timer
.func
= timer
->callback
;
114 data
.timer
.time
= timer
->when
;
115 data
.timer
.arg
= timer
->arg
;
117 else data
.type
= APC_NONE
; /* wake up only */
119 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, &data
))
121 release_object( timer
->thread
);
122 timer
->thread
= NULL
;
126 if (timer
->period
) /* schedule the next expiration */
128 timer
->when
+= (timeout_t
)timer
->period
* 10000;
129 timer
->timeout
= add_timeout_user( timer
->when
, timer_callback
, timer
);
131 else timer
->timeout
= NULL
;
133 /* wake up waiters */
135 wake_up( &timer
->obj
, 0 );
138 /* cancel a running timer */
139 static int cancel_timer( struct timer
*timer
)
141 int signaled
= timer
->signaled
;
145 remove_timeout_user( timer
->timeout
);
146 timer
->timeout
= NULL
;
150 thread_cancel_apc( timer
->thread
, &timer
->obj
, APC_TIMER
);
151 release_object( timer
->thread
);
152 timer
->thread
= NULL
;
157 /* set the timer expiration and period */
158 static int set_timer( struct timer
*timer
, timeout_t expire
, unsigned int period
,
159 void *callback
, void *arg
)
161 int signaled
= cancel_timer( timer
);
164 period
= 0; /* period doesn't make any sense for a manual timer */
167 timer
->when
= (expire
<= 0) ? current_time
- expire
: max( expire
, current_time
);
168 timer
->period
= period
;
169 timer
->callback
= callback
;
171 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
172 timer
->timeout
= add_timeout_user( timer
->when
, timer_callback
, timer
);
176 static void timer_dump( struct object
*obj
, int verbose
)
178 struct timer
*timer
= (struct timer
*)obj
;
179 assert( obj
->ops
== &timer_ops
);
180 fprintf( stderr
, "Timer manual=%d when=%s period=%u ",
181 timer
->manual
, get_timeout_str(timer
->when
), timer
->period
);
182 dump_object_name( &timer
->obj
);
183 fputc( '\n', stderr
);
186 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
188 struct timer
*timer
= (struct timer
*)obj
;
189 assert( obj
->ops
== &timer_ops
);
190 return timer
->signaled
;
193 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
195 struct timer
*timer
= (struct timer
*)obj
;
196 assert( obj
->ops
== &timer_ops
);
197 if (!timer
->manual
) timer
->signaled
= 0;
201 static unsigned int timer_map_access( struct object
*obj
, unsigned int access
)
203 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
| TIMER_QUERY_STATE
;
204 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| TIMER_MODIFY_STATE
;
205 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
206 if (access
& GENERIC_ALL
) access
|= TIMER_ALL_ACCESS
;
207 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
210 static void timer_destroy( struct object
*obj
)
212 struct timer
*timer
= (struct timer
*)obj
;
213 assert( obj
->ops
== &timer_ops
);
215 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
216 if (timer
->thread
) release_object( timer
->thread
);
220 DECL_HANDLER(create_timer
)
223 struct unicode_str name
;
224 struct directory
*root
= NULL
;
227 get_req_unicode_str( &name
);
228 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
231 if ((timer
= create_timer( root
, &name
, req
->attributes
, req
->manual
)))
233 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
, req
->attributes
);
234 release_object( timer
);
237 if (root
) release_object( root
);
240 /* open a handle to a timer */
241 DECL_HANDLER(open_timer
)
243 struct unicode_str name
;
244 struct directory
*root
= NULL
;
247 get_req_unicode_str( &name
);
248 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
251 if ((timer
= open_object_dir( root
, &name
, req
->attributes
, &timer_ops
)))
253 reply
->handle
= alloc_handle( current
->process
, &timer
->obj
, req
->access
, req
->attributes
);
254 release_object( timer
);
257 if (root
) release_object( root
);
260 /* set a waitable timer */
261 DECL_HANDLER(set_timer
)
265 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
266 TIMER_MODIFY_STATE
, &timer_ops
)))
268 reply
->signaled
= set_timer( timer
, req
->expire
, req
->period
, req
->callback
, req
->arg
);
269 release_object( timer
);
273 /* cancel a waitable timer */
274 DECL_HANDLER(cancel_timer
)
278 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
279 TIMER_MODIFY_STATE
, &timer_ops
)))
281 reply
->signaled
= cancel_timer( timer
);
282 release_object( timer
);
286 /* Get information on a waitable timer */
287 DECL_HANDLER(get_timer_info
)
291 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
292 TIMER_QUERY_STATE
, &timer_ops
)))
294 reply
->when
= timer
->when
;
295 reply
->signaled
= timer
->signaled
;
296 release_object( timer
);