Implement NtQueryTimer.
[wine] / server / timer.c
1 /*
2  * Waitable timers management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29
30 #include "windef.h"
31
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
35
36 struct timer
37 {
38     struct object        obj;       /* object header */
39     int                  manual;    /* manual reset */
40     int                  signaled;  /* current signaled state */
41     int                  period;    /* timer period in ms */
42     struct timeval       when;      /* next expiration */
43     struct timeout_user *timeout;   /* timeout user */
44     struct thread       *thread;    /* thread that set the APC function */
45     void                *callback;  /* callback APC function */
46     void                *arg;       /* callback argument */
47 };
48
49 static void timer_dump( struct object *obj, int verbose );
50 static int timer_signaled( struct object *obj, struct thread *thread );
51 static int timer_satisfied( struct object *obj, struct thread *thread );
52 static void timer_destroy( struct object *obj );
53
54 static const struct object_ops timer_ops =
55 {
56     sizeof(struct timer),      /* size */
57     timer_dump,                /* dump */
58     add_queue,                 /* add_queue */
59     remove_queue,              /* remove_queue */
60     timer_signaled,            /* signaled */
61     timer_satisfied,           /* satisfied */
62     no_get_fd,                 /* get_fd */
63     timer_destroy              /* destroy */
64 };
65
66
67 /* create a timer object */
68 static struct timer *create_timer( const WCHAR *name, size_t len, int manual )
69 {
70     struct timer *timer;
71
72     if ((timer = create_named_object( sync_namespace, &timer_ops, name, len )))
73     {
74         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
75         {
76             /* initialize it if it didn't already exist */
77             timer->manual       = manual;
78             timer->signaled     = 0;
79             timer->when.tv_sec  = 0;
80             timer->when.tv_usec = 0;
81             timer->period       = 0;
82             timer->timeout      = NULL;
83             timer->thread       = NULL;
84         }
85     }
86     return timer;
87 }
88
89 /* callback on timer expiration */
90 static void timer_callback( void *private )
91 {
92     struct timer *timer = (struct timer *)private;
93
94     /* queue an APC */
95     if (timer->thread)
96     {
97         if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0,
98                                (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
99         {
100             release_object( timer->thread );
101             timer->thread = NULL;
102         }
103     }
104
105     if (timer->period)  /* schedule the next expiration */
106     {
107         add_timeout( &timer->when, timer->period );
108         timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
109     }
110     else timer->timeout = NULL;
111
112     /* wake up waiters */
113     timer->signaled = 1;
114     wake_up( &timer->obj, 0 );
115 }
116
117 /* cancel a running timer */
118 static int cancel_timer( struct timer *timer )
119 {
120     int signaled = timer->signaled;
121
122     if (timer->timeout)
123     {
124         remove_timeout_user( timer->timeout );
125         timer->timeout = NULL;
126     }
127     if (timer->thread)
128     {
129         thread_cancel_apc( timer->thread, &timer->obj, 0 );
130         release_object( timer->thread );
131         timer->thread = NULL;
132     }
133     return signaled;
134 }
135
136 /* set the timer expiration and period */
137 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
138                       void *callback, void *arg )
139 {
140     int signaled = cancel_timer( timer );
141     if (timer->manual)
142     {
143         period = 0;  /* period doesn't make any sense for a manual timer */
144         timer->signaled = 0;
145     }
146     if (!expire->sec && !expire->usec)
147     {
148         /* special case: use now + period as first expiration */
149         gettimeofday( &timer->when, 0 );
150         add_timeout( &timer->when, period );
151     }
152     else
153     {
154         timer->when.tv_sec  = expire->sec;
155         timer->when.tv_usec = expire->usec;
156     }
157     timer->period       = period;
158     timer->callback     = callback;
159     timer->arg          = arg;
160     if (callback) timer->thread = (struct thread *)grab_object( current );
161     timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
162     return signaled;
163 }
164
165 static void timer_dump( struct object *obj, int verbose )
166 {
167     struct timer *timer = (struct timer *)obj;
168     assert( obj->ops == &timer_ops );
169     fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
170              timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
171     dump_object_name( &timer->obj );
172     fputc( '\n', stderr );
173 }
174
175 static int timer_signaled( struct object *obj, struct thread *thread )
176 {
177     struct timer *timer = (struct timer *)obj;
178     assert( obj->ops == &timer_ops );
179     return timer->signaled;
180 }
181
182 static int timer_satisfied( struct object *obj, struct thread *thread )
183 {
184     struct timer *timer = (struct timer *)obj;
185     assert( obj->ops == &timer_ops );
186     if (!timer->manual) timer->signaled = 0;
187     return 0;
188 }
189
190 static void timer_destroy( struct object *obj )
191 {
192     struct timer *timer = (struct timer *)obj;
193     assert( obj->ops == &timer_ops );
194
195     if (timer->timeout) remove_timeout_user( timer->timeout );
196     if (timer->thread) release_object( timer->thread );
197 }
198
199 /* create a timer */
200 DECL_HANDLER(create_timer)
201 {
202     struct timer *timer;
203
204     reply->handle = 0;
205     if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual )))
206     {
207         reply->handle = alloc_handle( current->process, timer, req->access, req->inherit );
208         release_object( timer );
209     }
210 }
211
212 /* open a handle to a timer */
213 DECL_HANDLER(open_timer)
214 {
215     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
216                                  &timer_ops, req->access, req->inherit );
217 }
218
219 /* set a waitable timer */
220 DECL_HANDLER(set_timer)
221 {
222     struct timer *timer;
223
224     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
225                                                  TIMER_MODIFY_STATE, &timer_ops )))
226     {
227         reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
228         release_object( timer );
229     }
230 }
231
232 /* cancel a waitable timer */
233 DECL_HANDLER(cancel_timer)
234 {
235     struct timer *timer;
236
237     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
238                                                  TIMER_MODIFY_STATE, &timer_ops )))
239     {
240         reply->signaled = cancel_timer( timer );
241         release_object( timer );
242     }
243 }
244
245 /* Get information on a waitable timer */
246 DECL_HANDLER(get_timer_info)
247 {
248     struct timer *timer;
249
250     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
251                                                  TIMER_QUERY_STATE, &timer_ops )))
252     {
253         reply->when.sec  = timer->when.tv_sec;
254         reply->when.usec = timer->when.tv_usec;
255         reply->signaled  = timer->signaled;
256         release_object( timer );
257     }
258 }