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 */
54 struct completion
*completion
; /* completion associated with fd */
55 apc_param_t comp_key
; /* completion key associated with fd */
56 unsigned int comp_flags
; /* completion flags */
59 static void async_dump( struct object
*obj
, int verbose
);
60 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
61 static void async_satisfied( struct object
* obj
, struct wait_queue_entry
*entry
);
62 static void async_destroy( struct object
*obj
);
64 static const struct object_ops async_ops
=
66 sizeof(struct async
), /* size */
67 async_dump
, /* dump */
68 no_get_type
, /* get_type */
69 add_queue
, /* add_queue */
70 remove_queue
, /* remove_queue */
71 async_signaled
, /* signaled */
72 async_satisfied
, /* satisfied */
73 no_signal
, /* signal */
74 no_get_fd
, /* get_fd */
75 no_map_access
, /* map_access */
76 default_get_sd
, /* get_sd */
77 default_set_sd
, /* set_sd */
78 no_lookup_name
, /* lookup_name */
79 no_link_name
, /* link_name */
80 NULL
, /* unlink_name */
81 no_open_file
, /* open_file */
82 no_close_handle
, /* close_handle */
83 async_destroy
/* destroy */
86 static inline void async_reselect( struct async
*async
)
88 if (async
->queue
&& async
->fd
) fd_reselect_async( async
->fd
, async
->queue
);
91 static void async_dump( struct object
*obj
, int verbose
)
93 struct async
*async
= (struct async
*)obj
;
94 assert( obj
->ops
== &async_ops
);
95 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
98 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
100 struct async
*async
= (struct async
*)obj
;
101 assert( obj
->ops
== &async_ops
);
102 return async
->signaled
;
105 static void async_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
107 struct async
*async
= (struct async
*)obj
;
108 assert( obj
->ops
== &async_ops
);
110 if (async
->direct_result
)
112 async_set_result( &async
->obj
, async
->iosb
->status
, async
->iosb
->result
);
113 async
->direct_result
= 0;
116 /* close wait handle here to avoid extra server round trip */
117 if (async
->wait_handle
)
119 close_handle( async
->thread
->process
, async
->wait_handle
);
120 async
->wait_handle
= 0;
124 static void async_destroy( struct object
*obj
)
126 struct async
*async
= (struct async
*)obj
;
127 assert( obj
->ops
== &async_ops
);
129 list_remove( &async
->process_entry
);
133 list_remove( &async
->queue_entry
);
134 async_reselect( async
);
136 else if (async
->fd
) release_object( async
->fd
);
138 if (async
->timeout
) remove_timeout_user( async
->timeout
);
139 if (async
->completion
) release_object( async
->completion
);
140 if (async
->event
) release_object( async
->event
);
141 if (async
->iosb
) release_object( async
->iosb
);
142 release_object( async
->thread
);
145 /* notifies client thread of new status of its async request */
146 void async_terminate( struct async
*async
, unsigned int status
)
148 assert( status
!= STATUS_PENDING
);
150 if (async
->status
!= STATUS_PENDING
)
152 /* already terminated, just update status */
153 async
->status
= status
;
157 async
->status
= status
;
158 if (async
->iosb
&& async
->iosb
->status
== STATUS_PENDING
) async
->iosb
->status
= status
;
160 if (!async
->direct_result
)
162 if (async
->data
.user
)
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
);
173 else async_set_result( &async
->obj
, STATUS_SUCCESS
, 0 );
176 async_reselect( async
);
177 if (async
->queue
) release_object( async
); /* so that it gets destroyed when the async is done */
180 /* callback for timeout on an async request */
181 static void async_timeout( void *private )
183 struct async
*async
= private;
185 async
->timeout
= NULL
;
186 async_terminate( async
, async
->timeout_status
);
189 /* free an async queue, cancelling all async operations */
190 void free_async_queue( struct async_queue
*queue
)
192 struct async
*async
, *next
;
194 LIST_FOR_EACH_ENTRY_SAFE( async
, next
, &queue
->queue
, struct async
, queue_entry
)
196 grab_object( &async
->obj
);
197 if (!async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
199 async_terminate( async
, STATUS_HANDLES_CLOSED
);
201 release_object( &async
->obj
);
205 void queue_async( struct async_queue
*queue
, struct async
*async
)
207 /* fd will be set to NULL in free_async_queue when fd is destroyed */
208 release_object( async
->fd
);
210 async
->queue
= queue
;
211 grab_object( async
);
212 list_add_tail( &queue
->queue
, &async
->queue_entry
);
214 set_fd_signaled( async
->fd
, 0 );
217 /* create an async on a given queue of a fd */
218 struct async
*create_async( struct fd
*fd
, struct thread
*thread
, const async_data_t
*data
, struct iosb
*iosb
)
220 struct event
*event
= NULL
;
223 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
226 if (!(async
= alloc_object( &async_ops
)))
228 if (event
) release_object( event
);
232 async
->thread
= (struct thread
*)grab_object( thread
);
233 async
->event
= event
;
234 async
->status
= STATUS_PENDING
;
236 async
->timeout
= NULL
;
238 async
->fd
= (struct fd
*)grab_object( fd
);
240 async
->wait_handle
= 0;
241 async
->direct_result
= 0;
242 async
->completion
= fd_get_completion( fd
, &async
->comp_key
);
243 async
->comp_flags
= 0;
245 if (iosb
) async
->iosb
= (struct iosb
*)grab_object( iosb
);
246 else async
->iosb
= NULL
;
248 list_add_head( &thread
->process
->asyncs
, &async
->process_entry
);
249 if (event
) reset_event( event
);
251 if (async
->completion
&& data
->apc
)
253 release_object( async
);
254 set_error( STATUS_INVALID_PARAMETER
);
261 /* create an async associated with iosb for async-based requests
262 * returned async must be passed to async_handoff */
263 struct async
*create_request_async( struct fd
*fd
, unsigned int comp_flags
, const async_data_t
*data
)
268 if (!(iosb
= create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
271 async
= create_async( fd
, current
, data
, iosb
);
272 release_object( iosb
);
275 if (!(async
->wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 )))
277 release_object( async
);
280 async
->direct_result
= 1;
281 async
->comp_flags
= comp_flags
;
286 /* return async object status and wait handle to client */
287 obj_handle_t
async_handoff( struct async
*async
, int success
, data_size_t
*result
, int force_blocking
)
291 close_handle( async
->thread
->process
, async
->wait_handle
);
292 async
->wait_handle
= 0;
296 if (get_error() != STATUS_PENDING
)
298 /* status and data are already set and returned */
299 async_terminate( async
, get_error() );
301 else if (async
->iosb
->status
!= STATUS_PENDING
)
303 /* result is already available in iosb, return it */
304 if (async
->iosb
->out_data
)
306 set_reply_data_ptr( async
->iosb
->out_data
, async
->iosb
->out_size
);
307 async
->iosb
->out_data
= NULL
;
311 if (async
->iosb
->status
!= STATUS_PENDING
)
313 if (result
) *result
= async
->iosb
->result
;
318 async
->direct_result
= 0;
319 if (!force_blocking
&& async
->fd
&& is_fd_overlapped( async
->fd
))
321 close_handle( async
->thread
->process
, async
->wait_handle
);
322 async
->wait_handle
= 0;
325 set_error( async
->iosb
->status
);
326 return async
->wait_handle
;
329 /* set the timeout of an async operation */
330 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
332 if (async
->timeout
) remove_timeout_user( async
->timeout
);
333 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
334 else async
->timeout
= NULL
;
335 async
->timeout_status
= status
;
338 static void add_async_completion( struct async
*async
, apc_param_t cvalue
, unsigned int status
,
339 apc_param_t information
)
341 if (async
->fd
&& !async
->completion
) async
->completion
= fd_get_completion( async
->fd
, &async
->comp_key
);
342 if (async
->completion
) add_completion( async
->completion
, async
->comp_key
, cvalue
, status
, information
);
345 /* store the result of the client-side async callback */
346 void async_set_result( struct object
*obj
, unsigned int status
, apc_param_t total
)
348 struct async
*async
= (struct async
*)obj
;
350 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
352 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
354 if (status
== STATUS_PENDING
) /* restart it */
356 status
= async
->status
;
357 async
->status
= STATUS_PENDING
;
358 grab_object( async
);
360 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
361 async_terminate( async
, status
);
363 async_reselect( async
);
367 if (async
->timeout
) remove_timeout_user( async
->timeout
);
368 async
->timeout
= NULL
;
369 async
->status
= status
;
370 if (status
== STATUS_MORE_PROCESSING_REQUIRED
) return; /* don't report the completion */
375 memset( &data
, 0, sizeof(data
) );
376 data
.type
= APC_USER
;
377 data
.user
.func
= async
->data
.apc
;
378 data
.user
.args
[0] = async
->data
.apc_context
;
379 data
.user
.args
[1] = async
->data
.iosb
;
380 data
.user
.args
[2] = 0;
381 thread_queue_apc( NULL
, async
->thread
, NULL
, &data
);
383 else if (async
->data
.apc_context
&& (!async
->direct_result
||
384 !(async
->comp_flags
& FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
)))
386 add_async_completion( async
, async
->data
.apc_context
, status
, total
);
389 if (async
->event
) set_event( async
->event
);
390 else if (async
->fd
) set_fd_signaled( async
->fd
, 1 );
391 if (!async
->signaled
)
394 wake_up( &async
->obj
, 0 );
399 /* check if an async operation is waiting to be alerted */
400 int async_waiting( struct async_queue
*queue
)
405 if (!(ptr
= list_head( &queue
->queue
))) return 0;
406 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
407 return async
->status
== STATUS_PENDING
;
410 static int cancel_async( struct process
*process
, struct object
*obj
, struct thread
*thread
, client_ptr_t iosb
)
416 LIST_FOR_EACH_ENTRY( async
, &process
->asyncs
, struct async
, process_entry
)
418 if (async
->status
== STATUS_CANCELLED
) continue;
419 if ((!obj
|| (async
->fd
&& get_fd_user( async
->fd
) == obj
)) &&
420 (!thread
|| async
->thread
== thread
) &&
421 (!iosb
|| async
->data
.iosb
== iosb
))
423 async_terminate( async
, STATUS_CANCELLED
);
431 void cancel_process_asyncs( struct process
*process
)
433 cancel_async( process
, NULL
, NULL
, 0 );
436 /* wake up async operations on the queue */
437 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
439 struct list
*ptr
, *next
;
441 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
443 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
444 async_terminate( async
, status
);
445 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */
449 static void iosb_dump( struct object
*obj
, int verbose
);
450 static void iosb_destroy( struct object
*obj
);
452 static const struct object_ops iosb_ops
=
454 sizeof(struct iosb
), /* size */
455 iosb_dump
, /* dump */
456 no_get_type
, /* get_type */
457 no_add_queue
, /* add_queue */
458 NULL
, /* remove_queue */
460 NULL
, /* satisfied */
461 no_signal
, /* signal */
462 no_get_fd
, /* get_fd */
463 no_map_access
, /* map_access */
464 default_get_sd
, /* get_sd */
465 default_set_sd
, /* set_sd */
466 no_lookup_name
, /* lookup_name */
467 no_link_name
, /* link_name */
468 NULL
, /* unlink_name */
469 no_open_file
, /* open_file */
470 no_close_handle
, /* close_handle */
471 iosb_destroy
/* destroy */
474 static void iosb_dump( struct object
*obj
, int verbose
)
476 assert( obj
->ops
== &iosb_ops
);
477 fprintf( stderr
, "I/O status block\n" );
480 static void iosb_destroy( struct object
*obj
)
482 struct iosb
*iosb
= (struct iosb
*)obj
;
484 free( iosb
->in_data
);
485 free( iosb
->out_data
);
488 /* allocate iosb struct */
489 struct iosb
*create_iosb( const void *in_data
, data_size_t in_size
, data_size_t out_size
)
493 if (!(iosb
= alloc_object( &iosb_ops
))) return NULL
;
495 iosb
->status
= STATUS_PENDING
;
497 iosb
->in_size
= in_size
;
498 iosb
->in_data
= NULL
;
499 iosb
->out_size
= out_size
;
500 iosb
->out_data
= NULL
;
502 if (in_size
&& !(iosb
->in_data
= memdup( in_data
, in_size
)))
504 release_object( iosb
);
511 struct iosb
*async_get_iosb( struct async
*async
)
513 return async
->iosb
? (struct iosb
*)grab_object( async
->iosb
) : NULL
;
516 int async_is_blocking( struct async
*async
)
518 return !async
->event
&& !async
->data
.apc
&& !async
->data
.apc_context
;
521 /* find the first pending async in queue */
522 struct async
*find_pending_async( struct async_queue
*queue
)
525 LIST_FOR_EACH_ENTRY( async
, &queue
->queue
, struct async
, queue_entry
)
526 if (async
->status
== STATUS_PENDING
) return (struct async
*)grab_object( async
);
530 /* cancels all async I/O */
531 DECL_HANDLER(cancel_async
)
533 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
534 struct thread
*thread
= req
->only_thread
? current
: NULL
;
538 int count
= cancel_async( current
->process
, obj
, thread
, req
->iosb
);
539 if (!count
&& req
->iosb
) set_error( STATUS_NOT_FOUND
);
540 release_object( obj
);
544 /* get async result from associated iosb */
545 DECL_HANDLER(get_async_result
)
547 struct iosb
*iosb
= NULL
;
550 LIST_FOR_EACH_ENTRY( async
, ¤t
->process
->asyncs
, struct async
, process_entry
)
551 if (async
->data
.user
== req
->user_arg
)
559 set_error( STATUS_INVALID_PARAMETER
);
565 data_size_t size
= min( iosb
->out_size
, get_reply_max_size() );
568 set_reply_data_ptr( iosb
->out_data
, size
);
569 iosb
->out_data
= NULL
;
572 reply
->size
= iosb
->result
;
573 set_error( iosb
->status
);