Implemented NtQueueApcThread, and changed the server APC interface to
[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 void cancel_timer( struct timer *timer )
119 {
120     if (timer->timeout)
121     {
122         remove_timeout_user( timer->timeout );
123         timer->timeout = NULL;
124     }
125     if (timer->thread)
126     {
127         thread_cancel_apc( timer->thread, &timer->obj, 0 );
128         release_object( timer->thread );
129         timer->thread = NULL;
130     }
131 }
132
133 /* set the timer expiration and period */
134 static void set_timer( struct timer *timer, const abs_time_t *expire, int period,
135                        void *callback, void *arg )
136 {
137     cancel_timer( timer );
138     if (timer->manual)
139     {
140         period = 0;  /* period doesn't make any sense for a manual timer */
141         timer->signaled = 0;
142     }
143     if (!expire->sec && !expire->usec)
144     {
145         /* special case: use now + period as first expiration */
146         gettimeofday( &timer->when, 0 );
147         add_timeout( &timer->when, period );
148     }
149     else
150     {
151         timer->when.tv_sec  = expire->sec;
152         timer->when.tv_usec = expire->usec;
153     }
154     timer->period       = period;
155     timer->callback     = callback;
156     timer->arg          = arg;
157     if (callback) timer->thread = (struct thread *)grab_object( current );
158     timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
159 }
160
161 static void timer_dump( struct object *obj, int verbose )
162 {
163     struct timer *timer = (struct timer *)obj;
164     assert( obj->ops == &timer_ops );
165     fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
166              timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
167     dump_object_name( &timer->obj );
168     fputc( '\n', stderr );
169 }
170
171 static int timer_signaled( struct object *obj, struct thread *thread )
172 {
173     struct timer *timer = (struct timer *)obj;
174     assert( obj->ops == &timer_ops );
175     return timer->signaled;
176 }
177
178 static int timer_satisfied( struct object *obj, struct thread *thread )
179 {
180     struct timer *timer = (struct timer *)obj;
181     assert( obj->ops == &timer_ops );
182     if (!timer->manual) timer->signaled = 0;
183     return 0;
184 }
185
186 static void timer_destroy( struct object *obj )
187 {
188     struct timer *timer = (struct timer *)obj;
189     assert( obj->ops == &timer_ops );
190
191     if (timer->timeout) remove_timeout_user( timer->timeout );
192     if (timer->thread) release_object( timer->thread );
193 }
194
195 /* create a timer */
196 DECL_HANDLER(create_timer)
197 {
198     struct timer *timer;
199
200     reply->handle = 0;
201     if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual )))
202     {
203         reply->handle = alloc_handle( current->process, timer, TIMER_ALL_ACCESS, req->inherit );
204         release_object( timer );
205     }
206 }
207
208 /* open a handle to a timer */
209 DECL_HANDLER(open_timer)
210 {
211     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
212                                  &timer_ops, req->access, req->inherit );
213 }
214
215 /* set a waitable timer */
216 DECL_HANDLER(set_timer)
217 {
218     struct timer *timer;
219
220     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
221                                                  TIMER_MODIFY_STATE, &timer_ops )))
222     {
223         set_timer( timer, &req->expire, req->period, req->callback, req->arg );
224         release_object( timer );
225     }
226 }
227
228 /* cancel a waitable timer */
229 DECL_HANDLER(cancel_timer)
230 {
231     struct timer *timer;
232
233     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
234                                                  TIMER_MODIFY_STATE, &timer_ops )))
235     {
236         cancel_timer( timer );
237         release_object( timer );
238     }
239 }