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 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 no_lookup_name
, /* lookup_name */
75 no_open_file
, /* open_file */
76 no_close_handle
, /* close_handle */
77 timer_destroy
/* destroy */
81 /* create a timer object */
82 static struct timer
*create_timer( struct directory
*root
, const struct unicode_str
*name
,
83 unsigned int attr
, int manual
, const struct security_descriptor
*sd
)
87 if ((timer
= create_named_object_dir( root
, name
, attr
, &timer_ops
)))
89 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
91 /* initialize it if it didn't already exist */
92 timer
->manual
= manual
;
96 timer
->timeout
= NULL
;
98 if (sd
) default_set_sd( &timer
->obj
, sd
,
99 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
100 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
106 /* callback on timer expiration */
107 static void timer_callback( void *private )
109 struct timer
*timer
= (struct timer
*)private;
116 memset( &data
, 0, sizeof(data
) );
119 data
.type
= APC_TIMER
;
120 data
.timer
.func
= timer
->callback
;
121 data
.timer
.time
= timer
->when
;
122 data
.timer
.arg
= timer
->arg
;
124 else data
.type
= APC_NONE
; /* wake up only */
126 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, &data
))
128 release_object( timer
->thread
);
129 timer
->thread
= NULL
;
133 if (timer
->period
) /* schedule the next expiration */
135 timer
->when
+= (timeout_t
)timer
->period
* 10000;
136 timer
->timeout
= add_timeout_user( timer
->when
, timer_callback
, timer
);
138 else timer
->timeout
= NULL
;
140 /* wake up waiters */
142 wake_up( &timer
->obj
, 0 );
145 /* cancel a running timer */
146 static int cancel_timer( struct timer
*timer
)
148 int signaled
= timer
->signaled
;
152 remove_timeout_user( timer
->timeout
);
153 timer
->timeout
= NULL
;
157 thread_cancel_apc( timer
->thread
, &timer
->obj
, APC_TIMER
);
158 release_object( timer
->thread
);
159 timer
->thread
= NULL
;
164 /* set the timer expiration and period */
165 static int set_timer( struct timer
*timer
, timeout_t expire
, unsigned int period
,
166 client_ptr_t callback
, client_ptr_t arg
)
168 int signaled
= cancel_timer( timer
);
171 period
= 0; /* period doesn't make any sense for a manual timer */
174 timer
->when
= (expire
<= 0) ? current_time
- expire
: max( expire
, current_time
);
175 timer
->period
= period
;
176 timer
->callback
= callback
;
178 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
179 timer
->timeout
= add_timeout_user( timer
->when
, timer_callback
, timer
);
183 static void timer_dump( struct object
*obj
, int verbose
)
185 struct timer
*timer
= (struct timer
*)obj
;
186 assert( obj
->ops
== &timer_ops
);
187 fprintf( stderr
, "Timer manual=%d when=%s period=%u\n",
188 timer
->manual
, get_timeout_str(timer
->when
), timer
->period
);
191 static struct object_type
*timer_get_type( struct object
*obj
)
193 static const WCHAR name
[] = {'T','i','m','e','r'};
194 static const struct unicode_str str
= { name
, sizeof(name
) };
195 return get_object_type( &str
);
198 static int timer_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
200 struct timer
*timer
= (struct timer
*)obj
;
201 assert( obj
->ops
== &timer_ops
);
202 return timer
->signaled
;
205 static void timer_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
207 struct timer
*timer
= (struct timer
*)obj
;
208 assert( obj
->ops
== &timer_ops
);
209 if (!timer
->manual
) timer
->signaled
= 0;
212 static unsigned int timer_map_access( struct object
*obj
, unsigned int access
)
214 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
| TIMER_QUERY_STATE
;
215 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| TIMER_MODIFY_STATE
;
216 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
217 if (access
& GENERIC_ALL
) access
|= TIMER_ALL_ACCESS
;
218 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
221 static void timer_destroy( struct object
*obj
)
223 struct timer
*timer
= (struct timer
*)obj
;
224 assert( obj
->ops
== &timer_ops
);
226 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
227 if (timer
->thread
) release_object( timer
->thread
);
231 DECL_HANDLER(create_timer
)
234 struct unicode_str name
;
235 struct directory
*root
;
236 const struct security_descriptor
*sd
;
237 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
239 if (!objattr
) return;
241 if ((timer
= create_timer( root
, &name
, objattr
->attributes
, req
->manual
, sd
)))
243 reply
->handle
= alloc_handle( current
->process
, timer
, req
->access
, objattr
->attributes
);
244 release_object( timer
);
247 if (root
) release_object( root
);
250 /* open a handle to a timer */
251 DECL_HANDLER(open_timer
)
253 struct unicode_str name
= get_req_unicode_str();
255 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
256 &timer_ops
, &name
, req
->attributes
);
259 /* set a waitable timer */
260 DECL_HANDLER(set_timer
)
264 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
265 TIMER_MODIFY_STATE
, &timer_ops
)))
267 reply
->signaled
= set_timer( timer
, req
->expire
, req
->period
, req
->callback
, req
->arg
);
268 release_object( timer
);
272 /* cancel a waitable timer */
273 DECL_HANDLER(cancel_timer
)
277 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
278 TIMER_MODIFY_STATE
, &timer_ops
)))
280 reply
->signaled
= cancel_timer( timer
);
281 release_object( timer
);
285 /* Get information on a waitable timer */
286 DECL_HANDLER(get_timer_info
)
290 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
291 TIMER_QUERY_STATE
, &timer_ops
)))
293 reply
->when
= timer
->when
;
294 reply
->signaled
= timer
->signaled
;
295 release_object( timer
);