event.c 4.82 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Server-side event management
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
24 25 26 27
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

28
#include "windef.h"
29 30 31

#include "handle.h"
#include "thread.h"
32
#include "request.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33 34 35 36 37 38 39 40

struct event
{
    struct object  obj;             /* object header */
    int            manual_reset;    /* is it a manual reset event? */
    int            signaled;        /* event has been signaled */
};

41
static void event_dump( struct object *obj, int verbose );
Alexandre Julliard's avatar
Alexandre Julliard committed
42 43 44 45 46
static int event_signaled( struct object *obj, struct thread *thread );
static int event_satisfied( struct object *obj, struct thread *thread );

static const struct object_ops event_ops =
{
47 48 49 50 51 52
    sizeof(struct event),      /* size */
    event_dump,                /* dump */
    add_queue,                 /* add_queue */
    remove_queue,              /* remove_queue */
    event_signaled,            /* signaled */
    event_satisfied,           /* satisfied */
53
    no_get_fd,                 /* get_fd */
54
    no_destroy                 /* destroy */
Alexandre Julliard's avatar
Alexandre Julliard committed
55 56 57
};


58 59
struct event *create_event( const WCHAR *name, size_t len,
                            int manual_reset, int initial_state )
Alexandre Julliard's avatar
Alexandre Julliard committed
60 61 62
{
    struct event *event;

63
    if ((event = create_named_object( sync_namespace, &event_ops, name, len )))
Alexandre Julliard's avatar
Alexandre Julliard committed
64
    {
65
        if (get_error() != STATUS_OBJECT_NAME_COLLISION)
66 67 68 69 70
        {
            /* initialize it if it didn't already exist */
            event->manual_reset = manual_reset;
            event->signaled     = initial_state;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
71
    }
72
    return event;
Alexandre Julliard's avatar
Alexandre Julliard committed
73 74
}

75
struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard's avatar
Alexandre Julliard committed
76
{
77
    return (struct event *)get_handle_obj( process, handle, access, &event_ops );
78
}
Alexandre Julliard's avatar
Alexandre Julliard committed
79

80 81
void pulse_event( struct event *event )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
82 83 84 85 86 87
    event->signaled = 1;
    /* wake up all waiters if manual reset, a single one otherwise */
    wake_up( &event->obj, !event->manual_reset );
    event->signaled = 0;
}

88
void set_event( struct event *event )
Alexandre Julliard's avatar
Alexandre Julliard committed
89 90 91 92 93 94
{
    event->signaled = 1;
    /* wake up all waiters if manual reset, a single one otherwise */
    wake_up( &event->obj, !event->manual_reset );
}

95
void reset_event( struct event *event )
Alexandre Julliard's avatar
Alexandre Julliard committed
96 97 98 99
{
    event->signaled = 0;
}

100
static void event_dump( struct object *obj, int verbose )
Alexandre Julliard's avatar
Alexandre Julliard committed
101 102 103
{
    struct event *event = (struct event *)obj;
    assert( obj->ops == &event_ops );
104 105 106 107
    fprintf( stderr, "Event manual=%d signaled=%d ",
             event->manual_reset, event->signaled );
    dump_object_name( &event->obj );
    fputc( '\n', stderr );
Alexandre Julliard's avatar
Alexandre Julliard committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

static int event_signaled( struct object *obj, struct thread *thread )
{
    struct event *event = (struct event *)obj;
    assert( obj->ops == &event_ops );
    return event->signaled;
}

static int event_satisfied( struct object *obj, struct thread *thread )
{
    struct event *event = (struct event *)obj;
    assert( obj->ops == &event_ops );
    /* Reset if it's an auto-reset event */
    if (!event->manual_reset) event->signaled = 0;
    return 0;  /* Not abandoned */
}

126 127 128
/* create an event */
DECL_HANDLER(create_event)
{
129 130
    struct event *event;

131 132
    reply->handle = 0;
    if ((event = create_event( get_req_data(), get_req_data_size(),
133
                               req->manual_reset, req->initial_state )))
134
    {
135
        reply->handle = alloc_handle( current->process, event, EVENT_ALL_ACCESS, req->inherit );
136
        release_object( event );
137 138 139 140 141 142
    }
}

/* open a handle to an event */
DECL_HANDLER(open_event)
{
143
    reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
144
                                 &event_ops, req->access, req->inherit );
145 146 147 148 149
}

/* do an event operation */
DECL_HANDLER(event_op)
{
150 151 152
    struct event *event;

    if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
153 154 155
    switch(req->op)
    {
    case PULSE_EVENT:
156
        pulse_event( event );
157 158
        break;
    case SET_EVENT:
159
        set_event( event );
160 161
        break;
    case RESET_EVENT:
162
        reset_event( event );
163 164
        break;
    default:
165
        fatal_protocol_error( current, "event_op: invalid operation %d\n", req->op );
166
    }
167
    release_object( event );
168
}