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>
36 struct object obj
; /* object header */
37 int manual
; /* manual reset */
38 int signaled
; /* current signaled state */
39 int period
; /* timer period in ms */
40 struct timeval when
; /* next expiration */
41 struct timeout_user
*timeout
; /* timeout user */
42 struct thread
*thread
; /* thread that set the APC function */
43 void *callback
; /* callback APC function */
44 void *arg
; /* callback argument */
47 static void timer_dump( struct object
*obj
, int verbose
);
48 static int timer_signaled( struct object
*obj
, struct thread
*thread
);
49 static int timer_satisfied( struct object
*obj
, struct thread
*thread
);
50 static void timer_destroy( struct object
*obj
);
52 static const struct object_ops timer_ops
=
54 sizeof(struct timer
), /* size */
55 timer_dump
, /* dump */
56 add_queue
, /* add_queue */
57 remove_queue
, /* remove_queue */
58 timer_signaled
, /* signaled */
59 timer_satisfied
, /* satisfied */
60 no_get_fd
, /* get_fd */
61 timer_destroy
/* destroy */
65 /* create a timer object */
66 static struct timer
*create_timer( const WCHAR
*name
, size_t len
, int manual
)
70 if ((timer
= create_named_object( sync_namespace
, &timer_ops
, name
, len
)))
72 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
74 /* initialize it if it didn't already exist */
75 timer
->manual
= manual
;
77 timer
->when
.tv_sec
= 0;
78 timer
->when
.tv_usec
= 0;
80 timer
->timeout
= NULL
;
87 /* callback on timer expiration */
88 static void timer_callback( void *private )
90 struct timer
*timer
= (struct timer
*)private;
95 if (!thread_queue_apc( timer
->thread
, &timer
->obj
, timer
->callback
, APC_TIMER
, 0, 3,
96 (void *)timer
->when
.tv_sec
, (void *)timer
->when
.tv_usec
, timer
->arg
))
98 release_object( timer
->thread
);
103 if (timer
->period
) /* schedule the next expiration */
105 add_timeout( &timer
->when
, timer
->period
);
106 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
108 else timer
->timeout
= NULL
;
110 /* wake up waiters */
112 wake_up( &timer
->obj
, 0 );
115 /* cancel a running timer */
116 static void cancel_timer( struct timer
*timer
)
120 remove_timeout_user( timer
->timeout
);
121 timer
->timeout
= NULL
;
125 thread_cancel_apc( timer
->thread
, &timer
->obj
, 0 );
126 release_object( timer
->thread
);
127 timer
->thread
= NULL
;
131 /* set the timer expiration and period */
132 static void set_timer( struct timer
*timer
, int sec
, int usec
, int period
,
133 void *callback
, void *arg
)
135 cancel_timer( timer
);
138 period
= 0; /* period doesn't make any sense for a manual timer */
143 /* special case: use now + period as first expiration */
144 gettimeofday( &timer
->when
, 0 );
145 add_timeout( &timer
->when
, period
);
149 timer
->when
.tv_sec
= sec
;
150 timer
->when
.tv_usec
= usec
;
152 timer
->period
= period
;
153 timer
->callback
= callback
;
155 if (callback
) timer
->thread
= (struct thread
*)grab_object( current
);
156 timer
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, timer
);
159 static void timer_dump( struct object
*obj
, int verbose
)
161 struct timer
*timer
= (struct timer
*)obj
;
162 assert( obj
->ops
== &timer_ops
);
163 fprintf( stderr
, "Timer manual=%d when=%ld.%06ld period=%d ",
164 timer
->manual
, timer
->when
.tv_sec
, timer
->when
.tv_usec
, timer
->period
);
165 dump_object_name( &timer
->obj
);
166 fputc( '\n', stderr
);
169 static int timer_signaled( struct object
*obj
, struct thread
*thread
)
171 struct timer
*timer
= (struct timer
*)obj
;
172 assert( obj
->ops
== &timer_ops
);
173 return timer
->signaled
;
176 static int timer_satisfied( struct object
*obj
, struct thread
*thread
)
178 struct timer
*timer
= (struct timer
*)obj
;
179 assert( obj
->ops
== &timer_ops
);
180 if (!timer
->manual
) timer
->signaled
= 0;
184 static void timer_destroy( struct object
*obj
)
186 struct timer
*timer
= (struct timer
*)obj
;
187 assert( obj
->ops
== &timer_ops
);
189 if (timer
->timeout
) remove_timeout_user( timer
->timeout
);
190 if (timer
->thread
) release_object( timer
->thread
);
194 DECL_HANDLER(create_timer
)
199 if ((timer
= create_timer( get_req_data(), get_req_data_size(), req
->manual
)))
201 reply
->handle
= alloc_handle( current
->process
, timer
, TIMER_ALL_ACCESS
, req
->inherit
);
202 release_object( timer
);
206 /* open a handle to a timer */
207 DECL_HANDLER(open_timer
)
209 reply
->handle
= open_object( sync_namespace
, get_req_data(), get_req_data_size(),
210 &timer_ops
, req
->access
, req
->inherit
);
213 /* set a waitable timer */
214 DECL_HANDLER(set_timer
)
218 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
219 TIMER_MODIFY_STATE
, &timer_ops
)))
221 set_timer( timer
, req
->sec
, req
->usec
, req
->period
, req
->callback
, req
->arg
);
222 release_object( timer
);
226 /* cancel a waitable timer */
227 DECL_HANDLER(cancel_timer
)
231 if ((timer
= (struct timer
*)get_handle_obj( current
->process
, req
->handle
,
232 TIMER_MODIFY_STATE
, &timer_ops
)))
234 cancel_timer( timer
);
235 release_object( timer
);