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