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