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
40 static const WCHAR timer_name
[] = {'T','i','m','e','r'};
42 struct type_descr timer_type
=
44 { timer_name
, sizeof(timer_name
) }, /* name */
45 TIMER_ALL_ACCESS
, /* valid_access */
47 STANDARD_RIGHTS_READ
| TIMER_QUERY_STATE
,
48 STANDARD_RIGHTS_WRITE
| TIMER_MODIFY_STATE
,
49 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
56 struct object obj
; /* object header */
57 int manual
; /* manual reset */
58 int signaled
; /* current signaled state */
59 unsigned int period
; /* timer period in ms */
60 abstime_t when
; /* next expiration */
61 struct timeout_user
*timeout
; /* timeout user */
62 struct thread
*thread
; /* thread that set the APC function */
63 client_ptr_t callback
; /* callback APC function */
64 client_ptr_t arg
; /* callback argument */
67 static void timer_dump( struct object
*obj
, int verbose
);
68 static int timer_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
69 static void timer_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
);
70 static void timer_destroy( struct object
*obj
);
72 static const struct object_ops timer_ops
=
74 sizeof(struct timer
), /* size */
75 &timer_type
, /* type */
76 timer_dump
, /* dump */
77 add_queue
, /* add_queue */
78 remove_queue
, /* remove_queue */
79 timer_signaled
, /* signaled */
80 timer_satisfied
, /* satisfied */
81 no_signal
, /* signal */
82 no_get_fd
, /* get_fd */
83 default_map_access
, /* map_access */
84 default_get_sd
, /* get_sd */
85 default_set_sd
, /* set_sd */
86 default_get_full_name
, /* get_full_name */
87 no_lookup_name
, /* lookup_name */
88 directory_link_name
, /* link_name */
89 default_unlink_name
, /* unlink_name */
90 no_open_file
, /* open_file */
91 no_kernel_obj_list
, /* get_kernel_obj_list */
92 no_close_handle
, /* close_handle */
93 timer_destroy
/* destroy */
97 /* create a timer object */
98 static struct timer
*create_timer( struct object
*root
, const struct unicode_str
*name
,
99 unsigned int attr
, int manual
, const struct security_descriptor
*sd
)
103 if ((timer
= create_named_object( root
, &timer_ops
, name
, attr
, sd
)))
105 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
107 /* initialize it if it didn't already exist */
108 timer
->manual
= manual
;
112 timer
->timeout
= NULL
;
113 timer
->thread
= NULL
;
119 /* callback on timer expiration */
120 static void timer_callback( void *private )
122 struct timer
*timer
= (struct timer
*)private;
129 assert (timer
->callback
);
130 memset( &data
, 0, sizeof(data
) );
131 data
.type
= APC_TIMER
;
132 data
.user
.timer
.func
= timer
->callback
;
133 data
.user
.timer
.time
= timer
->when
;
134 data
.user
.timer
.arg
= timer
->arg
;
136 if (!thread_queue_apc( NULL
, timer
->thread
, &timer
->obj
, &data
))
138 release_object( timer
->thread
);
139 timer
->thread
= NULL
;
143 if (timer
->period
) /* schedule the next expiration */
145 if (timer
->when
> 0) timer
->when
= -monotonic_time
;
146 timer
->when
-= (abstime_t
)timer
->period
* 10000;
147 timer
->timeout
= add_timeout_user( abstime_to_timeout(timer
->when
), timer_callback
, timer
);
149 else timer
->timeout
= NULL
;
151 /* wake up waiters */
153 wake_up( &timer
->obj
, 0 );
156 /* cancel a running timer */
157 static int cancel_timer( struct timer
*timer
)
159 int signaled
= timer
->signaled
;
163 remove_timeout_user( timer
->timeout
);
164 timer
->timeout
= NULL
;
168 thread_cancel_apc( timer
->thread
, &timer
->obj
, APC_TIMER
);
169 release_object( timer
->thread
);
170 timer
->thread
= NULL
;
175 /* set the timer expiration and period */
176 static int set_timer( struct timer
*timer
, timeout_t expire
, unsigned int period
,
177 client_ptr_t callback
, client_ptr_t arg
)
179 int signaled
= cancel_timer( timer
);
182 period
= 0; /* period doesn't make any sense for a manual timer */
185 timer
->when
= (expire
<= 0) ? expire
- monotonic_time
: max( expire
, current_time
);
186 timer
->period
= period
;
187 timer
->callback
= callback
;
189 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
190 if (expire
!= TIMEOUT_INFINITE
)
191 timer
->timeout
= add_timeout_user( expire
, timer_callback
, timer
);
195 static void timer_dump( struct object
*obj
, int verbose
)
197 struct timer
*timer
= (struct timer
*)obj
;
198 timeout_t timeout
= abstime_to_timeout( timer
->when
);
199 assert( obj
->ops
== &timer_ops
);
200 fprintf( stderr
, "Timer manual=%d when=%s period=%u\n",
201 timer
->manual
, get_timeout_str(timeout
), timer
->period
);
204 static int timer_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
206 struct timer
*timer
= (struct timer
*)obj
;
207 assert( obj
->ops
== &timer_ops
);
208 return timer
->signaled
;
211 static void timer_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
213 struct timer
*timer
= (struct timer
*)obj
;
214 assert( obj
->ops
== &timer_ops
);
215 if (!timer
->manual
) timer
->signaled
= 0;
218 static void timer_destroy( struct object
*obj
)
220 struct timer
*timer
= (struct timer
*)obj
;
221 assert( obj
->ops
== &timer_ops
);
223 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
224 if (timer
->thread
) release_object( timer
->thread
);
228 DECL_HANDLER(create_timer
)
231 struct unicode_str name
;
233 const struct security_descriptor
*sd
;
234 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
236 if (!objattr
) return;
238 if ((timer
= create_timer( root
, &name
, objattr
->attributes
, req
->manual
, sd
)))
240 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
, objattr
->attributes
);
241 release_object( timer
);
244 if (root
) release_object( root
);
247 /* open a handle to a timer */
248 DECL_HANDLER(open_timer
)
250 struct unicode_str name
= get_req_unicode_str();
252 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
253 &timer_ops
, &name
, req
->attributes
);
256 /* set a waitable timer */
257 DECL_HANDLER(set_timer
)
261 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
262 TIMER_MODIFY_STATE
, &timer_ops
)))
264 reply
->signaled
= set_timer( timer
, req
->expire
, req
->period
, req
->callback
, req
->arg
);
265 release_object( timer
);
269 /* cancel a waitable timer */
270 DECL_HANDLER(cancel_timer
)
274 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
275 TIMER_MODIFY_STATE
, &timer_ops
)))
277 reply
->signaled
= cancel_timer( timer
);
278 release_object( timer
);
282 /* Get information on a waitable timer */
283 DECL_HANDLER(get_timer_info
)
287 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
288 TIMER_QUERY_STATE
, &timer_ops
)))
290 reply
->when
= timer
->when
;
291 reply
->signaled
= timer
->signaled
;
292 release_object( timer
);