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 */
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 */
56 static void async_dump( struct object
*obj
, int verbose
);
57 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
58 static void async_satisfied( struct object
* obj
, struct wait_queue_entry
*entry
);
59 static void async_destroy( struct object
*obj
);
61 static const struct object_ops async_ops
=
63 sizeof(struct async
), /* size */
64 async_dump
, /* dump */
65 no_get_type
, /* get_type */
66 add_queue
, /* add_queue */
67 remove_queue
, /* remove_queue */
68 async_signaled
, /* signaled */
69 async_satisfied
, /* satisfied */
70 no_signal
, /* signal */
71 no_get_fd
, /* get_fd */
72 no_map_access
, /* map_access */
73 default_get_sd
, /* get_sd */
74 default_set_sd
, /* set_sd */
75 no_lookup_name
, /* lookup_name */
76 no_link_name
, /* link_name */
77 NULL
, /* unlink_name */
78 no_open_file
, /* open_file */
79 no_close_handle
, /* close_handle */
80 async_destroy
/* destroy */
86 struct object obj
; /* object header */
87 struct fd
*fd
; /* file descriptor owning this queue */
88 struct completion
*completion
; /* completion associated with a recently closed file descriptor */
89 apc_param_t comp_key
; /* completion key associated with a recently closed file descriptor */
90 struct list queue
; /* queue of async objects */
93 static void async_queue_dump( struct object
*obj
, int verbose
);
94 static void async_queue_destroy( struct object
*obj
);
96 static const struct object_ops async_queue_ops
=
98 sizeof(struct async_queue
), /* size */
99 async_queue_dump
, /* dump */
100 no_get_type
, /* get_type */
101 no_add_queue
, /* add_queue */
102 NULL
, /* remove_queue */
104 NULL
, /* satisfied */
105 no_signal
, /* signal */
106 no_get_fd
, /* get_fd */
107 no_map_access
, /* map_access */
108 default_get_sd
, /* get_sd */
109 default_set_sd
, /* set_sd */
110 no_lookup_name
, /* lookup_name */
111 no_link_name
, /* link_name */
112 NULL
, /* unlink_name */
113 no_open_file
, /* open_file */
114 no_close_handle
, /* close_handle */
115 async_queue_destroy
/* destroy */
119 static inline void async_reselect( struct async
*async
)
121 if (async
->queue
&& async
->queue
->fd
) fd_reselect_async( async
->queue
->fd
, async
->queue
);
124 static void async_dump( struct object
*obj
, int verbose
)
126 struct async
*async
= (struct async
*)obj
;
127 assert( obj
->ops
== &async_ops
);
128 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
131 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
133 struct async
*async
= (struct async
*)obj
;
134 assert( obj
->ops
== &async_ops
);
135 return async
->signaled
;
138 static void async_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
140 struct async
*async
= (struct async
*)obj
;
141 assert( obj
->ops
== &async_ops
);
143 if (async
->direct_result
)
145 async_set_result( &async
->obj
, async
->iosb
->status
, async
->iosb
->result
);
146 async
->direct_result
= 0;
149 /* close wait handle here to avoid extra server round trip */
150 if (async
->wait_handle
)
152 close_handle( async
->thread
->process
, async
->wait_handle
);
153 async
->wait_handle
= 0;
157 static void async_destroy( struct object
*obj
)
159 struct async
*async
= (struct async
*)obj
;
160 assert( obj
->ops
== &async_ops
);
162 list_remove( &async
->process_entry
);
166 list_remove( &async
->queue_entry
);
167 async_reselect( async
);
168 release_object( async
->queue
);
171 if (async
->fd
) release_object( async
->fd
);
172 if (async
->timeout
) remove_timeout_user( async
->timeout
);
173 if (async
->event
) release_object( async
->event
);
174 if (async
->iosb
) release_object( async
->iosb
);
175 release_object( async
->thread
);
178 static void async_queue_dump( struct object
*obj
, int verbose
)
180 struct async_queue
*async_queue
= (struct async_queue
*)obj
;
181 assert( obj
->ops
== &async_queue_ops
);
182 fprintf( stderr
, "Async queue fd=%p\n", async_queue
->fd
);
185 static void async_queue_destroy( struct object
*obj
)
187 struct async_queue
*async_queue
= (struct async_queue
*)obj
;
188 assert( obj
->ops
== &async_queue_ops
);
189 if (async_queue
->completion
) release_object( async_queue
->completion
);
192 /* notifies client thread of new status of its async request */
193 void async_terminate( struct async
*async
, unsigned int status
)
195 assert( status
!= STATUS_PENDING
);
197 if (async
->status
!= STATUS_PENDING
)
199 /* already terminated, just update status */
200 async
->status
= status
;
204 async
->status
= status
;
205 if (async
->iosb
&& async
->iosb
->status
== STATUS_PENDING
) async
->iosb
->status
= status
;
207 if (!async
->direct_result
)
209 if (async
->data
.user
)
213 memset( &data
, 0, sizeof(data
) );
214 data
.type
= APC_ASYNC_IO
;
215 data
.async_io
.user
= async
->data
.user
;
216 data
.async_io
.sb
= async
->data
.iosb
;
217 data
.async_io
.status
= status
;
218 thread_queue_apc( async
->thread
, &async
->obj
, &data
);
220 else async_set_result( &async
->obj
, STATUS_SUCCESS
, 0 );
223 async_reselect( async
);
224 if (async
->queue
) release_object( async
); /* so that it gets destroyed when the async is done */
227 /* callback for timeout on an async request */
228 static void async_timeout( void *private )
230 struct async
*async
= private;
232 async
->timeout
= NULL
;
233 async_terminate( async
, async
->timeout_status
);
236 /* create a new async queue for a given fd */
237 struct async_queue
*create_async_queue( struct fd
*fd
)
239 struct async_queue
*queue
= alloc_object( &async_queue_ops
);
244 queue
->completion
= NULL
;
245 list_init( &queue
->queue
);
250 /* free an async queue, cancelling all async operations */
251 void free_async_queue( struct async_queue
*queue
)
254 if (queue
->fd
) queue
->completion
= fd_get_completion( queue
->fd
, &queue
->comp_key
);
256 async_wake_up( queue
, STATUS_HANDLES_CLOSED
);
257 release_object( queue
);
260 void queue_async( struct async_queue
*queue
, struct async
*async
)
262 release_object( async
->fd
);
265 async
->queue
= (struct async_queue
*)grab_object( queue
);
266 grab_object( async
);
267 list_add_tail( &queue
->queue
, &async
->queue_entry
);
269 if (queue
->fd
) set_fd_signaled( queue
->fd
, 0 );
272 /* create an async on a given queue of a fd */
273 struct async
*create_async( struct fd
*fd
, struct thread
*thread
, const async_data_t
*data
, struct iosb
*iosb
)
275 struct event
*event
= NULL
;
278 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
281 if (!(async
= alloc_object( &async_ops
)))
283 if (event
) release_object( event
);
287 async
->thread
= (struct thread
*)grab_object( thread
);
288 async
->event
= event
;
289 async
->status
= STATUS_PENDING
;
291 async
->timeout
= NULL
;
293 async
->fd
= (struct fd
*)grab_object( fd
);
295 async
->wait_handle
= 0;
296 async
->direct_result
= 0;
298 if (iosb
) async
->iosb
= (struct iosb
*)grab_object( iosb
);
299 else async
->iosb
= NULL
;
301 list_add_head( &thread
->process
->asyncs
, &async
->process_entry
);
302 if (event
) reset_event( event
);
307 /* create an async associated with iosb for async-based requests
308 * returned async must be passed to async_handoff */
309 struct async
*create_request_async( struct fd
*fd
, const async_data_t
*data
)
314 if (!(iosb
= create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
317 async
= create_async( fd
, current
, data
, iosb
);
318 release_object( iosb
);
321 if (!(async
->wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 )))
323 release_object( async
);
326 async
->direct_result
= 1;
331 /* return async object status and wait handle to client */
332 obj_handle_t
async_handoff( struct async
*async
, int success
, data_size_t
*result
)
336 close_handle( async
->thread
->process
, async
->wait_handle
);
337 async
->wait_handle
= 0;
341 if (get_error() != STATUS_PENDING
)
343 /* status and data are already set and returned */
344 async_terminate( async
, get_error() );
346 else if (async
->iosb
->status
!= STATUS_PENDING
)
348 /* result is already available in iosb, return it */
349 if (async
->iosb
->out_data
)
351 set_reply_data_ptr( async
->iosb
->out_data
, async
->iosb
->out_size
);
352 async
->iosb
->out_data
= NULL
;
356 if (async
->iosb
->status
!= STATUS_PENDING
)
358 if (result
) *result
= async
->iosb
->result
;
363 async
->direct_result
= 0;
364 if (!async_is_blocking( async
))
366 close_handle( async
->thread
->process
, async
->wait_handle
);
367 async
->wait_handle
= 0;
370 set_error( async
->iosb
->status
);
371 return async
->wait_handle
;
374 /* set the timeout of an async operation */
375 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
377 if (async
->timeout
) remove_timeout_user( async
->timeout
);
378 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
379 else async
->timeout
= NULL
;
380 async
->timeout_status
= status
;
383 static void add_async_completion( struct async
*async
, apc_param_t cvalue
, unsigned int status
,
384 apc_param_t information
)
386 struct fd
*fd
= async
->queue
? async
->queue
->fd
: async
->fd
;
391 struct completion
*completion
= fd_get_completion( fd
, &ckey
);
395 add_completion( completion
, ckey
, cvalue
, status
, information
);
396 release_object( completion
);
399 else if (async
->queue
&& async
->queue
->completion
)
400 add_completion( async
->queue
->completion
, async
->queue
->comp_key
, cvalue
, status
, information
);
403 /* store the result of the client-side async callback */
404 void async_set_result( struct object
*obj
, unsigned int status
, apc_param_t total
)
406 struct async
*async
= (struct async
*)obj
;
408 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
410 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
412 if (status
== STATUS_PENDING
) /* restart it */
414 status
= async
->status
;
415 async
->status
= STATUS_PENDING
;
416 grab_object( async
);
418 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
419 async_terminate( async
, status
);
421 async_reselect( async
);
425 if (async
->timeout
) remove_timeout_user( async
->timeout
);
426 async
->timeout
= NULL
;
427 async
->status
= status
;
428 if (status
== STATUS_MORE_PROCESSING_REQUIRED
) return; /* don't report the completion */
433 memset( &data
, 0, sizeof(data
) );
434 data
.type
= APC_USER
;
435 data
.user
.func
= async
->data
.apc
;
436 data
.user
.args
[0] = async
->data
.apc_context
;
437 data
.user
.args
[1] = async
->data
.iosb
;
438 data
.user
.args
[2] = 0;
439 thread_queue_apc( async
->thread
, NULL
, &data
);
441 else if (async
->data
.apc_context
)
442 add_async_completion( async
, async
->data
.apc_context
, status
, total
);
444 if (async
->event
) set_event( async
->event
);
445 else if (async
->fd
) set_fd_signaled( async
->fd
, 1 );
446 else if (async
->queue
&& async
->queue
->fd
) set_fd_signaled( async
->queue
->fd
, 1 );
447 if (!async
->signaled
)
450 wake_up( &async
->obj
, 0 );
455 /* check if there are any queued async operations */
456 int async_queued( struct async_queue
*queue
)
458 return queue
&& list_head( &queue
->queue
);
461 /* check if an async operation is waiting to be alerted */
462 int async_waiting( struct async_queue
*queue
)
467 if (!queue
) return 0;
468 if (!(ptr
= list_head( &queue
->queue
))) return 0;
469 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
470 return async
->status
== STATUS_PENDING
;
473 static int cancel_async( struct process
*process
, struct object
*obj
, struct thread
*thread
, client_ptr_t iosb
)
480 LIST_FOR_EACH_ENTRY( async
, &process
->asyncs
, struct async
, process_entry
)
482 if (async
->status
== STATUS_CANCELLED
) continue;
483 fd
= async
->queue
? async
->queue
->fd
: async
->fd
;
484 if ((!obj
|| (fd
&& get_fd_user( fd
) == obj
)) &&
485 (!thread
|| async
->thread
== thread
) &&
486 (!iosb
|| async
->data
.iosb
== iosb
))
488 async_terminate( async
, STATUS_CANCELLED
);
496 void cancel_process_asyncs( struct process
*process
)
498 cancel_async( process
, NULL
, NULL
, 0 );
501 /* wake up async operations on the queue */
502 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
504 struct list
*ptr
, *next
;
508 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
510 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
511 async_terminate( async
, status
);
512 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */
516 static void iosb_dump( struct object
*obj
, int verbose
);
517 static void iosb_destroy( struct object
*obj
);
519 static const struct object_ops iosb_ops
=
521 sizeof(struct iosb
), /* size */
522 iosb_dump
, /* dump */
523 no_get_type
, /* get_type */
524 no_add_queue
, /* add_queue */
525 NULL
, /* remove_queue */
527 NULL
, /* satisfied */
528 no_signal
, /* signal */
529 no_get_fd
, /* get_fd */
530 no_map_access
, /* map_access */
531 default_get_sd
, /* get_sd */
532 default_set_sd
, /* set_sd */
533 no_lookup_name
, /* lookup_name */
534 no_link_name
, /* link_name */
535 NULL
, /* unlink_name */
536 no_open_file
, /* open_file */
537 no_close_handle
, /* close_handle */
538 iosb_destroy
/* destroy */
541 static void iosb_dump( struct object
*obj
, int verbose
)
543 assert( obj
->ops
== &iosb_ops
);
544 fprintf( stderr
, "I/O status block\n" );
547 static void iosb_destroy( struct object
*obj
)
549 struct iosb
*iosb
= (struct iosb
*)obj
;
551 free( iosb
->in_data
);
552 free( iosb
->out_data
);
555 /* allocate iosb struct */
556 struct iosb
*create_iosb( const void *in_data
, data_size_t in_size
, data_size_t out_size
)
560 if (!(iosb
= alloc_object( &iosb_ops
))) return NULL
;
562 iosb
->status
= STATUS_PENDING
;
564 iosb
->in_size
= in_size
;
565 iosb
->in_data
= NULL
;
566 iosb
->out_size
= out_size
;
567 iosb
->out_data
= NULL
;
569 if (in_size
&& !(iosb
->in_data
= memdup( in_data
, in_size
)))
571 release_object( iosb
);
578 struct iosb
*async_get_iosb( struct async
*async
)
580 return async
->iosb
? (struct iosb
*)grab_object( async
->iosb
) : NULL
;
583 int async_is_blocking( struct async
*async
)
585 return !async
->event
&& !async
->data
.apc
&& !async
->data
.apc_context
;
588 /* find the first pending async in queue */
589 struct async
*find_pending_async( struct async_queue
*queue
)
592 if (queue
) LIST_FOR_EACH_ENTRY( async
, &queue
->queue
, struct async
, queue_entry
)
593 if (async
->status
== STATUS_PENDING
) return (struct async
*)grab_object( async
);
597 /* cancels all async I/O */
598 DECL_HANDLER(cancel_async
)
600 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
601 struct thread
*thread
= req
->only_thread
? current
: NULL
;
605 int count
= cancel_async( current
->process
, obj
, thread
, req
->iosb
);
606 if (!count
&& req
->iosb
) set_error( STATUS_NOT_FOUND
);
607 release_object( obj
);
611 /* get async result from associated iosb */
612 DECL_HANDLER(get_async_result
)
614 struct iosb
*iosb
= NULL
;
617 LIST_FOR_EACH_ENTRY( async
, ¤t
->process
->asyncs
, struct async
, process_entry
)
618 if (async
->data
.user
== req
->user_arg
)
626 set_error( STATUS_INVALID_PARAMETER
);
632 data_size_t size
= min( iosb
->out_size
, get_reply_max_size() );
635 set_reply_data_ptr( iosb
->out_data
, size
);
636 iosb
->out_data
= NULL
;
639 reply
->size
= iosb
->result
;
640 set_error( iosb
->status
);