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
)
164 if (async
->data
.user
)
168 memset( &data
, 0, sizeof(data
) );
169 data
.type
= APC_ASYNC_IO
;
170 data
.async_io
.user
= async
->data
.user
;
171 data
.async_io
.sb
= async
->data
.iosb
;
172 data
.async_io
.status
= status
;
173 thread_queue_apc( async
->thread
->process
, async
->thread
, &async
->obj
, &data
);
175 else async_set_result( &async
->obj
, STATUS_SUCCESS
, 0 );
178 async_reselect( async
);
179 if (async
->queue
) release_object( async
); /* so that it gets destroyed when the async is done */
182 /* callback for timeout on an async request */
183 static void async_timeout( void *private )
185 struct async
*async
= private;
187 async
->timeout
= NULL
;
188 async_terminate( async
, async
->timeout_status
);
191 /* free an async queue, cancelling all async operations */
192 void free_async_queue( struct async_queue
*queue
)
194 struct async
*async
, *next
;
196 LIST_FOR_EACH_ENTRY_SAFE( async
, next
, &queue
->queue
, struct async
, queue_entry
)
198 grab_object( &async
->obj
);
199 if (!async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
201 async_terminate( async
, STATUS_HANDLES_CLOSED
);
203 release_object( &async
->obj
);
207 void queue_async( struct async_queue
*queue
, struct async
*async
)
209 /* fd will be set to NULL in free_async_queue when fd is destroyed */
210 release_object( async
->fd
);
212 async
->queue
= queue
;
213 grab_object( async
);
214 list_add_tail( &queue
->queue
, &async
->queue_entry
);
216 set_fd_signaled( async
->fd
, 0 );
219 /* create an async on a given queue of a fd */
220 struct async
*create_async( struct fd
*fd
, struct thread
*thread
, const async_data_t
*data
, struct iosb
*iosb
)
222 struct event
*event
= NULL
;
225 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
228 if (!(async
= alloc_object( &async_ops
)))
230 if (event
) release_object( event
);
234 async
->thread
= (struct thread
*)grab_object( thread
);
235 async
->event
= event
;
236 async
->status
= STATUS_PENDING
;
238 async
->timeout
= NULL
;
240 async
->fd
= (struct fd
*)grab_object( fd
);
243 async
->wait_handle
= 0;
244 async
->direct_result
= 0;
245 async
->completion
= fd_get_completion( fd
, &async
->comp_key
);
246 async
->comp_flags
= 0;
247 async
->completion_callback
= NULL
;
248 async
->completion_callback_private
= NULL
;
250 if (iosb
) async
->iosb
= (struct iosb
*)grab_object( iosb
);
251 else async
->iosb
= NULL
;
253 list_add_head( &thread
->process
->asyncs
, &async
->process_entry
);
254 if (event
) reset_event( event
);
256 if (async
->completion
&& data
->apc
)
258 release_object( async
);
259 set_error( STATUS_INVALID_PARAMETER
);
266 void set_async_pending( struct async
*async
, int signal
)
268 if (async
->status
== STATUS_PENDING
)
271 if (signal
&& !async
->signaled
)
274 wake_up( &async
->obj
, 0 );
279 /* create an async associated with iosb for async-based requests
280 * returned async must be passed to async_handoff */
281 struct async
*create_request_async( struct fd
*fd
, unsigned int comp_flags
, const async_data_t
*data
)
286 if (!(iosb
= create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
289 async
= create_async( fd
, current
, data
, iosb
);
290 release_object( iosb
);
293 if (!(async
->wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 )))
295 release_object( async
);
299 async
->direct_result
= 1;
300 async
->comp_flags
= comp_flags
;
305 /* return async object status and wait handle to client */
306 obj_handle_t
async_handoff( struct async
*async
, int success
, data_size_t
*result
, int force_blocking
)
310 if (get_error() == STATUS_PENDING
)
312 /* we don't know the result yet, so client needs to wait */
313 async
->direct_result
= 0;
314 return async
->wait_handle
;
316 close_handle( async
->thread
->process
, async
->wait_handle
);
317 async
->wait_handle
= 0;
321 if (get_error() != STATUS_PENDING
)
323 /* status and data are already set and returned */
324 async_terminate( async
, get_error() );
326 else if (async
->iosb
->status
!= STATUS_PENDING
)
328 /* result is already available in iosb, return it */
329 if (async
->iosb
->out_data
)
331 set_reply_data_ptr( async
->iosb
->out_data
, async
->iosb
->out_size
);
332 async
->iosb
->out_data
= NULL
;
336 if (async
->iosb
->status
!= STATUS_PENDING
)
338 if (result
) *result
= async
->iosb
->result
;
343 async
->direct_result
= 0;
345 if (!force_blocking
&& async
->fd
&& is_fd_overlapped( async
->fd
))
347 close_handle( async
->thread
->process
, async
->wait_handle
);
348 async
->wait_handle
= 0;
351 set_error( async
->iosb
->status
);
352 return async
->wait_handle
;
355 /* set the timeout of an async operation */
356 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
358 if (async
->timeout
) remove_timeout_user( async
->timeout
);
359 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
360 else async
->timeout
= NULL
;
361 async
->timeout_status
= status
;
364 /* set a callback to be notified when the async is completed */
365 void async_set_completion_callback( struct async
*async
, async_completion_callback func
, void *private )
367 async
->completion_callback
= func
;
368 async
->completion_callback_private
= private;
371 static void add_async_completion( struct async
*async
, apc_param_t cvalue
, unsigned int status
,
372 apc_param_t information
)
374 if (async
->fd
&& !async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
375 if (async
->completion
) add_completion( async
->completion
, async
->comp_key
, cvalue
, status
, information
);
378 /* store the result of the client-side async callback */
379 void async_set_result( struct object
*obj
, unsigned int status
, apc_param_t total
)
381 struct async
*async
= (struct async
*)obj
;
383 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
385 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
387 if (status
== STATUS_PENDING
) /* restart it */
389 status
= async
->status
;
390 async
->status
= STATUS_PENDING
;
391 grab_object( async
);
393 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
394 async_terminate( async
, status
);
396 async_reselect( async
);
400 if (async
->timeout
) remove_timeout_user( async
->timeout
);
401 async
->timeout
= NULL
;
402 async
->status
= status
;
403 if (status
== STATUS_MORE_PROCESSING_REQUIRED
) return; /* don't report the completion */
408 memset( &data
, 0, sizeof(data
) );
409 data
.type
= APC_USER
;
410 data
.user
.func
= async
->data
.apc
;
411 data
.user
.args
[0] = async
->data
.apc_context
;
412 data
.user
.args
[1] = async
->data
.iosb
;
413 data
.user
.args
[2] = 0;
414 thread_queue_apc( NULL
, async
->thread
, NULL
, &data
);
416 else if (async
->data
.apc_context
&& (async
->pending
||
417 !(async
->comp_flags
& FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
)))
419 add_async_completion( async
, async
->data
.apc_context
, status
, total
);
422 if (async
->event
) set_event( async
->event
);
423 else if (async
->fd
) set_fd_signaled( async
->fd
, 1 );
424 if (!async
->signaled
)
427 wake_up( &async
->obj
, 0 );
430 if (async
->completion_callback
)
431 async
->completion_callback( async
->completion_callback_private
);
432 async
->completion_callback
= NULL
;
436 /* check if an async operation is waiting to be alerted */
437 int async_waiting( struct async_queue
*queue
)
442 if (!(ptr
= list_head( &queue
->queue
))) return 0;
443 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
444 return async
->status
== STATUS_PENDING
;
447 static int cancel_async( struct process
*process
, struct object
*obj
, struct thread
*thread
, client_ptr_t iosb
)
453 LIST_FOR_EACH_ENTRY( async
, &process
->asyncs
, struct async
, process_entry
)
455 if (async
->status
!= STATUS_PENDING
) continue;
456 if ((!obj
|| (async
->fd
&& get_fd_user( async
->fd
) == obj
)) &&
457 (!thread
|| async
->thread
== thread
) &&
458 (!iosb
|| async
->data
.iosb
== iosb
))
460 async_terminate( async
, STATUS_CANCELLED
);
468 void cancel_process_asyncs( struct process
*process
)
470 cancel_async( process
, NULL
, NULL
, 0 );
473 /* wake up async operations on the queue */
474 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
476 struct list
*ptr
, *next
;
478 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
480 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
481 async_terminate( async
, status
);
482 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */
486 static void iosb_dump( struct object
*obj
, int verbose
);
487 static void iosb_destroy( struct object
*obj
);
489 static const struct object_ops iosb_ops
=
491 sizeof(struct iosb
), /* size */
493 iosb_dump
, /* dump */
494 no_add_queue
, /* add_queue */
495 NULL
, /* remove_queue */
497 NULL
, /* satisfied */
498 no_signal
, /* signal */
499 no_get_fd
, /* get_fd */
500 default_map_access
, /* map_access */
501 default_get_sd
, /* get_sd */
502 default_set_sd
, /* set_sd */
503 no_get_full_name
, /* get_full_name */
504 no_lookup_name
, /* lookup_name */
505 no_link_name
, /* link_name */
506 NULL
, /* unlink_name */
507 no_open_file
, /* open_file */
508 no_kernel_obj_list
, /* get_kernel_obj_list */
509 no_close_handle
, /* close_handle */
510 iosb_destroy
/* destroy */
513 static void iosb_dump( struct object
*obj
, int verbose
)
515 assert( obj
->ops
== &iosb_ops
);
516 fprintf( stderr
, "I/O status block\n" );
519 static void iosb_destroy( struct object
*obj
)
521 struct iosb
*iosb
= (struct iosb
*)obj
;
523 free( iosb
->in_data
);
524 free( iosb
->out_data
);
527 /* allocate iosb struct */
528 struct iosb
*create_iosb( const void *in_data
, data_size_t in_size
, data_size_t out_size
)
532 if (!(iosb
= alloc_object( &iosb_ops
))) return NULL
;
534 iosb
->status
= STATUS_PENDING
;
536 iosb
->in_size
= in_size
;
537 iosb
->in_data
= NULL
;
538 iosb
->out_size
= out_size
;
539 iosb
->out_data
= NULL
;
541 if (in_size
&& !(iosb
->in_data
= memdup( in_data
, in_size
)))
543 release_object( iosb
);
550 struct iosb
*async_get_iosb( struct async
*async
)
552 return async
->iosb
? (struct iosb
*)grab_object( async
->iosb
) : NULL
;
555 struct thread
*async_get_thread( struct async
*async
)
557 return async
->thread
;
560 /* find the first pending async in queue */
561 struct async
*find_pending_async( struct async_queue
*queue
)
564 LIST_FOR_EACH_ENTRY( async
, &queue
->queue
, struct async
, queue_entry
)
565 if (async
->status
== STATUS_PENDING
) return (struct async
*)grab_object( async
);
569 /* cancels all async I/O */
570 DECL_HANDLER(cancel_async
)
572 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
573 struct thread
*thread
= req
->only_thread
? current
: NULL
;
577 int count
= cancel_async( current
->process
, obj
, thread
, req
->iosb
);
578 if (!count
&& req
->iosb
) set_error( STATUS_NOT_FOUND
);
579 release_object( obj
);
583 /* get async result from associated iosb */
584 DECL_HANDLER(get_async_result
)
586 struct iosb
*iosb
= NULL
;
589 LIST_FOR_EACH_ENTRY( async
, ¤t
->process
->asyncs
, struct async
, process_entry
)
590 if (async
->data
.user
== req
->user_arg
)
598 set_error( STATUS_INVALID_PARAMETER
);
604 data_size_t size
= min( iosb
->out_size
, get_reply_max_size() );
607 set_reply_data_ptr( iosb
->out_data
, size
);
608 iosb
->out_data
= NULL
;
611 reply
->size
= iosb
->result
;
612 set_error( iosb
->status
);