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