ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / async.c
blob4832d69b7bf21a0a1e31775229e9ada2a91f860a
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 struct timeout_user *timeout;
46 unsigned int timeout_status; /* status to report upon timeout */
47 struct event *event;
48 async_data_t data; /* data for async I/O call */
49 struct iosb *iosb; /* I/O status block */
50 obj_handle_t wait_handle; /* pre-allocated wait handle */
51 unsigned int initial_status; /* status returned from initial request */
52 unsigned int signaled :1;
53 unsigned int pending :1; /* request successfully queued, but pending */
54 unsigned int direct_result :1;/* a flag if we're passing result directly from request instead of APC */
55 unsigned int alerted :1; /* fd is signaled, but we are waiting for client-side I/O */
56 unsigned int terminated :1; /* async has been terminated */
57 unsigned int canceled :1; /* have we already queued cancellation for this async? */
58 unsigned int unknown_status :1; /* initial status is not known yet */
59 unsigned int blocking :1; /* async is blocking */
60 struct completion *completion; /* completion associated with fd */
61 apc_param_t comp_key; /* completion key associated with fd */
62 unsigned int comp_flags; /* completion flags */
63 async_completion_callback completion_callback; /* callback to be called on completion */
64 void *completion_callback_private; /* argument to completion_callback */
67 static void async_dump( struct object *obj, int verbose );
68 static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
69 static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
70 static void async_destroy( struct object *obj );
72 static const struct object_ops async_ops =
74 sizeof(struct async), /* size */
75 &no_type, /* type */
76 async_dump, /* dump */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 async_signaled, /* signaled */
80 async_satisfied, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 default_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_get_full_name, /* get_full_name */
87 no_lookup_name, /* lookup_name */
88 no_link_name, /* link_name */
89 NULL, /* unlink_name */
90 no_open_file, /* open_file */
91 no_kernel_obj_list, /* get_kernel_obj_list */
92 no_close_handle, /* close_handle */
93 async_destroy /* destroy */
96 static inline void async_reselect( struct async *async )
98 if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
101 static void async_dump( struct object *obj, int verbose )
103 struct async *async = (struct async *)obj;
104 assert( obj->ops == &async_ops );
105 fprintf( stderr, "Async thread=%p\n", async->thread );
108 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
110 struct async *async = (struct async *)obj;
111 assert( obj->ops == &async_ops );
112 return async->signaled;
115 static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
117 struct async *async = (struct async *)obj;
118 assert( obj->ops == &async_ops );
120 /* we only return an async handle for asyncs created via create_request_async() */
121 assert( async->iosb );
123 if (async->direct_result)
125 async_set_result( &async->obj, async->iosb->status, async->iosb->result );
126 async->direct_result = 0;
129 if (async->initial_status == STATUS_PENDING && async->blocking)
130 set_wait_status( entry, async->iosb->status );
131 else
132 set_wait_status( entry, async->initial_status );
134 /* close wait handle here to avoid extra server round trip */
135 if (async->wait_handle)
137 close_handle( async->thread->process, async->wait_handle );
138 async->wait_handle = 0;
142 static void async_destroy( struct object *obj )
144 struct async *async = (struct async *)obj;
145 assert( obj->ops == &async_ops );
147 list_remove( &async->process_entry );
149 if (async->queue)
151 list_remove( &async->queue_entry );
152 async_reselect( async );
154 else if (async->fd) release_object( async->fd );
156 if (async->timeout) remove_timeout_user( async->timeout );
157 if (async->completion) release_object( async->completion );
158 if (async->event) release_object( async->event );
159 if (async->iosb) release_object( async->iosb );
160 release_object( async->thread );
163 /* notifies client thread of new status of its async request */
164 void async_terminate( struct async *async, unsigned int status )
166 struct iosb *iosb = async->iosb;
168 if (async->terminated) return;
170 async->terminated = 1;
171 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
172 if (status == STATUS_ALERTED)
173 async->alerted = 1;
175 /* if no APC could be queued (e.g. the process is terminated),
176 * thread_queue_apc() may trigger async_set_result(), which may drop the
177 * last reference to the async, so grab a temporary reference here */
178 grab_object( async );
180 if (!async->direct_result)
182 apc_call_t data;
184 memset( &data, 0, sizeof(data) );
185 data.type = APC_ASYNC_IO;
186 data.async_io.user = async->data.user;
187 data.async_io.result = iosb ? iosb->result : 0;
189 /* this can happen if the initial status was unknown (i.e. for device
190 * files). the client should not fill the IOSB in this case; pass it as
191 * NULL to communicate that.
192 * note that we check the IOSB status and not the initial status */
193 if (NT_ERROR( status ) && (!is_fd_overlapped( async->fd ) || !async->pending))
194 data.async_io.sb = 0;
195 else
196 data.async_io.sb = async->data.iosb;
198 /* if there is output data, the client needs to make an extra request
199 * to retrieve it; use STATUS_ALERTED to signal this case */
200 if (iosb && iosb->out_data)
201 data.async_io.status = STATUS_ALERTED;
202 else
203 data.async_io.status = status;
205 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
208 async_reselect( async );
210 release_object( async );
213 /* callback for timeout on an async request */
214 static void async_timeout( void *private )
216 struct async *async = private;
218 async->timeout = NULL;
219 async_terminate( async, async->timeout_status );
222 /* free an async queue, cancelling all async operations */
223 void free_async_queue( struct async_queue *queue )
225 struct async *async, *next;
227 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
229 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
230 async->fd = NULL;
231 async_terminate( async, STATUS_HANDLES_CLOSED );
232 async->queue = NULL;
233 release_object( &async->obj );
237 void queue_async( struct async_queue *queue, struct async *async )
239 /* fd will be set to NULL in free_async_queue when fd is destroyed */
240 release_object( async->fd );
242 async->queue = queue;
243 grab_object( async );
244 list_add_tail( &queue->queue, &async->queue_entry );
246 set_fd_signaled( async->fd, 0 );
249 /* create an async on a given queue of a fd */
250 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
252 struct event *event = NULL;
253 struct async *async;
255 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
256 return NULL;
258 if (!(async = alloc_object( &async_ops )))
260 if (event) release_object( event );
261 return NULL;
264 async->thread = (struct thread *)grab_object( thread );
265 async->event = event;
266 async->data = *data;
267 async->timeout = NULL;
268 async->queue = NULL;
269 async->fd = (struct fd *)grab_object( fd );
270 async->initial_status = STATUS_PENDING;
271 async->signaled = 0;
272 async->pending = 1;
273 async->wait_handle = 0;
274 async->direct_result = 0;
275 async->alerted = 0;
276 async->terminated = 0;
277 async->canceled = 0;
278 async->unknown_status = 0;
279 async->blocking = !is_fd_overlapped( fd );
280 async->completion = fd_get_completion( fd, &async->comp_key );
281 async->comp_flags = 0;
282 async->completion_callback = NULL;
283 async->completion_callback_private = NULL;
285 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
286 else async->iosb = NULL;
288 list_add_head( &thread->process->asyncs, &async->process_entry );
289 if (event) reset_event( event );
291 if (async->completion && data->apc)
293 release_object( async );
294 set_error( STATUS_INVALID_PARAMETER );
295 return NULL;
298 return async;
301 /* set the initial status of an async whose status was previously unknown
302 * the initial status may be STATUS_PENDING */
303 void async_set_initial_status( struct async *async, unsigned int status )
305 async->initial_status = status;
306 async->unknown_status = 0;
309 void set_async_pending( struct async *async )
311 if (!async->terminated)
312 async->pending = 1;
315 void async_wake_obj( struct async *async )
317 assert( !async->unknown_status );
318 if (!async->blocking)
320 async->signaled = 1;
321 wake_up( &async->obj, 0 );
325 static void async_call_completion_callback( struct async *async )
327 if (async->completion_callback)
328 async->completion_callback( async->completion_callback_private );
329 async->completion_callback = NULL;
332 /* return async object status and wait handle to client */
333 obj_handle_t async_handoff( struct async *async, data_size_t *result, int force_blocking )
335 async->blocking = force_blocking || async->blocking;
337 if (async->unknown_status)
339 /* even the initial status is not known yet */
340 set_error( STATUS_PENDING );
341 return async->wait_handle;
344 if (get_error() == STATUS_ALERTED)
346 /* give the client opportunity to complete synchronously. after the
347 * client performs the I/O, it reports the result back to the server
348 * via the set_async_direct_result request. if it turns out that the
349 * I/O request is not actually immediately satiable, the client may
350 * then choose to re-queue the async by reporting STATUS_PENDING
351 * instead.
353 * since we're deferring the initial I/O (to the client), we mark the
354 * async as having unknown initial status (unknown_status = 1). note
355 * that we don't reuse async_set_unknown_status() here. this is because
356 * the one responsible for performing the I/O is not the device driver,
357 * but instead the client that requested the I/O in the first place.
359 * also, async_set_unknown_status() would set direct_result to zero
360 * forcing APC_ASYNC_IO to fire in async_terminate(), which is not
361 * useful due to subtle semantic differences between synchronous and
362 * asynchronous completion.
364 async->unknown_status = 1;
365 async_terminate( async, STATUS_ALERTED );
366 return async->wait_handle;
369 async->initial_status = get_error();
371 if (!async->pending && NT_ERROR( get_error() ))
373 async->iosb->status = get_error();
374 async_call_completion_callback( async );
376 close_handle( async->thread->process, async->wait_handle );
377 async->wait_handle = 0;
378 return 0;
381 if (get_error() != STATUS_PENDING)
383 /* status and data are already set and returned */
384 async_terminate( async, get_error() );
386 else if (async->iosb->status != STATUS_PENDING)
388 /* result is already available in iosb, return it */
389 if (async->iosb->out_data)
391 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
392 async->iosb->out_data = NULL;
396 if (async->iosb->status != STATUS_PENDING)
398 if (result) *result = async->iosb->result;
399 async->signaled = 1;
401 else
403 async->direct_result = 0;
404 async->pending = 1;
405 if (!async->blocking)
407 close_handle( async->thread->process, async->wait_handle);
408 async->wait_handle = 0;
411 async->initial_status = async->iosb->status;
412 set_error( async->iosb->status );
413 return async->wait_handle;
416 /* complete a request-based async with a pre-allocated buffer */
417 void async_request_complete( struct async *async, unsigned int status, data_size_t result,
418 data_size_t out_size, void *out_data )
420 struct iosb *iosb = async_get_iosb( async );
422 /* the async may have already been canceled */
423 if (iosb->status != STATUS_PENDING)
425 release_object( iosb );
426 free( out_data );
427 return;
430 iosb->status = status;
431 iosb->result = result;
432 iosb->out_data = out_data;
433 iosb->out_size = out_size;
435 release_object( iosb );
437 async_terminate( async, status );
440 /* complete a request-based async */
441 void async_request_complete_alloc( struct async *async, unsigned int status, data_size_t result,
442 data_size_t out_size, const void *out_data )
444 void *out_data_copy = NULL;
446 if (out_size && !(out_data_copy = memdup( out_data, out_size )))
448 async_terminate( async, STATUS_NO_MEMORY );
449 return;
452 async_request_complete( async, status, result, out_size, out_data_copy );
455 /* mark an async as having unknown initial status */
456 void async_set_unknown_status( struct async *async )
458 async->unknown_status = 1;
459 async->direct_result = 0;
462 /* set the timeout of an async operation */
463 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
465 if (async->timeout) remove_timeout_user( async->timeout );
466 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
467 else async->timeout = NULL;
468 async->timeout_status = status;
471 /* set a callback to be notified when the async is completed */
472 void async_set_completion_callback( struct async *async, async_completion_callback func, void *private )
474 async->completion_callback = func;
475 async->completion_callback_private = private;
478 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
479 apc_param_t information )
481 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
482 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
485 /* store the result of the client-side async callback */
486 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
488 struct async *async = (struct async *)obj;
490 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
492 assert( async->terminated ); /* it must have been woken up if we get a result */
494 if (async->unknown_status) async_set_initial_status( async, status );
496 if (async->alerted && status == STATUS_PENDING) /* restart it */
498 async->terminated = 0;
499 async->alerted = 0;
500 async_reselect( async );
502 else
504 if (async->timeout) remove_timeout_user( async->timeout );
505 async->timeout = NULL;
506 async->terminated = 1;
507 if (async->iosb) async->iosb->status = status;
509 /* don't signal completion if the async failed synchronously
510 * this can happen if the initial status was unknown (i.e. for device files)
511 * note that we check the IOSB status here, not the initial status */
512 if (async->pending || !NT_ERROR( status ))
514 if (async->data.apc)
516 apc_call_t data;
517 memset( &data, 0, sizeof(data) );
518 data.type = APC_USER;
519 data.user.func = async->data.apc;
520 data.user.args[0] = async->data.apc_context;
521 data.user.args[1] = async->data.iosb;
522 data.user.args[2] = 0;
523 thread_queue_apc( NULL, async->thread, NULL, &data );
525 else if (async->data.apc_context && (async->pending ||
526 !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
528 add_async_completion( async, async->data.apc_context, status, total );
531 if (async->event) set_event( async->event );
532 else if (async->fd) set_fd_signaled( async->fd, 1 );
535 if (!async->signaled)
537 async->signaled = 1;
538 wake_up( &async->obj, 0 );
541 async_call_completion_callback( async );
543 if (async->queue)
545 list_remove( &async->queue_entry );
546 async_reselect( async );
547 async->fd = NULL;
548 async->queue = NULL;
549 release_object( async );
554 /* check if an async operation is waiting to be alerted */
555 int async_waiting( struct async_queue *queue )
557 struct list *ptr;
558 struct async *async;
560 if (!(ptr = list_head( &queue->queue ))) return 0;
561 async = LIST_ENTRY( ptr, struct async, queue_entry );
562 return !async->terminated;
565 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
567 struct async *async;
568 int woken = 0;
570 /* FIXME: it would probably be nice to replace the "canceled" flag with a
571 * single LIST_FOR_EACH_ENTRY_SAFE, but currently cancelling an async can
572 * cause other asyncs to be removed via async_reselect() */
574 restart:
575 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
577 if (async->terminated || async->canceled) continue;
578 if ((!obj || (get_fd_user( async->fd ) == obj)) &&
579 (!thread || async->thread == thread) &&
580 (!iosb || async->data.iosb == iosb))
582 async->canceled = 1;
583 fd_cancel_async( async->fd, async );
584 woken++;
585 goto restart;
588 return woken;
591 void cancel_process_asyncs( struct process *process )
593 cancel_async( process, NULL, NULL, 0 );
596 void cancel_terminating_thread_asyncs( struct thread *thread )
598 struct async *async;
600 restart:
601 LIST_FOR_EACH_ENTRY( async, &thread->process->asyncs, struct async, process_entry )
603 if (async->thread != thread || async->terminated || async->canceled) continue;
604 if (async->completion && async->data.apc_context && !async->event) continue;
606 async->canceled = 1;
607 fd_cancel_async( async->fd, async );
608 goto restart;
612 /* wake up async operations on the queue */
613 void async_wake_up( struct async_queue *queue, unsigned int status )
615 struct list *ptr, *next;
617 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
619 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
620 async_terminate( async, status );
621 if (status == STATUS_ALERTED) break; /* only wake up the first one */
625 static void iosb_dump( struct object *obj, int verbose );
626 static void iosb_destroy( struct object *obj );
628 static const struct object_ops iosb_ops =
630 sizeof(struct iosb), /* size */
631 &no_type, /* type */
632 iosb_dump, /* dump */
633 no_add_queue, /* add_queue */
634 NULL, /* remove_queue */
635 NULL, /* signaled */
636 NULL, /* satisfied */
637 no_signal, /* signal */
638 no_get_fd, /* get_fd */
639 default_map_access, /* map_access */
640 default_get_sd, /* get_sd */
641 default_set_sd, /* set_sd */
642 no_get_full_name, /* get_full_name */
643 no_lookup_name, /* lookup_name */
644 no_link_name, /* link_name */
645 NULL, /* unlink_name */
646 no_open_file, /* open_file */
647 no_kernel_obj_list, /* get_kernel_obj_list */
648 no_close_handle, /* close_handle */
649 iosb_destroy /* destroy */
652 static void iosb_dump( struct object *obj, int verbose )
654 assert( obj->ops == &iosb_ops );
655 fprintf( stderr, "I/O status block\n" );
658 static void iosb_destroy( struct object *obj )
660 struct iosb *iosb = (struct iosb *)obj;
662 free( iosb->in_data );
663 free( iosb->out_data );
666 /* allocate iosb struct */
667 static struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
669 struct iosb *iosb;
671 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
673 iosb->status = STATUS_PENDING;
674 iosb->result = 0;
675 iosb->in_size = in_size;
676 iosb->in_data = NULL;
677 iosb->out_size = out_size;
678 iosb->out_data = NULL;
680 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
682 release_object( iosb );
683 iosb = NULL;
686 return iosb;
689 /* create an async associated with iosb for async-based requests
690 * returned async must be passed to async_handoff */
691 struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
693 struct async *async;
694 struct iosb *iosb;
696 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
697 return NULL;
699 async = create_async( fd, current, data, iosb );
700 release_object( iosb );
701 if (async)
703 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
705 release_object( async );
706 return NULL;
708 async->pending = 0;
709 async->direct_result = 1;
710 async->comp_flags = comp_flags;
712 return async;
715 struct iosb *async_get_iosb( struct async *async )
717 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
720 struct thread *async_get_thread( struct async *async )
722 return async->thread;
725 /* find the first pending async in queue */
726 struct async *find_pending_async( struct async_queue *queue )
728 struct async *async;
729 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
730 if (!async->terminated) return (struct async *)grab_object( async );
731 return NULL;
734 /* cancels all async I/O */
735 DECL_HANDLER(cancel_async)
737 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
738 struct thread *thread = req->only_thread ? current : NULL;
740 if (obj)
742 int count = cancel_async( current->process, obj, thread, req->iosb );
743 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
744 release_object( obj );
748 /* get async result from associated iosb */
749 DECL_HANDLER(get_async_result)
751 struct iosb *iosb = NULL;
752 struct async *async;
754 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
755 if (async->data.user == req->user_arg)
757 iosb = async->iosb;
758 break;
761 if (!iosb)
763 set_error( STATUS_INVALID_PARAMETER );
764 return;
767 if (iosb->out_data)
769 data_size_t size = min( iosb->out_size, get_reply_max_size() );
770 if (size)
772 set_reply_data_ptr( iosb->out_data, size );
773 iosb->out_data = NULL;
776 set_error( iosb->status );
779 /* notify direct completion of async and close the wait handle if not blocking */
780 DECL_HANDLER(set_async_direct_result)
782 struct async *async = (struct async *)get_handle_obj( current->process, req->handle, 0, &async_ops );
783 unsigned int status = req->status;
785 if (!async) return;
787 if (!async->unknown_status || !async->terminated || !async->alerted)
789 set_error( STATUS_INVALID_PARAMETER );
790 release_object( &async->obj );
791 return;
794 if (status == STATUS_PENDING)
796 async->direct_result = 0;
797 async->pending = 1;
799 else if (req->mark_pending)
801 async->pending = 1;
804 /* if the I/O has completed successfully (or unsuccessfully, and
805 * async->pending is set), the client would have already set the IOSB.
806 * therefore, we can do async_set_result() directly and let the client skip
807 * waiting on wait_handle.
809 async_set_result( &async->obj, status, req->information );
811 /* close wait handle here to avoid extra server round trip, if the I/O
812 * either has completed, or is pending and not blocking.
814 if (status != STATUS_PENDING || !async->blocking)
816 close_handle( async->thread->process, async->wait_handle );
817 async->wait_handle = 0;
820 /* report back to the client whether the wait handle has been closed.
821 * handle will be 0 if closed by us; otherwise the original value is
822 * retained
824 reply->handle = async->wait_handle;
826 release_object( &async->obj );