Added Norwegian translation.
[wine/multimedia.git] / server / timer.c
blobc4ab0e874bcbeccc0e5aee61e18ee8de3f52c4c2
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"
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
36 struct timer
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 */
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 );
54 static const struct object_ops timer_ops =
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 no_close_handle, /* close_handle */
65 timer_destroy /* destroy */
69 /* create a timer object */
70 static struct timer *create_timer( const WCHAR *name, size_t len, int manual )
72 struct timer *timer;
74 if ((timer = create_named_object( sync_namespace, &timer_ops, name, len )))
76 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
78 /* initialize it if it didn't already exist */
79 timer->manual = manual;
80 timer->signaled = 0;
81 timer->when.tv_sec = 0;
82 timer->when.tv_usec = 0;
83 timer->period = 0;
84 timer->timeout = NULL;
85 timer->thread = NULL;
88 return timer;
91 /* callback on timer expiration */
92 static void timer_callback( void *private )
94 struct timer *timer = (struct timer *)private;
96 /* queue an APC */
97 if (timer->thread)
99 if (!thread_queue_apc( timer->thread, &timer->obj, timer->callback, APC_TIMER, 0,
100 (void *)timer->when.tv_sec, (void *)timer->when.tv_usec, timer->arg))
102 release_object( timer->thread );
103 timer->thread = NULL;
107 if (timer->period) /* schedule the next expiration */
109 add_timeout( &timer->when, timer->period );
110 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
112 else timer->timeout = NULL;
114 /* wake up waiters */
115 timer->signaled = 1;
116 wake_up( &timer->obj, 0 );
119 /* cancel a running timer */
120 static int cancel_timer( struct timer *timer )
122 int signaled = timer->signaled;
124 if (timer->timeout)
126 remove_timeout_user( timer->timeout );
127 timer->timeout = NULL;
129 if (timer->thread)
131 thread_cancel_apc( timer->thread, &timer->obj, 0 );
132 release_object( timer->thread );
133 timer->thread = NULL;
135 return signaled;
138 /* set the timer expiration and period */
139 static int set_timer( struct timer *timer, const abs_time_t *expire, int period,
140 void *callback, void *arg )
142 int signaled = cancel_timer( timer );
143 if (timer->manual)
145 period = 0; /* period doesn't make any sense for a manual timer */
146 timer->signaled = 0;
148 if (!expire->sec && !expire->usec)
150 /* special case: use now + period as first expiration */
151 gettimeofday( &timer->when, NULL );
152 add_timeout( &timer->when, period );
154 else
156 timer->when.tv_sec = expire->sec;
157 timer->when.tv_usec = expire->usec;
159 timer->period = period;
160 timer->callback = callback;
161 timer->arg = arg;
162 if (callback) timer->thread = (struct thread *)grab_object( current );
163 timer->timeout = add_timeout_user( &timer->when, timer_callback, timer );
164 return signaled;
167 static void timer_dump( struct object *obj, int verbose )
169 struct timer *timer = (struct timer *)obj;
170 assert( obj->ops == &timer_ops );
171 fprintf( stderr, "Timer manual=%d when=%ld.%06ld period=%d ",
172 timer->manual, timer->when.tv_sec, timer->when.tv_usec, timer->period );
173 dump_object_name( &timer->obj );
174 fputc( '\n', stderr );
177 static int timer_signaled( struct object *obj, struct thread *thread )
179 struct timer *timer = (struct timer *)obj;
180 assert( obj->ops == &timer_ops );
181 return timer->signaled;
184 static int timer_satisfied( struct object *obj, struct thread *thread )
186 struct timer *timer = (struct timer *)obj;
187 assert( obj->ops == &timer_ops );
188 if (!timer->manual) timer->signaled = 0;
189 return 0;
192 static void timer_destroy( struct object *obj )
194 struct timer *timer = (struct timer *)obj;
195 assert( obj->ops == &timer_ops );
197 if (timer->timeout) remove_timeout_user( timer->timeout );
198 if (timer->thread) release_object( timer->thread );
201 /* create a timer */
202 DECL_HANDLER(create_timer)
204 struct timer *timer;
206 reply->handle = 0;
207 if ((timer = create_timer( get_req_data(), get_req_data_size(), req->manual )))
209 reply->handle = alloc_handle( current->process, timer, req->access, req->inherit );
210 release_object( timer );
214 /* open a handle to a timer */
215 DECL_HANDLER(open_timer)
217 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
218 &timer_ops, req->access, req->inherit );
221 /* set a waitable timer */
222 DECL_HANDLER(set_timer)
224 struct timer *timer;
226 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
227 TIMER_MODIFY_STATE, &timer_ops )))
229 reply->signaled = set_timer( timer, &req->expire, req->period, req->callback, req->arg );
230 release_object( timer );
234 /* cancel a waitable timer */
235 DECL_HANDLER(cancel_timer)
237 struct timer *timer;
239 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
240 TIMER_MODIFY_STATE, &timer_ops )))
242 reply->signaled = cancel_timer( timer );
243 release_object( timer );
247 /* Get information on a waitable timer */
248 DECL_HANDLER(get_timer_info)
250 struct timer *timer;
252 if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
253 TIMER_QUERY_STATE, &timer_ops )))
255 reply->when.sec = timer->when.tv_sec;
256 reply->when.usec = timer->when.tv_usec;
257 reply->signaled = timer->signaled;
258 release_object( timer );