Porting fixes.
[wine.git] / server / timer.c
blob2d48fc6be659a23b91012106c933466bc734101e
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>
30 #include "windef.h"
31 #include "handle.h"
32 #include "request.h"
34 struct timer
36 struct object obj; /* object header */
37 int manual; /* manual reset */
38 int signaled; /* current signaled state */
39 int period; /* timer period in ms */
40 struct timeval when; /* next expiration */
41 struct timeout_user *timeout; /* timeout user */
42 struct thread *thread; /* thread that set the APC function */
43 void *callback; /* callback APC function */
44 void *arg; /* callback argument */
47 static void timer_dump( struct object *obj, int verbose );
48 static int timer_signaled( struct object *obj, struct thread *thread );
49 static int timer_satisfied( struct object *obj, struct thread *thread );
50 static void timer_destroy( struct object *obj );
52 static const struct object_ops timer_ops =
54 sizeof(struct timer), /* size */
55 timer_dump, /* dump */
56 add_queue, /* add_queue */
57 remove_queue, /* remove_queue */
58 timer_signaled, /* signaled */
59 timer_satisfied, /* satisfied */
60 no_get_fd, /* get_fd */
61 no_get_file_info, /* get_file_info */
62 timer_destroy /* destroy */
66 /* create a timer object */
67 static struct timer *create_timer( const WCHAR *name, size_t len, int manual )
69 struct timer *timer;
71 if ((timer = create_named_object( sync_namespace, &timer_ops, name, len )))
73 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
75 /* initialize it if it didn't already exist */
76 timer->manual = manual;
77 timer->signaled = 0;
78 timer->when.tv_sec = 0;
79 timer->when.tv_usec = 0;
80 timer->period = 0;
81 timer->timeout = NULL;
82 timer->thread = NULL;
85 return timer;
88 /* callback on timer expiration */
89 static void timer_callback( void *private )
91 struct timer *timer = (struct timer *)private;
93 /* queue an APC */
94 if (timer->thread)
96 if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0, 3,
97 (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
99 release_object( timer->thread );
100 timer->thread = NULL;
104 if (timer->period) /* schedule the next expiration */
106 add_timeout( &timer->when, timer->period );
107 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
109 else timer->timeout = NULL;
111 /* wake up waiters */
112 timer->signaled = 1;
113 wake_up( &timer->obj, 0 );
116 /* cancel a running timer */
117 static void cancel_timer( struct timer *timer )
119 if (timer->timeout)
121 remove_timeout_user( timer->timeout );
122 timer->timeout = NULL;
124 if (timer->thread)
126 thread_cancel_apc( timer->thread, &timer->obj, 0 );
127 release_object( timer->thread );
128 timer->thread = NULL;
132 /* set the timer expiration and period */
133 static void set_timer( struct timer *timer, int sec, int usec, int period,
134 void *callback, void *arg )
136 cancel_timer( timer );
137 if (timer->manual)
139 period = 0; /* period doesn't make any sense for a manual timer */
140 timer->signaled = 0;
142 if (!sec && !usec)
144 /* special case: use now + period as first expiration */
145 gettimeofday( &timer->when, 0 );
146 add_timeout( &timer->when, period );
148 else
150 timer->when.tv_sec = sec;
151 timer->when.tv_usec = usec;
153 timer->period = period;
154 timer->callback = callback;
155 timer->arg = arg;
156 if (callback) timer->thread = (struct thread *)grab_object( current );
157 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
160 static void timer_dump( struct object *obj, int verbose )
162 struct timer *timer = (struct timer *)obj;
163 assert( obj->ops == &timer_ops );
164 fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
165 timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
166 dump_object_name( &timer->obj );
167 fputc( '\n', stderr );
170 static int timer_signaled( struct object *obj, struct thread *thread )
172 struct timer *timer = (struct timer *)obj;
173 assert( obj->ops == &timer_ops );
174 return timer->signaled;
177 static int timer_satisfied( struct object *obj, struct thread *thread )
179 struct timer *timer = (struct timer *)obj;
180 assert( obj->ops == &timer_ops );
181 if (!timer->manual) timer->signaled = 0;
182 return 0;
185 static void timer_destroy( struct object *obj )
187 struct timer *timer = (struct timer *)obj;
188 assert( obj->ops == &timer_ops );
190 if (timer->timeout) remove_timeout_user( timer->timeout );
191 if (timer->thread) release_object( timer->thread );
194 /* create a timer */
195 DECL_HANDLER(create_timer)
197 struct timer *timer;
199 reply->handle = 0;
200 if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual )))
202 reply->handle = alloc_handle( current->process, timer, TIMER_ALL_ACCESS, req->inherit );
203 release_object( timer );
207 /* open a handle to a timer */
208 DECL_HANDLER(open_timer)
210 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
211 &timer_ops, req->access, req->inherit );
214 /* set a waitable timer */
215 DECL_HANDLER(set_timer)
217 struct timer *timer;
219 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
220 TIMER_MODIFY_STATE, &timer_ops )))
222 set_timer( timer, req->sec, req->usec, req->period, req->callback, req->arg );
223 release_object( timer );
227 /* cancel a waitable timer */
228 DECL_HANDLER(cancel_timer)
230 struct timer *timer;
232 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
233 TIMER_MODIFY_STATE, &timer_ops )))
235 cancel_timer( timer );
236 release_object( timer );