CreateWindow should not activate invisible minimized or maximized
[wine/dcerpc.git] / server / timer.c
blob2576dcd8e09d9e414db53e045fb8b84950503700
1 /*
2 * Waitable timers management
4 * Copyright (C) 1999 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "file.h"
37 #include "handle.h"
38 #include "request.h"
40 struct timer
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 */
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 );
58 static const struct object_ops timer_ops =
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 */
74 /* create a timer object */
75 static struct timer *create_timer( const struct unicode_str *name, unsigned int attr,
76 int manual )
78 struct timer *timer;
80 if ((timer = create_named_object( sync_namespace, &timer_ops, name, attr )))
82 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
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;
94 return timer;
97 /* callback on timer expiration */
98 static void timer_callback( void *private )
100 struct timer *timer = (struct timer *)private;
102 /* queue an APC */
103 if (timer->thread)
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))
108 release_object( timer->thread );
109 timer->thread = NULL;
113 if (timer->period) /* schedule the next expiration */
115 add_timeout( &timer->when, timer->period );
116 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
118 else timer->timeout = NULL;
120 /* wake up waiters */
121 timer->signaled = 1;
122 wake_up( &timer->obj, 0 );
125 /* cancel a running timer */
126 static int cancel_timer( struct timer *timer )
128 int signaled = timer->signaled;
130 if (timer->timeout)
132 remove_timeout_user( timer->timeout );
133 timer->timeout = NULL;
135 if (timer->thread)
137 thread_cancel_apc( timer->thread, &timer->obj, 0 );
138 release_object( timer->thread );
139 timer->thread = NULL;
141 return signaled;
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 )
148 int signaled = cancel_timer( timer );
149 if (timer->manual)
151 period = 0; /* period doesn't make any sense for a manual timer */
152 timer->signaled = 0;
154 if (!expire->sec && !expire->usec)
156 /* special case: use now + period as first expiration */
157 gettimeofday( &timer->when, NULL );
158 add_timeout( &timer->when, period );
160 else
162 timer->when.tv_sec = expire->sec;
163 timer->when.tv_usec = expire->usec;
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;
173 static void timer_dump( struct object *obj, int verbose )
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 );
183 static int timer_signaled( struct object *obj, struct thread *thread )
185 struct timer *timer = (struct timer *)obj;
186 assert( obj->ops == &timer_ops );
187 return timer->signaled;
190 static int timer_satisfied( struct object *obj, struct thread *thread )
192 struct timer *timer = (struct timer *)obj;
193 assert( obj->ops == &timer_ops );
194 if (!timer->manual) timer->signaled = 0;
195 return 0;
198 static void timer_destroy( struct object *obj )
200 struct timer *timer = (struct timer *)obj;
201 assert( obj->ops == &timer_ops );
203 if (timer->timeout) remove_timeout_user( timer->timeout );
204 if (timer->thread) release_object( timer->thread );
207 /* create a timer */
208 DECL_HANDLER(create_timer)
210 struct timer *timer;
211 struct unicode_str name;
213 reply->handle = 0;
214 get_req_unicode_str( &name );
215 if ((timer = create_timer( &name, req->attributes, req->manual )))
217 reply->handle = alloc_handle( current->process, timer, req->access,
218 req->attributes & OBJ_INHERIT );
219 release_object( timer );
223 /* open a handle to a timer */
224 DECL_HANDLER(open_timer)
226 struct unicode_str name;
228 get_req_unicode_str( &name );
229 reply->handle = open_object( sync_namespace, &name, &timer_ops, req->access, req->attributes );
232 /* set a waitable timer */
233 DECL_HANDLER(set_timer)
235 struct timer *timer;
237 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
238 TIMER_MODIFY_STATE, &timer_ops )))
240 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
241 release_object( timer );
245 /* cancel a waitable timer */
246 DECL_HANDLER(cancel_timer)
248 struct timer *timer;
250 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
251 TIMER_MODIFY_STATE, &timer_ops )))
253 reply->signaled = cancel_timer( timer );
254 release_object( timer );
258 /* Get information on a waitable timer */
259 DECL_HANDLER(get_timer_info)
261 struct timer *timer;
263 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
264 TIMER_QUERY_STATE, &timer_ops )))
266 reply->when.sec = timer->when.tv_sec;
267 reply->when.usec = timer->when.tv_usec;
268 reply->signaled = timer->signaled;
269 release_object( timer );