server: Add an open_file() function to the object operations.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 unsigned int timer_map_access( struct object *obj, unsigned int access );
57 static void timer_destroy( struct object *obj );
58
59 static const struct object_ops timer_ops =
60 {
61     sizeof(struct timer),      /* size */
62     timer_dump,                /* dump */
63     add_queue,                 /* add_queue */
64     remove_queue,              /* remove_queue */
65     timer_signaled,            /* signaled */
66     timer_satisfied,           /* satisfied */
67     no_signal,                 /* signal */
68     no_get_fd,                 /* get_fd */
69     timer_map_access,          /* map_access */
70     no_lookup_name,            /* lookup_name */
71     no_open_file,              /* open_file */
72     no_close_handle,           /* close_handle */
73     timer_destroy              /* destroy */
74 };
75
76
77 /* create a timer object */
78 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
79                                    unsigned int attr, int manual )
80 {
81     struct timer *timer;
82
83     if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
84     {
85         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
86         {
87             /* initialize it if it didn't already exist */
88             timer->manual       = manual;
89             timer->signaled     = 0;
90             timer->when.tv_sec  = 0;
91             timer->when.tv_usec = 0;
92             timer->period       = 0;
93             timer->timeout      = NULL;
94             timer->thread       = NULL;
95         }
96     }
97     return timer;
98 }
99
100 /* callback on timer expiration */
101 static void timer_callback( void *private )
102 {
103     struct timer *timer = (struct timer *)private;
104
105     /* queue an APC */
106     if (timer->thread)
107     {
108         apc_call_t data;
109
110         memset( &data, 0, sizeof(data) );
111         if (timer->callback)
112         {
113             data.type            = APC_TIMER;
114             data.timer.func      = timer->callback;
115             data.timer.time.sec  = timer->when.tv_sec;
116             data.timer.time.usec = timer->when.tv_usec;
117             data.timer.arg       = timer->arg;
118         }
119         else data.type = APC_NONE;  /* wake up only */
120
121         if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
122         {
123             release_object( timer->thread );
124             timer->thread = NULL;
125         }
126     }
127
128     if (timer->period)  /* schedule the next expiration */
129     {
130         add_timeout( &timer->when, timer->period );
131         timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
132     }
133     else timer->timeout = NULL;
134
135     /* wake up waiters */
136     timer->signaled = 1;
137     wake_up( &timer->obj, 0 );
138 }
139
140 /* cancel a running timer */
141 static int cancel_timer( struct timer *timer )
142 {
143     int signaled = timer->signaled;
144
145     if (timer->timeout)
146     {
147         remove_timeout_user( timer->timeout );
148         timer->timeout = NULL;
149     }
150     if (timer->thread)
151     {
152         thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
153         release_object( timer->thread );
154         timer->thread = NULL;
155     }
156     return signaled;
157 }
158
159 /* set the timer expiration and period */
160 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
161                       void *callback, void *arg )
162 {
163     int signaled = cancel_timer( timer );
164     if (timer->manual)
165     {
166         period = 0;  /* period doesn't make any sense for a manual timer */
167         timer->signaled = 0;
168     }
169     if (!expire->sec && !expire->usec)
170     {
171         /* special case: use now + period as first expiration */
172         timer->when = current_time;
173         add_timeout( &timer->when, period );
174     }
175     else
176     {
177         timer->when.tv_sec  = expire->sec;
178         timer->when.tv_usec = expire->usec;
179     }
180     timer->period       = period;
181     timer->callback     = callback;
182     timer->arg          = arg;
183     if (callback) timer->thread = (struct thread *)grab_object( current );
184     timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
185     return signaled;
186 }
187
188 static void timer_dump( struct object *obj, int verbose )
189 {
190     struct timer *timer = (struct timer *)obj;
191     assert( obj->ops == &timer_ops );
192     fprintf( stderr, "Timer manual=%d when=%ld.%06u period=%d ",
193              timer->manual, timer->when.tv_sec, (unsigned int)timer->when.tv_usec, timer->period );
194     dump_object_name( &timer->obj );
195     fputc( '\n', stderr );
196 }
197
198 static int timer_signaled( struct object *obj, struct thread *thread )
199 {
200     struct timer *timer = (struct timer *)obj;
201     assert( obj->ops == &timer_ops );
202     return timer->signaled;
203 }
204
205 static int timer_satisfied( struct object *obj, struct thread *thread )
206 {
207     struct timer *timer = (struct timer *)obj;
208     assert( obj->ops == &timer_ops );
209     if (!timer->manual) timer->signaled = 0;
210     return 0;
211 }
212
213 static unsigned int timer_map_access( struct object *obj, unsigned int access )
214 {
215     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
216     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
217     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
218     if (access & GENERIC_ALL)     access |= TIMER_ALL_ACCESS;
219     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
220 }
221
222 static void timer_destroy( struct object *obj )
223 {
224     struct timer *timer = (struct timer *)obj;
225     assert( obj->ops == &timer_ops );
226
227     if (timer->timeout) remove_timeout_user( timer->timeout );
228     if (timer->thread) release_object( timer->thread );
229 }
230
231 /* create a timer */
232 DECL_HANDLER(create_timer)
233 {
234     struct timer *timer;
235     struct unicode_str name;
236     struct directory *root = NULL;
237
238     reply->handle = 0;
239     get_req_unicode_str( &name );
240     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
241         return;
242
243     if ((timer = create_timer( root, &name, req->attributes, req->manual )))
244     {
245         reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
246         release_object( timer );
247     }
248
249     if (root) release_object( root );
250 }
251
252 /* open a handle to a timer */
253 DECL_HANDLER(open_timer)
254 {
255     struct unicode_str name;
256     struct directory *root = NULL;
257     struct timer *timer;
258
259     get_req_unicode_str( &name );
260     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
261         return;
262
263     if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
264     {
265         reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
266         release_object( timer );
267     }
268
269     if (root) release_object( root );
270 }
271
272 /* set a waitable timer */
273 DECL_HANDLER(set_timer)
274 {
275     struct timer *timer;
276
277     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
278                                                  TIMER_MODIFY_STATE, &timer_ops )))
279     {
280         reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
281         release_object( timer );
282     }
283 }
284
285 /* cancel a waitable timer */
286 DECL_HANDLER(cancel_timer)
287 {
288     struct timer *timer;
289
290     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
291                                                  TIMER_MODIFY_STATE, &timer_ops )))
292     {
293         reply->signaled = cancel_timer( timer );
294         release_object( timer );
295     }
296 }
297
298 /* Get information on a waitable timer */
299 DECL_HANDLER(get_timer_info)
300 {
301     struct timer *timer;
302
303     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
304                                                  TIMER_QUERY_STATE, &timer_ops )))
305     {
306         reply->when.sec  = timer->when.tv_sec;
307         reply->when.usec = timer->when.tv_usec;
308         reply->signaled  = timer->signaled;
309         release_object( timer );
310     }
311 }