timer.c 9.51 KB
Newer Older
1 2 3 4
/*
 * Waitable timers management
 *
 * Copyright (C) 1999 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23
#include "config.h"
#include "wine/port.h"

24 25 26 27 28
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
29
#include <stdarg.h>
30

31 32
#include "ntstatus.h"
#define WIN32_NO_STATUS
33
#include "windef.h"
34
#include "winternl.h"
35 36

#include "file.h"
37 38 39 40 41 42 43 44
#include "handle.h"
#include "request.h"

struct timer
{
    struct object        obj;       /* object header */
    int                  manual;    /* manual reset */
    int                  signaled;  /* current signaled state */
45
    unsigned int         period;    /* timer period in ms */
46
    abstime_t            when;      /* next expiration */
47
    struct timeout_user *timeout;   /* timeout user */
48
    struct thread       *thread;    /* thread that set the APC function */
49 50
    client_ptr_t         callback;  /* callback APC function */
    client_ptr_t         arg;       /* callback argument */
51 52 53
};

static void timer_dump( struct object *obj, int verbose );
54
static struct object_type *timer_get_type( struct object *obj );
55
static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
56
static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
57
static unsigned int timer_map_access( struct object *obj, unsigned int access );
58 59 60 61
static void timer_destroy( struct object *obj );

static const struct object_ops timer_ops =
{
62 63
    sizeof(struct timer),      /* size */
    timer_dump,                /* dump */
64
    timer_get_type,            /* get_type */
65 66 67 68
    add_queue,                 /* add_queue */
    remove_queue,              /* remove_queue */
    timer_signaled,            /* signaled */
    timer_satisfied,           /* satisfied */
69
    no_signal,                 /* signal */
70
    no_get_fd,                 /* get_fd */
71
    timer_map_access,          /* map_access */
72 73
    default_get_sd,            /* get_sd */
    default_set_sd,            /* set_sd */
74
    no_lookup_name,            /* lookup_name */
75 76
    directory_link_name,       /* link_name */
    default_unlink_name,       /* unlink_name */
77
    no_open_file,              /* open_file */
78
    no_kernel_obj_list,        /* get_kernel_obj_list */
79
    no_close_handle,           /* close_handle */
80
    timer_destroy              /* destroy */
81 82 83 84
};


/* create a timer object */
85
static struct timer *create_timer( struct object *root, const struct unicode_str *name,
86
                                   unsigned int attr, int manual, const struct security_descriptor *sd )
87 88 89
{
    struct timer *timer;

90
    if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
91
    {
92
        if (get_error() != STATUS_OBJECT_NAME_EXISTS)
93 94
        {
            /* initialize it if it didn't already exist */
95 96 97 98 99 100
            timer->manual   = manual;
            timer->signaled = 0;
            timer->when     = 0;
            timer->period   = 0;
            timer->timeout  = NULL;
            timer->thread   = NULL;
101 102 103 104 105 106 107 108 109 110
        }
    }
    return timer;
}

/* callback on timer expiration */
static void timer_callback( void *private )
{
    struct timer *timer = (struct timer *)private;

111 112
    /* queue an APC */
    if (timer->thread)
113
    {
114 115 116 117 118
        apc_call_t data;

        memset( &data, 0, sizeof(data) );
        if (timer->callback)
        {
119 120 121 122
            data.type            = APC_TIMER;
            data.user.timer.func = timer->callback;
            data.user.timer.time = timer->when;
            data.user.timer.arg  = timer->arg;
123 124 125
        }
        else data.type = APC_NONE;  /* wake up only */

126
        if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
127 128 129 130 131
        {
            release_object( timer->thread );
            timer->thread = NULL;
        }
    }
132

133 134
    if (timer->period)  /* schedule the next expiration */
    {
135 136 137
        if (timer->when > 0) timer->when = -monotonic_time;
        timer->when -= (abstime_t)timer->period * 10000;
        timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
138 139 140 141 142 143 144 145
    }
    else timer->timeout = NULL;

    /* wake up waiters */
    timer->signaled = 1;
    wake_up( &timer->obj, 0 );
}

146
/* cancel a running timer */
147
static int cancel_timer( struct timer *timer )
148
{
149 150
    int signaled = timer->signaled;

151 152 153 154 155 156 157
    if (timer->timeout)
    {
        remove_timeout_user( timer->timeout );
        timer->timeout = NULL;
    }
    if (timer->thread)
    {
158
        thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
159
        release_object( timer->thread );
160 161
        timer->thread = NULL;
    }
162
    return signaled;
163 164
}

165
/* set the timer expiration and period */
166
static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
167
                      client_ptr_t callback, client_ptr_t arg )
168
{
169
    int signaled = cancel_timer( timer );
170 171 172 173 174
    if (timer->manual)
    {
        period = 0;  /* period doesn't make any sense for a manual timer */
        timer->signaled = 0;
    }
175
    timer->when     = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
176 177 178
    timer->period   = period;
    timer->callback = callback;
    timer->arg      = arg;
179
    if (callback) timer->thread = (struct thread *)grab_object( current );
180
    timer->timeout = add_timeout_user( expire, timer_callback, timer );
181
    return signaled;
182 183 184 185 186
}

static void timer_dump( struct object *obj, int verbose )
{
    struct timer *timer = (struct timer *)obj;
187
    timeout_t timeout = abstime_to_timeout( timer->when );
188
    assert( obj->ops == &timer_ops );
189
    fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
190
             timer->manual, get_timeout_str(timeout), timer->period );
191 192
}

193 194 195 196 197 198 199
static struct object_type *timer_get_type( struct object *obj )
{
    static const WCHAR name[] = {'T','i','m','e','r'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

200
static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
201 202 203 204 205 206
{
    struct timer *timer = (struct timer *)obj;
    assert( obj->ops == &timer_ops );
    return timer->signaled;
}

207
static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
208 209 210 211 212 213
{
    struct timer *timer = (struct timer *)obj;
    assert( obj->ops == &timer_ops );
    if (!timer->manual) timer->signaled = 0;
}

214 215 216 217 218 219 220 221 222
static unsigned int timer_map_access( struct object *obj, unsigned int access )
{
    if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
    if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
    if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
    if (access & GENERIC_ALL)     access |= TIMER_ALL_ACCESS;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

223 224 225 226 227 228
static void timer_destroy( struct object *obj )
{
    struct timer *timer = (struct timer *)obj;
    assert( obj->ops == &timer_ops );

    if (timer->timeout) remove_timeout_user( timer->timeout );
229
    if (timer->thread) release_object( timer->thread );
230 231 232 233 234 235
}

/* create a timer */
DECL_HANDLER(create_timer)
{
    struct timer *timer;
236
    struct unicode_str name;
237
    struct object *root;
238
    const struct security_descriptor *sd;
239
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
240

241
    if (!objattr) return;
242

243
    if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
244
    {
245
        reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
246 247
        release_object( timer );
    }
248 249

    if (root) release_object( root );
250 251 252 253 254
}

/* open a handle to a timer */
DECL_HANDLER(open_timer)
{
255
    struct unicode_str name = get_req_unicode_str();
256

257 258
    reply->handle = open_object( current->process, req->rootdir, req->access,
                                 &timer_ops, &name, req->attributes );
259 260 261 262 263 264 265 266 267 268
}

/* set a waitable timer */
DECL_HANDLER(set_timer)
{
    struct timer *timer;

    if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
                                                 TIMER_MODIFY_STATE, &timer_ops )))
    {
269
        reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
270 271 272 273 274 275 276 277 278 279 280 281
        release_object( timer );
    }
}

/* cancel a waitable timer */
DECL_HANDLER(cancel_timer)
{
    struct timer *timer;

    if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
                                                 TIMER_MODIFY_STATE, &timer_ops )))
    {
282
        reply->signaled = cancel_timer( timer );
283 284 285
        release_object( timer );
    }
}
286 287 288 289 290 291 292 293 294

/* Get information on a waitable timer */
DECL_HANDLER(get_timer_info)
{
    struct timer *timer;

    if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
                                                 TIMER_QUERY_STATE, &timer_ops )))
    {
295
        reply->when      = timer->when;
296 297 298 299
        reply->signaled  = timer->signaled;
        release_object( timer );
    }
}