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
27 #define WIN32_NO_STATUS
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 */
49 async_data_t data
; /* data for async I/O call */
50 struct iosb
*iosb
; /* I/O status block */
51 obj_handle_t wait_handle
; /* pre-allocated wait handle */
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 struct completion
*completion
; /* completion associated with fd */
56 apc_param_t comp_key
; /* completion key associated with fd */
57 unsigned int comp_flags
; /* completion flags */
58 async_completion_callback completion_callback
; /* callback to be called on completion */
59 void *completion_callback_private
; /* argument to completion_callback */
62 static void async_dump( struct object
*obj
, int verbose
);
63 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
64 static void async_satisfied( struct object
* obj
, struct wait_queue_entry
*entry
);
65 static void async_destroy( struct object
*obj
);
67 static const struct object_ops async_ops
=
69 sizeof(struct async
), /* size */
71 async_dump
, /* dump */
72 add_queue
, /* add_queue */
73 remove_queue
, /* remove_queue */
74 async_signaled
, /* signaled */
75 async_satisfied
, /* satisfied */
76 no_signal
, /* signal */
77 no_get_fd
, /* get_fd */
78 default_map_access
, /* map_access */
79 default_get_sd
, /* get_sd */
80 default_set_sd
, /* set_sd */
81 no_get_full_name
, /* get_full_name */
82 no_lookup_name
, /* lookup_name */
83 no_link_name
, /* link_name */
84 NULL
, /* unlink_name */
85 no_open_file
, /* open_file */
86 no_kernel_obj_list
, /* get_kernel_obj_list */
87 no_close_handle
, /* close_handle */
88 async_destroy
/* destroy */
91 static inline void async_reselect( struct async
*async
)
93 if (async
->queue
&& async
->fd
) fd_reselect_async( async
->fd
, async
->queue
);
96 static void async_dump( struct object
*obj
, int verbose
)
98 struct async
*async
= (struct async
*)obj
;
99 assert( obj
->ops
== &async_ops
);
100 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
103 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
105 struct async
*async
= (struct async
*)obj
;
106 assert( obj
->ops
== &async_ops
);
107 return async
->signaled
;
110 static void async_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
112 struct async
*async
= (struct async
*)obj
;
113 assert( obj
->ops
== &async_ops
);
115 if (async
->direct_result
)
117 async_set_result( &async
->obj
, async
->iosb
->status
, async
->iosb
->result
);
118 async
->direct_result
= 0;
121 set_wait_status( entry
, async
->status
);
123 /* close wait handle here to avoid extra server round trip */
124 if (async
->wait_handle
)
126 close_handle( async
->thread
->process
, async
->wait_handle
);
127 async
->wait_handle
= 0;
131 static void async_destroy( struct object
*obj
)
133 struct async
*async
= (struct async
*)obj
;
134 assert( obj
->ops
== &async_ops
);
136 list_remove( &async
->process_entry
);
140 list_remove( &async
->queue_entry
);
141 async_reselect( async
);
143 else if (async
->fd
) release_object( async
->fd
);
145 if (async
->timeout
) remove_timeout_user( async
->timeout
);
146 if (async
->completion
) release_object( async
->completion
);
147 if (async
->event
) release_object( async
->event
);
148 if (async
->iosb
) release_object( async
->iosb
);
149 release_object( async
->thread
);
152 /* notifies client thread of new status of its async request */
153 void async_terminate( struct async
*async
, unsigned int status
)
155 assert( status
!= STATUS_PENDING
);
157 if (async
->status
!= STATUS_PENDING
) return; /* already terminated */
159 async
->status
= status
;
160 if (async
->iosb
&& async
->iosb
->status
== STATUS_PENDING
) async
->iosb
->status
= status
;
162 if (!async
->direct_result
)
166 memset( &data
, 0, sizeof(data
) );
167 data
.type
= APC_ASYNC_IO
;
168 data
.async_io
.user
= async
->data
.user
;
169 data
.async_io
.sb
= async
->data
.iosb
;
170 data
.async_io
.status
= status
;
171 thread_queue_apc( async
->thread
->process
, async
->thread
, &async
->obj
, &data
);
174 async_reselect( async
);
175 if (async
->queue
) release_object( async
); /* so that it gets destroyed when the async is done */
178 /* callback for timeout on an async request */
179 static void async_timeout( void *private )
181 struct async
*async
= private;
183 async
->timeout
= NULL
;
184 async_terminate( async
, async
->timeout_status
);
187 /* free an async queue, cancelling all async operations */
188 void free_async_queue( struct async_queue
*queue
)
190 struct async
*async
, *next
;
192 LIST_FOR_EACH_ENTRY_SAFE( async
, next
, &queue
->queue
, struct async
, queue_entry
)
194 grab_object( &async
->obj
);
195 if (!async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
197 async_terminate( async
, STATUS_HANDLES_CLOSED
);
199 release_object( &async
->obj
);
203 void queue_async( struct async_queue
*queue
, struct async
*async
)
205 /* fd will be set to NULL in free_async_queue when fd is destroyed */
206 release_object( async
->fd
);
208 async
->queue
= queue
;
209 grab_object( async
);
210 list_add_tail( &queue
->queue
, &async
->queue_entry
);
212 set_fd_signaled( async
->fd
, 0 );
215 /* create an async on a given queue of a fd */
216 struct async
*create_async( struct fd
*fd
, struct thread
*thread
, const async_data_t
*data
, struct iosb
*iosb
)
218 struct event
*event
= NULL
;
221 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
224 if (!(async
= alloc_object( &async_ops
)))
226 if (event
) release_object( event
);
230 async
->thread
= (struct thread
*)grab_object( thread
);
231 async
->event
= event
;
232 async
->status
= STATUS_PENDING
;
234 async
->timeout
= NULL
;
236 async
->fd
= (struct fd
*)grab_object( fd
);
239 async
->wait_handle
= 0;
240 async
->direct_result
= 0;
241 async
->completion
= fd_get_completion( fd
, &async
->comp_key
);
242 async
->comp_flags
= 0;
243 async
->completion_callback
= NULL
;
244 async
->completion_callback_private
= NULL
;
246 if (iosb
) async
->iosb
= (struct iosb
*)grab_object( iosb
);
247 else async
->iosb
= NULL
;
249 list_add_head( &thread
->process
->asyncs
, &async
->process_entry
);
250 if (event
) reset_event( event
);
252 if (async
->completion
&& data
->apc
)
254 release_object( async
);
255 set_error( STATUS_INVALID_PARAMETER
);
262 void set_async_pending( struct async
*async
, int signal
)
264 if (async
->status
== STATUS_PENDING
)
267 if (signal
&& !async
->signaled
)
270 wake_up( &async
->obj
, 0 );
275 /* create an async associated with iosb for async-based requests
276 * returned async must be passed to async_handoff */
277 struct async
*create_request_async( struct fd
*fd
, unsigned int comp_flags
, const async_data_t
*data
)
282 if (!(iosb
= create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
285 async
= create_async( fd
, current
, data
, iosb
);
286 release_object( iosb
);
289 if (!(async
->wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 )))
291 release_object( async
);
295 async
->direct_result
= 1;
296 async
->comp_flags
= comp_flags
;
301 /* return async object status and wait handle to client */
302 obj_handle_t
async_handoff( struct async
*async
, int success
, data_size_t
*result
, int force_blocking
)
306 if (get_error() == STATUS_PENDING
)
308 /* we don't know the result yet, so client needs to wait */
309 async
->direct_result
= 0;
310 return async
->wait_handle
;
312 close_handle( async
->thread
->process
, async
->wait_handle
);
313 async
->wait_handle
= 0;
317 if (get_error() != STATUS_PENDING
)
319 /* status and data are already set and returned */
320 async_terminate( async
, get_error() );
322 else if (async
->iosb
->status
!= STATUS_PENDING
)
324 /* result is already available in iosb, return it */
325 if (async
->iosb
->out_data
)
327 set_reply_data_ptr( async
->iosb
->out_data
, async
->iosb
->out_size
);
328 async
->iosb
->out_data
= NULL
;
332 if (async
->iosb
->status
!= STATUS_PENDING
)
334 if (result
) *result
= async
->iosb
->result
;
339 async
->direct_result
= 0;
341 if (!force_blocking
&& async
->fd
&& is_fd_overlapped( async
->fd
))
343 close_handle( async
->thread
->process
, async
->wait_handle
);
344 async
->wait_handle
= 0;
347 set_error( async
->iosb
->status
);
348 return async
->wait_handle
;
351 /* set the timeout of an async operation */
352 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
354 if (async
->timeout
) remove_timeout_user( async
->timeout
);
355 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
356 else async
->timeout
= NULL
;
357 async
->timeout_status
= status
;
360 /* set a callback to be notified when the async is completed */
361 void async_set_completion_callback( struct async
*async
, async_completion_callback func
, void *private )
363 async
->completion_callback
= func
;
364 async
->completion_callback_private
= private;
367 static void add_async_completion( struct async
*async
, apc_param_t cvalue
, unsigned int status
,
368 apc_param_t information
)
370 if (async
->fd
&& !async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
371 if (async
->completion
) add_completion( async
->completion
, async
->comp_key
, cvalue
, status
, information
);
374 /* store the result of the client-side async callback */
375 void async_set_result( struct object
*obj
, unsigned int status
, apc_param_t total
)
377 struct async
*async
= (struct async
*)obj
;
379 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
381 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
383 if (status
== STATUS_PENDING
) /* restart it */
385 status
= async
->status
;
386 async
->status
= STATUS_PENDING
;
387 grab_object( async
);
389 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
390 async_terminate( async
, status
);
392 async_reselect( async
);
396 if (async
->timeout
) remove_timeout_user( async
->timeout
);
397 async
->timeout
= NULL
;
398 async
->status
= status
;
403 memset( &data
, 0, sizeof(data
) );
404 data
.type
= APC_USER
;
405 data
.user
.func
= async
->data
.apc
;
406 data
.user
.args
[0] = async
->data
.apc_context
;
407 data
.user
.args
[1] = async
->data
.iosb
;
408 data
.user
.args
[2] = 0;
409 thread_queue_apc( NULL
, async
->thread
, NULL
, &data
);
411 else if (async
->data
.apc_context
&& (async
->pending
||
412 !(async
->comp_flags
& FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
)))
414 add_async_completion( async
, async
->data
.apc_context
, status
, total
);
417 if (async
->event
) set_event( async
->event
);
418 else if (async
->fd
) set_fd_signaled( async
->fd
, 1 );
419 if (!async
->signaled
)
422 wake_up( &async
->obj
, 0 );
425 if (async
->completion_callback
)
426 async
->completion_callback( async
->completion_callback_private
);
427 async
->completion_callback
= NULL
;
431 /* check if an async operation is waiting to be alerted */
432 int async_waiting( struct async_queue
*queue
)
437 if (!(ptr
= list_head( &queue
->queue
))) return 0;
438 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
439 return async
->status
== STATUS_PENDING
;
442 static int cancel_async( struct process
*process
, struct object
*obj
, struct thread
*thread
, client_ptr_t iosb
)
448 LIST_FOR_EACH_ENTRY( async
, &process
->asyncs
, struct async
, process_entry
)
450 if (async
->status
!= STATUS_PENDING
) continue;
451 if ((!obj
|| (async
->fd
&& get_fd_user( async
->fd
) == obj
)) &&
452 (!thread
|| async
->thread
== thread
) &&
453 (!iosb
|| async
->data
.iosb
== iosb
))
455 async_terminate( async
, STATUS_CANCELLED
);
463 void cancel_process_asyncs( struct process
*process
)
465 cancel_async( process
, NULL
, NULL
, 0 );
468 /* wake up async operations on the queue */
469 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
471 struct list
*ptr
, *next
;
473 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
475 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
476 async_terminate( async
, status
);
477 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */
481 static void iosb_dump( struct object
*obj
, int verbose
);
482 static void iosb_destroy( struct object
*obj
);
484 static const struct object_ops iosb_ops
=
486 sizeof(struct iosb
), /* size */
488 iosb_dump
, /* dump */
489 no_add_queue
, /* add_queue */
490 NULL
, /* remove_queue */
492 NULL
, /* satisfied */
493 no_signal
, /* signal */
494 no_get_fd
, /* get_fd */
495 default_map_access
, /* map_access */
496 default_get_sd
, /* get_sd */
497 default_set_sd
, /* set_sd */
498 no_get_full_name
, /* get_full_name */
499 no_lookup_name
, /* lookup_name */
500 no_link_name
, /* link_name */
501 NULL
, /* unlink_name */
502 no_open_file
, /* open_file */
503 no_kernel_obj_list
, /* get_kernel_obj_list */
504 no_close_handle
, /* close_handle */
505 iosb_destroy
/* destroy */
508 static void iosb_dump( struct object
*obj
, int verbose
)
510 assert( obj
->ops
== &iosb_ops
);
511 fprintf( stderr
, "I/O status block\n" );
514 static void iosb_destroy( struct object
*obj
)
516 struct iosb
*iosb
= (struct iosb
*)obj
;
518 free( iosb
->in_data
);
519 free( iosb
->out_data
);
522 /* allocate iosb struct */
523 struct iosb
*create_iosb( const void *in_data
, data_size_t in_size
, data_size_t out_size
)
527 if (!(iosb
= alloc_object( &iosb_ops
))) return NULL
;
529 iosb
->status
= STATUS_PENDING
;
531 iosb
->in_size
= in_size
;
532 iosb
->in_data
= NULL
;
533 iosb
->out_size
= out_size
;
534 iosb
->out_data
= NULL
;
536 if (in_size
&& !(iosb
->in_data
= memdup( in_data
, in_size
)))
538 release_object( iosb
);
545 struct iosb
*async_get_iosb( struct async
*async
)
547 return async
->iosb
? (struct iosb
*)grab_object( async
->iosb
) : NULL
;
550 struct thread
*async_get_thread( struct async
*async
)
552 return async
->thread
;
555 /* find the first pending async in queue */
556 struct async
*find_pending_async( struct async_queue
*queue
)
559 LIST_FOR_EACH_ENTRY( async
, &queue
->queue
, struct async
, queue_entry
)
560 if (async
->status
== STATUS_PENDING
) return (struct async
*)grab_object( async
);
564 /* cancels all async I/O */
565 DECL_HANDLER(cancel_async
)
567 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
568 struct thread
*thread
= req
->only_thread
? current
: NULL
;
572 int count
= cancel_async( current
->process
, obj
, thread
, req
->iosb
);
573 if (!count
&& req
->iosb
) set_error( STATUS_NOT_FOUND
);
574 release_object( obj
);
578 /* get async result from associated iosb */
579 DECL_HANDLER(get_async_result
)
581 struct iosb
*iosb
= NULL
;
584 LIST_FOR_EACH_ENTRY( async
, ¤t
->process
->asyncs
, struct async
, process_entry
)
585 if (async
->data
.user
== req
->user_arg
)
593 set_error( STATUS_INVALID_PARAMETER
);
599 data_size_t size
= min( iosb
->out_size
, get_reply_max_size() );
602 set_reply_data_ptr( iosb
->out_data
, size
);
603 iosb
->out_data
= NULL
;
606 reply
->size
= iosb
->result
;
607 set_error( iosb
->status
);