user32/tests: Remove Win9x workarounds.
[wine.git] / server / async.c
blobadbadc5a17f640aed934a3e3ff486e0850787269
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"
34 #include "process.h"
35 #include "handle.h"
37 struct async
39 struct object obj; /* object header */
40 struct thread *thread; /* owning thread */
41 struct list queue_entry; /* entry in async queue list */
42 struct list process_entry; /* entry in process list */
43 struct async_queue *queue; /* queue containing this async */
44 struct fd *fd; /* fd associated with an unqueued async */
45 unsigned int status; /* current status */
46 struct timeout_user *timeout;
47 unsigned int timeout_status; /* status to report upon timeout */
48 int signaled;
49 struct event *event;
50 async_data_t data; /* data for async I/O call */
51 struct iosb *iosb; /* I/O status block */
52 obj_handle_t wait_handle; /* pre-allocated wait handle */
53 int direct_result; /* a flag if we're passing result directly from request instead of APC */
54 struct completion *completion; /* completion associated with fd */
55 apc_param_t comp_key; /* completion key associated with fd */
58 static void async_dump( struct object *obj, int verbose );
59 static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
60 static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
61 static void async_destroy( struct object *obj );
63 static const struct object_ops async_ops =
65 sizeof(struct async), /* size */
66 async_dump, /* dump */
67 no_get_type, /* get_type */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 async_signaled, /* signaled */
71 async_satisfied, /* satisfied */
72 no_signal, /* signal */
73 no_get_fd, /* get_fd */
74 no_map_access, /* map_access */
75 default_get_sd, /* get_sd */
76 default_set_sd, /* set_sd */
77 no_lookup_name, /* lookup_name */
78 no_link_name, /* link_name */
79 NULL, /* unlink_name */
80 no_open_file, /* open_file */
81 no_close_handle, /* close_handle */
82 async_destroy /* destroy */
85 static inline void async_reselect( struct async *async )
87 if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
90 static void async_dump( struct object *obj, int verbose )
92 struct async *async = (struct async *)obj;
93 assert( obj->ops == &async_ops );
94 fprintf( stderr, "Async thread=%p\n", async->thread );
97 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
99 struct async *async = (struct async *)obj;
100 assert( obj->ops == &async_ops );
101 return async->signaled;
104 static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
106 struct async *async = (struct async *)obj;
107 assert( obj->ops == &async_ops );
109 if (async->direct_result)
111 async_set_result( &async->obj, async->iosb->status, async->iosb->result );
112 async->direct_result = 0;
115 /* close wait handle here to avoid extra server round trip */
116 if (async->wait_handle)
118 close_handle( async->thread->process, async->wait_handle );
119 async->wait_handle = 0;
123 static void async_destroy( struct object *obj )
125 struct async *async = (struct async *)obj;
126 assert( obj->ops == &async_ops );
128 list_remove( &async->process_entry );
130 if (async->queue)
132 list_remove( &async->queue_entry );
133 async_reselect( async );
135 else if (async->fd) release_object( async->fd );
137 if (async->timeout) remove_timeout_user( async->timeout );
138 if (async->completion) release_object( async->completion );
139 if (async->event) release_object( async->event );
140 if (async->iosb) release_object( async->iosb );
141 release_object( async->thread );
144 /* notifies client thread of new status of its async request */
145 void async_terminate( struct async *async, unsigned int status )
147 assert( status != STATUS_PENDING );
149 if (async->status != STATUS_PENDING)
151 /* already terminated, just update status */
152 async->status = status;
153 return;
156 async->status = status;
157 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
159 if (!async->direct_result)
161 if (async->data.user)
163 apc_call_t data;
165 memset( &data, 0, sizeof(data) );
166 data.type = APC_ASYNC_IO;
167 data.async_io.user = async->data.user;
168 data.async_io.sb = async->data.iosb;
169 data.async_io.status = status;
170 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
172 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
175 async_reselect( async );
176 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
179 /* callback for timeout on an async request */
180 static void async_timeout( void *private )
182 struct async *async = private;
184 async->timeout = NULL;
185 async_terminate( async, async->timeout_status );
188 /* free an async queue, cancelling all async operations */
189 void free_async_queue( struct async_queue *queue )
191 struct async *async, *next;
193 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
195 grab_object( &async->obj );
196 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
197 async->fd = NULL;
198 async_terminate( async, STATUS_HANDLES_CLOSED );
199 async->queue = NULL;
200 release_object( &async->obj );
204 void queue_async( struct async_queue *queue, struct async *async )
206 /* fd will be set to NULL in free_async_queue when fd is destroyed */
207 release_object( async->fd );
209 async->queue = queue;
210 grab_object( async );
211 list_add_tail( &queue->queue, &async->queue_entry );
213 set_fd_signaled( async->fd, 0 );
216 /* create an async on a given queue of a fd */
217 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
219 struct event *event = NULL;
220 struct async *async;
222 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
223 return NULL;
225 if (!(async = alloc_object( &async_ops )))
227 if (event) release_object( event );
228 return NULL;
231 async->thread = (struct thread *)grab_object( thread );
232 async->event = event;
233 async->status = STATUS_PENDING;
234 async->data = *data;
235 async->timeout = NULL;
236 async->queue = NULL;
237 async->fd = (struct fd *)grab_object( fd );
238 async->signaled = 0;
239 async->wait_handle = 0;
240 async->direct_result = 0;
241 async->completion = fd_get_completion( fd, &async->comp_key );
243 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
244 else async->iosb = NULL;
246 list_add_head( &thread->process->asyncs, &async->process_entry );
247 if (event) reset_event( event );
249 if (async->completion && data->apc)
251 release_object( async );
252 set_error( STATUS_INVALID_PARAMETER );
253 return NULL;
256 return async;
259 /* create an async associated with iosb for async-based requests
260 * returned async must be passed to async_handoff */
261 struct async *create_request_async( struct fd *fd, const async_data_t *data )
263 struct async *async;
264 struct iosb *iosb;
266 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
267 return NULL;
269 async = create_async( fd, current, data, iosb );
270 release_object( iosb );
271 if (async)
273 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
275 release_object( async );
276 return NULL;
278 async->direct_result = 1;
280 return async;
283 /* return async object status and wait handle to client */
284 obj_handle_t async_handoff( struct async *async, int success, data_size_t *result )
286 if (!success)
288 close_handle( async->thread->process, async->wait_handle );
289 async->wait_handle = 0;
290 return 0;
293 if (get_error() != STATUS_PENDING)
295 /* status and data are already set and returned */
296 async_terminate( async, get_error() );
298 else if (async->iosb->status != STATUS_PENDING)
300 /* result is already available in iosb, return it */
301 if (async->iosb->out_data)
303 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
304 async->iosb->out_data = NULL;
308 if (async->iosb->status != STATUS_PENDING)
310 if (result) *result = async->iosb->result;
311 async->signaled = 1;
313 else
315 async->direct_result = 0;
316 if (!async_is_blocking( async ))
318 close_handle( async->thread->process, async->wait_handle);
319 async->wait_handle = 0;
322 set_error( async->iosb->status );
323 return async->wait_handle;
326 /* set the timeout of an async operation */
327 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
329 if (async->timeout) remove_timeout_user( async->timeout );
330 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
331 else async->timeout = NULL;
332 async->timeout_status = status;
335 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
336 apc_param_t information )
338 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
339 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
342 /* store the result of the client-side async callback */
343 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
345 struct async *async = (struct async *)obj;
347 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
349 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
351 if (status == STATUS_PENDING) /* restart it */
353 status = async->status;
354 async->status = STATUS_PENDING;
355 grab_object( async );
357 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
358 async_terminate( async, status );
359 else
360 async_reselect( async );
362 else
364 if (async->timeout) remove_timeout_user( async->timeout );
365 async->timeout = NULL;
366 async->status = status;
367 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
369 if (async->data.apc)
371 apc_call_t data;
372 memset( &data, 0, sizeof(data) );
373 data.type = APC_USER;
374 data.user.func = async->data.apc;
375 data.user.args[0] = async->data.apc_context;
376 data.user.args[1] = async->data.iosb;
377 data.user.args[2] = 0;
378 thread_queue_apc( NULL, async->thread, NULL, &data );
380 else if (async->data.apc_context)
381 add_async_completion( async, async->data.apc_context, status, total );
383 if (async->event) set_event( async->event );
384 else if (async->fd) set_fd_signaled( async->fd, 1 );
385 if (!async->signaled)
387 async->signaled = 1;
388 wake_up( &async->obj, 0 );
393 /* check if an async operation is waiting to be alerted */
394 int async_waiting( struct async_queue *queue )
396 struct list *ptr;
397 struct async *async;
399 if (!(ptr = list_head( &queue->queue ))) return 0;
400 async = LIST_ENTRY( ptr, struct async, queue_entry );
401 return async->status == STATUS_PENDING;
404 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
406 struct async *async;
407 int woken = 0;
409 restart:
410 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
412 if (async->status == STATUS_CANCELLED) continue;
413 if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
414 (!thread || async->thread == thread) &&
415 (!iosb || async->data.iosb == iosb))
417 async_terminate( async, STATUS_CANCELLED );
418 woken++;
419 goto restart;
422 return woken;
425 void cancel_process_asyncs( struct process *process )
427 cancel_async( process, NULL, NULL, 0 );
430 /* wake up async operations on the queue */
431 void async_wake_up( struct async_queue *queue, unsigned int status )
433 struct list *ptr, *next;
435 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
437 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
438 async_terminate( async, status );
439 if (status == STATUS_ALERTED) break; /* only wake up the first one */
443 static void iosb_dump( struct object *obj, int verbose );
444 static void iosb_destroy( struct object *obj );
446 static const struct object_ops iosb_ops =
448 sizeof(struct iosb), /* size */
449 iosb_dump, /* dump */
450 no_get_type, /* get_type */
451 no_add_queue, /* add_queue */
452 NULL, /* remove_queue */
453 NULL, /* signaled */
454 NULL, /* satisfied */
455 no_signal, /* signal */
456 no_get_fd, /* get_fd */
457 no_map_access, /* map_access */
458 default_get_sd, /* get_sd */
459 default_set_sd, /* set_sd */
460 no_lookup_name, /* lookup_name */
461 no_link_name, /* link_name */
462 NULL, /* unlink_name */
463 no_open_file, /* open_file */
464 no_close_handle, /* close_handle */
465 iosb_destroy /* destroy */
468 static void iosb_dump( struct object *obj, int verbose )
470 assert( obj->ops == &iosb_ops );
471 fprintf( stderr, "I/O status block\n" );
474 static void iosb_destroy( struct object *obj )
476 struct iosb *iosb = (struct iosb *)obj;
478 free( iosb->in_data );
479 free( iosb->out_data );
482 /* allocate iosb struct */
483 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
485 struct iosb *iosb;
487 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
489 iosb->status = STATUS_PENDING;
490 iosb->result = 0;
491 iosb->in_size = in_size;
492 iosb->in_data = NULL;
493 iosb->out_size = out_size;
494 iosb->out_data = NULL;
496 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
498 release_object( iosb );
499 iosb = NULL;
502 return iosb;
505 struct iosb *async_get_iosb( struct async *async )
507 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
510 int async_is_blocking( struct async *async )
512 return !async->event && !async->data.apc && !async->data.apc_context;
515 /* find the first pending async in queue */
516 struct async *find_pending_async( struct async_queue *queue )
518 struct async *async;
519 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
520 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
521 return NULL;
524 /* cancels all async I/O */
525 DECL_HANDLER(cancel_async)
527 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
528 struct thread *thread = req->only_thread ? current : NULL;
530 if (obj)
532 int count = cancel_async( current->process, obj, thread, req->iosb );
533 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
534 release_object( obj );
538 /* get async result from associated iosb */
539 DECL_HANDLER(get_async_result)
541 struct iosb *iosb = NULL;
542 struct async *async;
544 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
545 if (async->data.user == req->user_arg)
547 iosb = async->iosb;
548 break;
551 if (!iosb)
553 set_error( STATUS_INVALID_PARAMETER );
554 return;
557 if (iosb->out_data)
559 data_size_t size = min( iosb->out_size, get_reply_max_size() );
560 if (size)
562 set_reply_data_ptr( iosb->out_data, size );
563 iosb->out_data = NULL;
566 reply->size = iosb->result;
567 set_error( iosb->status );