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 abstime_t when
; /* next expiration */
47 struct timeout_user
*timeout
; /* timeout user */
48 struct thread
*thread
; /* thread that set the APC function */
49 client_ptr_t callback
; /* callback APC function */
50 client_ptr_t arg
; /* callback argument */
53 static void timer_dump( struct object
*obj
, int verbose
);
54 static struct object_type
*timer_get_type( struct object
*obj
);
55 static int timer_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
56 static void timer_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
);
57 static unsigned int timer_map_access( struct object
*obj
, unsigned int access
);
58 static void timer_destroy( struct object
*obj
);
60 static const struct object_ops timer_ops
=
62 sizeof(struct timer
), /* size */
63 timer_dump
, /* dump */
64 timer_get_type
, /* get_type */
65 add_queue
, /* add_queue */
66 remove_queue
, /* remove_queue */
67 timer_signaled
, /* signaled */
68 timer_satisfied
, /* satisfied */
69 no_signal
, /* signal */
70 no_get_fd
, /* get_fd */
71 timer_map_access
, /* map_access */
72 default_get_sd
, /* get_sd */
73 default_set_sd
, /* set_sd */
74 default_get_full_name
, /* get_full_name */
75 no_lookup_name
, /* lookup_name */
76 directory_link_name
, /* link_name */
77 default_unlink_name
, /* unlink_name */
78 no_open_file
, /* open_file */
79 no_kernel_obj_list
, /* get_kernel_obj_list */
80 no_close_handle
, /* close_handle */
81 timer_destroy
/* destroy */
85 /* create a timer object */
86 static struct timer
*create_timer( struct object
*root
, const struct unicode_str
*name
,
87 unsigned int attr
, int manual
, const struct security_descriptor
*sd
)
91 if ((timer
= create_named_object( root
, &timer_ops
, name
, attr
, sd
)))
93 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
95 /* initialize it if it didn't already exist */
96 timer
->manual
= manual
;
100 timer
->timeout
= NULL
;
101 timer
->thread
= NULL
;
107 /* callback on timer expiration */
108 static void timer_callback( void *private )
110 struct timer
*timer
= (struct timer
*)private;
117 memset( &data
, 0, sizeof(data
) );
120 data
.type
= APC_TIMER
;
121 data
.user
.timer
.func
= timer
->callback
;
122 data
.user
.timer
.time
= timer
->when
;
123 data
.user
.timer
.arg
= timer
->arg
;
125 else data
.type
= APC_NONE
; /* wake up only */
127 if (!thread_queue_apc( NULL
, timer
->thread
, &timer
->obj
, &data
))
129 release_object( timer
->thread
);
130 timer
->thread
= NULL
;
134 if (timer
->period
) /* schedule the next expiration */
136 if (timer
->when
> 0) timer
->when
= -monotonic_time
;
137 timer
->when
-= (abstime_t
)timer
->period
* 10000;
138 timer
->timeout
= add_timeout_user( abstime_to_timeout(timer
->when
), timer_callback
, timer
);
140 else timer
->timeout
= NULL
;
142 /* wake up waiters */
144 wake_up( &timer
->obj
, 0 );
147 /* cancel a running timer */
148 static int cancel_timer( struct timer
*timer
)
150 int signaled
= timer
->signaled
;
154 remove_timeout_user( timer
->timeout
);
155 timer
->timeout
= NULL
;
159 thread_cancel_apc( timer
->thread
, &timer
->obj
, APC_TIMER
);
160 release_object( timer
->thread
);
161 timer
->thread
= NULL
;
166 /* set the timer expiration and period */
167 static int set_timer( struct timer
*timer
, timeout_t expire
, unsigned int period
,
168 client_ptr_t callback
, client_ptr_t arg
)
170 int signaled
= cancel_timer( timer
);
173 period
= 0; /* period doesn't make any sense for a manual timer */
176 timer
->when
= (expire
<= 0) ? expire
- monotonic_time
: max( expire
, current_time
);
177 timer
->period
= period
;
178 timer
->callback
= callback
;
180 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
181 if (expire
!= TIMEOUT_INFINITE
)
182 timer
->timeout
= add_timeout_user( expire
, timer_callback
, timer
);
186 static void timer_dump( struct object
*obj
, int verbose
)
188 struct timer
*timer
= (struct timer
*)obj
;
189 timeout_t timeout
= abstime_to_timeout( timer
->when
);
190 assert( obj
->ops
== &timer_ops
);
191 fprintf( stderr
, "Timer manual=%d when=%s period=%u\n",
192 timer
->manual
, get_timeout_str(timeout
), timer
->period
);
195 static struct object_type
*timer_get_type( struct object
*obj
)
197 static const WCHAR name
[] = {'T','i','m','e','r'};
198 static const struct unicode_str str
= { name
, sizeof(name
) };
199 return get_object_type( &str
);
202 static int timer_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
204 struct timer
*timer
= (struct timer
*)obj
;
205 assert( obj
->ops
== &timer_ops
);
206 return timer
->signaled
;
209 static void timer_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
211 struct timer
*timer
= (struct timer
*)obj
;
212 assert( obj
->ops
== &timer_ops
);
213 if (!timer
->manual
) timer
->signaled
= 0;
216 static unsigned int timer_map_access( struct object
*obj
, unsigned int access
)
218 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
| TIMER_QUERY_STATE
;
219 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| TIMER_MODIFY_STATE
;
220 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
221 if (access
& GENERIC_ALL
) access
|= TIMER_ALL_ACCESS
;
222 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
225 static void timer_destroy( struct object
*obj
)
227 struct timer
*timer
= (struct timer
*)obj
;
228 assert( obj
->ops
== &timer_ops
);
230 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
231 if (timer
->thread
) release_object( timer
->thread
);
235 DECL_HANDLER(create_timer
)
238 struct unicode_str name
;
240 const struct security_descriptor
*sd
;
241 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
243 if (!objattr
) return;
245 if ((timer
= create_timer( root
, &name
, objattr
->attributes
, req
->manual
, sd
)))
247 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
, objattr
->attributes
);
248 release_object( timer
);
251 if (root
) release_object( root
);
254 /* open a handle to a timer */
255 DECL_HANDLER(open_timer
)
257 struct unicode_str name
= get_req_unicode_str();
259 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
260 &timer_ops
, &name
, req
->attributes
);
263 /* set a waitable timer */
264 DECL_HANDLER(set_timer
)
268 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
269 TIMER_MODIFY_STATE
, &timer_ops
)))
271 reply
->signaled
= set_timer( timer
, req
->expire
, req
->period
, req
->callback
, req
->arg
);
272 release_object( timer
);
276 /* cancel a waitable timer */
277 DECL_HANDLER(cancel_timer
)
281 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
282 TIMER_MODIFY_STATE
, &timer_ops
)))
284 reply
->signaled
= cancel_timer( timer
);
285 release_object( timer
);
289 /* Get information on a waitable timer */
290 DECL_HANDLER(get_timer_info
)
294 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
295 TIMER_QUERY_STATE
, &timer_ops
)))
297 reply
->when
= timer
->when
;
298 reply
->signaled
= timer
->signaled
;
299 release_object( timer
);