quartz: Release objects when they're no longer needed.
[wine.git] / server / event.c
blob47dde24ae759b6268e715badbe23849bacedcf7a
1 /*
2 * Server-side event management
4 * Copyright (C) 1998 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
38 struct event
40 struct object obj; /* object header */
41 int manual_reset; /* is it a manual reset event? */
42 int signaled; /* event has been signaled */
45 static void event_dump( struct object *obj, int verbose );
46 static int event_signaled( struct object *obj, struct thread *thread );
47 static int event_satisfied( struct object *obj, struct thread *thread );
48 static unsigned int event_map_access( struct object *obj, unsigned int access );
49 static int event_signal( struct object *obj, unsigned int access);
51 static const struct object_ops event_ops =
53 sizeof(struct event), /* size */
54 event_dump, /* dump */
55 add_queue, /* add_queue */
56 remove_queue, /* remove_queue */
57 event_signaled, /* signaled */
58 event_satisfied, /* satisfied */
59 event_signal, /* signal */
60 no_get_fd, /* get_fd */
61 event_map_access, /* map_access */
62 no_lookup_name, /* lookup_name */
63 no_close_handle, /* close_handle */
64 no_destroy /* destroy */
68 struct event *create_event( struct directory *root, const struct unicode_str *name,
69 unsigned int attr, int manual_reset, int initial_state )
71 struct event *event;
73 if ((event = create_named_object_dir( root, name, attr, &event_ops )))
75 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
77 /* initialize it if it didn't already exist */
78 event->manual_reset = manual_reset;
79 event->signaled = initial_state;
82 return event;
85 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
87 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
90 void pulse_event( struct event *event )
92 event->signaled = 1;
93 /* wake up all waiters if manual reset, a single one otherwise */
94 wake_up( &event->obj, !event->manual_reset );
95 event->signaled = 0;
98 void set_event( struct event *event )
100 event->signaled = 1;
101 /* wake up all waiters if manual reset, a single one otherwise */
102 wake_up( &event->obj, !event->manual_reset );
105 void reset_event( struct event *event )
107 event->signaled = 0;
110 static void event_dump( struct object *obj, int verbose )
112 struct event *event = (struct event *)obj;
113 assert( obj->ops == &event_ops );
114 fprintf( stderr, "Event manual=%d signaled=%d ",
115 event->manual_reset, event->signaled );
116 dump_object_name( &event->obj );
117 fputc( '\n', stderr );
120 static int event_signaled( struct object *obj, struct thread *thread )
122 struct event *event = (struct event *)obj;
123 assert( obj->ops == &event_ops );
124 return event->signaled;
127 static int event_satisfied( struct object *obj, struct thread *thread )
129 struct event *event = (struct event *)obj;
130 assert( obj->ops == &event_ops );
131 /* Reset if it's an auto-reset event */
132 if (!event->manual_reset) event->signaled = 0;
133 return 0; /* Not abandoned */
136 static unsigned int event_map_access( struct object *obj, unsigned int access )
138 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | EVENT_QUERY_STATE;
139 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
140 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
141 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_ALL_ACCESS;
142 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
145 static int event_signal( struct object *obj, unsigned int access )
147 struct event *event = (struct event *)obj;
148 assert( obj->ops == &event_ops );
150 if (!(access & EVENT_MODIFY_STATE))
152 set_error( STATUS_ACCESS_DENIED );
153 return 0;
155 set_event( event );
156 return 1;
159 /* create an event */
160 DECL_HANDLER(create_event)
162 struct event *event;
163 struct unicode_str name;
164 struct directory *root = NULL;
166 reply->handle = 0;
167 get_req_unicode_str( &name );
168 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
169 return;
171 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state )))
173 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
174 release_object( event );
177 if (root) release_object( root );
180 /* open a handle to an event */
181 DECL_HANDLER(open_event)
183 struct unicode_str name;
184 struct directory *root = NULL;
185 struct event *event;
187 get_req_unicode_str( &name );
188 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
189 return;
191 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
193 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
194 release_object( event );
197 if (root) release_object( root );
200 /* do an event operation */
201 DECL_HANDLER(event_op)
203 struct event *event;
205 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
206 switch(req->op)
208 case PULSE_EVENT:
209 pulse_event( event );
210 break;
211 case SET_EVENT:
212 set_event( event );
213 break;
214 case RESET_EVENT:
215 reset_event( event );
216 break;
217 default:
218 set_error( STATUS_INVALID_PARAMETER );
219 break;
221 release_object( event );