oleaut32: Finish COM cleanup for ITypeInfo objects.
[wine.git] / server / async.c
blobccddec76a6a81aca66e297ed31e6fa84f8fa4ca6
1 /*
2 * Server-side async I/O support
4 * Copyright (C) 2007 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 <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
31 #include "object.h"
32 #include "file.h"
33 #include "request.h"
35 struct async
37 struct object obj; /* object header */
38 struct thread *thread; /* owning thread */
39 struct list queue_entry; /* entry in async queue list */
40 struct async_queue *queue; /* queue containing this async */
41 unsigned int status; /* current status */
42 struct timeout_user *timeout;
43 unsigned int timeout_status; /* status to report upon timeout */
44 struct event *event;
45 async_data_t data; /* data for async I/O call */
48 static void async_dump( struct object *obj, int verbose );
49 static void async_destroy( struct object *obj );
51 static const struct object_ops async_ops =
53 sizeof(struct async), /* size */
54 async_dump, /* dump */
55 no_get_type, /* get_type */
56 no_add_queue, /* add_queue */
57 NULL, /* remove_queue */
58 NULL, /* signaled */
59 NULL, /* satisfied */
60 no_signal, /* signal */
61 no_get_fd, /* get_fd */
62 no_map_access, /* map_access */
63 default_get_sd, /* get_sd */
64 default_set_sd, /* set_sd */
65 no_lookup_name, /* lookup_name */
66 no_open_file, /* open_file */
67 no_close_handle, /* close_handle */
68 async_destroy /* destroy */
72 struct async_queue
74 struct object obj; /* object header */
75 struct fd *fd; /* file descriptor owning this queue */
76 struct completion *completion; /* completion associated with a recently closed file descriptor */
77 apc_param_t comp_key; /* completion key associated with a recently closed file descriptor */
78 struct list queue; /* queue of async objects */
81 static void async_queue_dump( struct object *obj, int verbose );
82 static void async_queue_destroy( struct object *obj );
84 static const struct object_ops async_queue_ops =
86 sizeof(struct async_queue), /* size */
87 async_queue_dump, /* dump */
88 no_get_type, /* get_type */
89 no_add_queue, /* add_queue */
90 NULL, /* remove_queue */
91 NULL, /* signaled */
92 NULL, /* satisfied */
93 no_signal, /* signal */
94 no_get_fd, /* get_fd */
95 no_map_access, /* map_access */
96 default_get_sd, /* get_sd */
97 default_set_sd, /* set_sd */
98 no_lookup_name, /* lookup_name */
99 no_open_file, /* open_file */
100 no_close_handle, /* close_handle */
101 async_queue_destroy /* destroy */
105 static inline void async_reselect( struct async *async )
107 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
110 static void async_dump( struct object *obj, int verbose )
112 struct async *async = (struct async *)obj;
113 assert( obj->ops == &async_ops );
114 fprintf( stderr, "Async thread=%p\n", async->thread );
117 static void async_destroy( struct object *obj )
119 struct async *async = (struct async *)obj;
120 assert( obj->ops == &async_ops );
122 list_remove( &async->queue_entry );
123 async_reselect( async );
125 if (async->timeout) remove_timeout_user( async->timeout );
126 if (async->event) release_object( async->event );
127 release_object( async->queue );
128 release_object( async->thread );
131 static void async_queue_dump( struct object *obj, int verbose )
133 struct async_queue *async_queue = (struct async_queue *)obj;
134 assert( obj->ops == &async_queue_ops );
135 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
138 static void async_queue_destroy( struct object *obj )
140 struct async_queue *async_queue = (struct async_queue *)obj;
141 assert( obj->ops == &async_queue_ops );
142 if (async_queue->completion) release_object( async_queue->completion );
145 /* notifies client thread of new status of its async request */
146 void async_terminate( struct async *async, unsigned int status )
148 apc_call_t data;
150 assert( status != STATUS_PENDING );
152 if (async->status != STATUS_PENDING)
154 /* already terminated, just update status */
155 async->status = status;
156 return;
159 memset( &data, 0, sizeof(data) );
160 data.type = APC_ASYNC_IO;
161 data.async_io.func = async->data.callback;
162 data.async_io.user = async->data.arg;
163 data.async_io.sb = async->data.iosb;
164 data.async_io.status = status;
165 thread_queue_apc( async->thread, &async->obj, &data );
166 async->status = status;
167 async_reselect( async );
168 release_object( async ); /* so that it gets destroyed when the async is done */
171 /* callback for timeout on an async request */
172 static void async_timeout( void *private )
174 struct async *async = private;
176 async->timeout = NULL;
177 async_terminate( async, async->timeout_status );
180 /* create a new async queue for a given fd */
181 struct async_queue *create_async_queue( struct fd *fd )
183 struct async_queue *queue = alloc_object( &async_queue_ops );
185 if (queue)
187 queue->fd = fd;
188 queue->completion = NULL;
189 list_init( &queue->queue );
191 return queue;
194 /* free an async queue, cancelling all async operations */
195 void free_async_queue( struct async_queue *queue )
197 if (!queue) return;
198 if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
199 queue->fd = NULL;
200 async_wake_up( queue, STATUS_HANDLES_CLOSED );
201 release_object( queue );
204 /* create an async on a given queue of a fd */
205 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
207 struct event *event = NULL;
208 struct async *async;
210 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
211 return NULL;
213 if (!(async = alloc_object( &async_ops )))
215 if (event) release_object( event );
216 return NULL;
219 async->thread = (struct thread *)grab_object( thread );
220 async->event = event;
221 async->status = STATUS_PENDING;
222 async->data = *data;
223 async->timeout = NULL;
224 async->queue = (struct async_queue *)grab_object( queue );
226 list_add_tail( &queue->queue, &async->queue_entry );
227 grab_object( async );
229 if (queue->fd) set_fd_signaled( queue->fd, 0 );
230 if (event) reset_event( event );
231 return async;
234 /* set the timeout of an async operation */
235 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
237 if (async->timeout) remove_timeout_user( async->timeout );
238 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
239 else async->timeout = NULL;
240 async->timeout_status = status;
243 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
244 unsigned int information )
246 if (status == STATUS_MORE_PROCESSING_REQUIRED)
247 return; /* The async callback has successfully finished but no completion should be reported */
249 if (queue->fd)
251 apc_param_t ckey;
252 struct completion *completion = fd_get_completion( queue->fd, &ckey );
254 if (completion)
256 add_completion( completion, ckey, cvalue, status, information );
257 release_object( completion );
260 else if (queue->completion) add_completion( queue->completion, queue->comp_key,
261 cvalue, status, information );
264 /* store the result of the client-side async callback */
265 void async_set_result( struct object *obj, unsigned int status, unsigned int total, client_ptr_t apc )
267 struct async *async = (struct async *)obj;
269 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
271 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
273 if (status == STATUS_PENDING) /* restart it */
275 status = async->status;
276 async->status = STATUS_PENDING;
277 grab_object( async );
279 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
280 async_terminate( async, status );
281 else
282 async_reselect( async );
284 else
286 if (async->timeout) remove_timeout_user( async->timeout );
287 async->timeout = NULL;
288 async->status = status;
289 if (async->data.cvalue) add_async_completion( async->queue, async->data.cvalue, status, total );
290 if (apc)
292 apc_call_t data;
293 memset( &data, 0, sizeof(data) );
294 data.type = APC_USER;
295 data.user.func = apc;
296 data.user.args[0] = async->data.arg;
297 data.user.args[1] = async->data.iosb;
298 data.user.args[2] = 0;
299 thread_queue_apc( async->thread, NULL, &data );
301 if (async->event) set_event( async->event );
302 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
306 /* check if there are any queued async operations */
307 int async_queued( struct async_queue *queue )
309 return queue && list_head( &queue->queue );
312 /* check if an async operation is waiting to be alerted */
313 int async_waiting( struct async_queue *queue )
315 struct list *ptr;
316 struct async *async;
318 if (!queue) return 0;
319 if (!(ptr = list_head( &queue->queue ))) return 0;
320 async = LIST_ENTRY( ptr, struct async, queue_entry );
321 return async->status == STATUS_PENDING;
324 int async_wake_up_by( struct async_queue *queue, struct process *process,
325 struct thread *thread, client_ptr_t iosb, unsigned int status )
327 struct list *ptr, *next;
328 int woken = 0;
330 if (!queue || (!process && !thread && !iosb)) return 0;
332 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
334 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
335 if ( (!process || async->thread->process == process) &&
336 (!thread || async->thread == thread) &&
337 (!iosb || async->data.iosb == iosb) )
339 async_terminate( async, status );
340 woken++;
343 return woken;
346 /* wake up async operations on the queue */
347 void async_wake_up( struct async_queue *queue, unsigned int status )
349 struct list *ptr, *next;
351 if (!queue) return;
353 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
355 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
356 async_terminate( async, status );
357 if (status == STATUS_ALERTED) break; /* only wake up the first one */