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 unsigned int status
; /* current status */
45 struct timeout_user
*timeout
;
46 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 */
53 static void async_dump( struct object
*obj
, int verbose
);
54 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
55 static void async_destroy( struct object
*obj
);
57 static const struct object_ops async_ops
=
59 sizeof(struct async
), /* size */
60 async_dump
, /* dump */
61 no_get_type
, /* get_type */
62 add_queue
, /* add_queue */
63 remove_queue
, /* remove_queue */
64 async_signaled
, /* signaled */
65 no_satisfied
, /* satisfied */
66 no_signal
, /* signal */
67 no_get_fd
, /* get_fd */
68 no_map_access
, /* map_access */
69 default_get_sd
, /* get_sd */
70 default_set_sd
, /* set_sd */
71 no_lookup_name
, /* lookup_name */
72 no_link_name
, /* link_name */
73 NULL
, /* unlink_name */
74 no_open_file
, /* open_file */
75 no_close_handle
, /* close_handle */
76 async_destroy
/* destroy */
82 struct object obj
; /* object header */
83 struct fd
*fd
; /* file descriptor owning this queue */
84 struct completion
*completion
; /* completion associated with a recently closed file descriptor */
85 apc_param_t comp_key
; /* completion key associated with a recently closed file descriptor */
86 struct list queue
; /* queue of async objects */
89 static void async_queue_dump( struct object
*obj
, int verbose
);
90 static void async_queue_destroy( struct object
*obj
);
92 static const struct object_ops async_queue_ops
=
94 sizeof(struct async_queue
), /* size */
95 async_queue_dump
, /* dump */
96 no_get_type
, /* get_type */
97 no_add_queue
, /* add_queue */
98 NULL
, /* remove_queue */
100 NULL
, /* satisfied */
101 no_signal
, /* signal */
102 no_get_fd
, /* get_fd */
103 no_map_access
, /* map_access */
104 default_get_sd
, /* get_sd */
105 default_set_sd
, /* set_sd */
106 no_lookup_name
, /* lookup_name */
107 no_link_name
, /* link_name */
108 NULL
, /* unlink_name */
109 no_open_file
, /* open_file */
110 no_close_handle
, /* close_handle */
111 async_queue_destroy
/* destroy */
115 static inline void async_reselect( struct async
*async
)
117 if (async
->queue
->fd
) fd_reselect_async( async
->queue
->fd
, async
->queue
);
120 static void async_dump( struct object
*obj
, int verbose
)
122 struct async
*async
= (struct async
*)obj
;
123 assert( obj
->ops
== &async_ops
);
124 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
127 static int async_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
129 struct async
*async
= (struct async
*)obj
;
130 assert( obj
->ops
== &async_ops
);
131 return async
->signaled
;
134 static void async_destroy( struct object
*obj
)
136 struct async
*async
= (struct async
*)obj
;
137 assert( obj
->ops
== &async_ops
);
139 list_remove( &async
->process_entry
);
140 list_remove( &async
->queue_entry
);
141 async_reselect( async
);
143 if (async
->timeout
) remove_timeout_user( async
->timeout
);
144 if (async
->event
) release_object( async
->event
);
145 if (async
->iosb
) release_object( async
->iosb
);
146 release_object( async
->queue
);
147 release_object( async
->thread
);
150 static void async_queue_dump( struct object
*obj
, int verbose
)
152 struct async_queue
*async_queue
= (struct async_queue
*)obj
;
153 assert( obj
->ops
== &async_queue_ops
);
154 fprintf( stderr
, "Async queue fd=%p\n", async_queue
->fd
);
157 static void async_queue_destroy( struct object
*obj
)
159 struct async_queue
*async_queue
= (struct async_queue
*)obj
;
160 assert( obj
->ops
== &async_queue_ops
);
161 if (async_queue
->completion
) release_object( async_queue
->completion
);
164 /* notifies client thread of new status of its async request */
165 void async_terminate( struct async
*async
, unsigned int status
)
167 assert( status
!= STATUS_PENDING
);
169 if (async
->status
!= STATUS_PENDING
)
171 /* already terminated, just update status */
172 async
->status
= status
;
176 async
->status
= status
;
177 if (async
->iosb
&& async
->iosb
->status
== STATUS_PENDING
) async
->iosb
->status
= status
;
179 if (async
->data
.callback
)
183 memset( &data
, 0, sizeof(data
) );
184 data
.type
= APC_ASYNC_IO
;
185 data
.async_io
.func
= async
->data
.callback
;
186 data
.async_io
.user
= async
->data
.arg
;
187 data
.async_io
.sb
= async
->data
.iosb
;
188 data
.async_io
.status
= status
;
189 thread_queue_apc( async
->thread
, &async
->obj
, &data
);
191 else async_set_result( &async
->obj
, STATUS_SUCCESS
, 0, 0, 0 );
193 async_reselect( async
);
194 release_object( async
); /* so that it gets destroyed when the async is done */
197 /* callback for timeout on an async request */
198 static void async_timeout( void *private )
200 struct async
*async
= private;
202 async
->timeout
= NULL
;
203 async_terminate( async
, async
->timeout_status
);
206 /* create a new async queue for a given fd */
207 struct async_queue
*create_async_queue( struct fd
*fd
)
209 struct async_queue
*queue
= alloc_object( &async_queue_ops
);
214 queue
->completion
= NULL
;
215 list_init( &queue
->queue
);
220 /* free an async queue, cancelling all async operations */
221 void free_async_queue( struct async_queue
*queue
)
224 if (queue
->fd
) queue
->completion
= fd_get_completion( queue
->fd
, &queue
->comp_key
);
226 async_wake_up( queue
, STATUS_HANDLES_CLOSED
);
227 release_object( queue
);
230 /* create an async on a given queue of a fd */
231 struct async
*create_async( struct thread
*thread
, struct async_queue
*queue
, const async_data_t
*data
,
234 struct event
*event
= NULL
;
237 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
240 if (!(async
= alloc_object( &async_ops
)))
242 if (event
) release_object( event
);
246 async
->thread
= (struct thread
*)grab_object( thread
);
247 async
->event
= event
;
248 async
->status
= STATUS_PENDING
;
250 async
->timeout
= NULL
;
251 async
->queue
= (struct async_queue
*)grab_object( queue
);
254 if (iosb
) async
->iosb
= (struct iosb
*)grab_object( iosb
);
255 else async
->iosb
= NULL
;
257 list_add_tail( &queue
->queue
, &async
->queue_entry
);
258 list_add_tail( &thread
->process
->asyncs
, &async
->process_entry
);
259 grab_object( async
);
261 if (queue
->fd
) set_fd_signaled( queue
->fd
, 0 );
262 if (event
) reset_event( event
);
266 /* set the timeout of an async operation */
267 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
269 if (async
->timeout
) remove_timeout_user( async
->timeout
);
270 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
271 else async
->timeout
= NULL
;
272 async
->timeout_status
= status
;
275 static void add_async_completion( struct async_queue
*queue
, apc_param_t cvalue
, unsigned int status
,
276 apc_param_t information
)
281 struct completion
*completion
= fd_get_completion( queue
->fd
, &ckey
);
285 add_completion( completion
, ckey
, cvalue
, status
, information
);
286 release_object( completion
);
289 else if (queue
->completion
) add_completion( queue
->completion
, queue
->comp_key
,
290 cvalue
, status
, information
);
293 /* store the result of the client-side async callback */
294 void async_set_result( struct object
*obj
, unsigned int status
, apc_param_t total
,
295 client_ptr_t apc
, client_ptr_t apc_arg
)
297 struct async
*async
= (struct async
*)obj
;
299 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
301 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
303 if (status
== STATUS_PENDING
) /* restart it */
305 status
= async
->status
;
306 async
->status
= STATUS_PENDING
;
307 grab_object( async
);
309 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
310 async_terminate( async
, status
);
312 async_reselect( async
);
316 if (async
->timeout
) remove_timeout_user( async
->timeout
);
317 async
->timeout
= NULL
;
318 async
->status
= status
;
319 if (status
== STATUS_MORE_PROCESSING_REQUIRED
) return; /* don't report the completion */
321 if (async
->data
.cvalue
) add_async_completion( async
->queue
, async
->data
.cvalue
, status
, total
);
325 memset( &data
, 0, sizeof(data
) );
326 data
.type
= APC_USER
;
327 data
.user
.func
= apc
;
328 data
.user
.args
[0] = apc_arg
;
329 data
.user
.args
[1] = async
->data
.iosb
;
330 data
.user
.args
[2] = 0;
331 thread_queue_apc( async
->thread
, NULL
, &data
);
333 if (async
->event
) set_event( async
->event
);
334 else if (async
->queue
->fd
) set_fd_signaled( async
->queue
->fd
, 1 );
336 wake_up( &async
->obj
, 0 );
340 /* check if there are any queued async operations */
341 int async_queued( struct async_queue
*queue
)
343 return queue
&& list_head( &queue
->queue
);
346 /* check if an async operation is waiting to be alerted */
347 int async_waiting( struct async_queue
*queue
)
352 if (!queue
) return 0;
353 if (!(ptr
= list_head( &queue
->queue
))) return 0;
354 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
355 return async
->status
== STATUS_PENDING
;
358 static int cancel_async( struct process
*process
, struct object
*obj
, struct thread
*thread
, client_ptr_t iosb
)
364 LIST_FOR_EACH_ENTRY( async
, &process
->asyncs
, struct async
, process_entry
)
366 if (async
->status
== STATUS_CANCELLED
) continue;
367 if ((!obj
|| (async
->queue
->fd
&& get_fd_user( async
->queue
->fd
) == obj
)) &&
368 (!thread
|| async
->thread
== thread
) &&
369 (!iosb
|| async
->data
.iosb
== iosb
))
371 async_terminate( async
, STATUS_CANCELLED
);
379 void cancel_process_asyncs( struct process
*process
)
381 cancel_async( process
, NULL
, NULL
, 0 );
384 /* wake up async operations on the queue */
385 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
387 struct list
*ptr
, *next
;
391 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
393 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
394 async_terminate( async
, status
);
395 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */
399 static void iosb_dump( struct object
*obj
, int verbose
);
400 static void iosb_destroy( struct object
*obj
);
402 static const struct object_ops iosb_ops
=
404 sizeof(struct iosb
), /* size */
405 iosb_dump
, /* dump */
406 no_get_type
, /* get_type */
407 no_add_queue
, /* add_queue */
408 NULL
, /* remove_queue */
410 NULL
, /* satisfied */
411 no_signal
, /* signal */
412 no_get_fd
, /* get_fd */
413 no_map_access
, /* map_access */
414 default_get_sd
, /* get_sd */
415 default_set_sd
, /* set_sd */
416 no_lookup_name
, /* lookup_name */
417 no_link_name
, /* link_name */
418 NULL
, /* unlink_name */
419 no_open_file
, /* open_file */
420 no_close_handle
, /* close_handle */
421 iosb_destroy
/* destroy */
424 static void iosb_dump( struct object
*obj
, int verbose
)
426 assert( obj
->ops
== &iosb_ops
);
427 fprintf( stderr
, "I/O status block\n" );
430 static void iosb_destroy( struct object
*obj
)
432 struct iosb
*iosb
= (struct iosb
*)obj
;
434 free( iosb
->in_data
);
435 free( iosb
->out_data
);
438 /* allocate iosb struct */
439 struct iosb
*create_iosb( const void *in_data
, data_size_t in_size
, data_size_t out_size
)
443 if (!(iosb
= alloc_object( &iosb_ops
))) return NULL
;
445 iosb
->status
= STATUS_PENDING
;
447 iosb
->in_size
= in_size
;
448 iosb
->in_data
= NULL
;
449 iosb
->out_size
= out_size
;
450 iosb
->out_data
= NULL
;
452 if (in_size
&& !(iosb
->in_data
= memdup( in_data
, in_size
)))
454 release_object( iosb
);
461 /* cancels all async I/O */
462 DECL_HANDLER(cancel_async
)
464 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
465 struct thread
*thread
= req
->only_thread
? current
: NULL
;
469 int count
= cancel_async( current
->process
, obj
, thread
, req
->iosb
);
470 if (!count
&& req
->iosb
) set_error( STATUS_NOT_FOUND
);
471 release_object( obj
);
475 /* get async result from associated iosb */
476 DECL_HANDLER(get_async_result
)
478 struct iosb
*iosb
= NULL
;
481 LIST_FOR_EACH_ENTRY( async
, ¤t
->process
->asyncs
, struct async
, process_entry
)
482 if (async
->data
.arg
== req
->user_arg
)
490 set_error( STATUS_INVALID_PARAMETER
);
496 data_size_t size
= min( iosb
->out_size
, get_reply_max_size() );
499 set_reply_data_ptr( iosb
->out_data
, size
);
500 iosb
->out_data
= NULL
;
503 reply
->size
= iosb
->result
;
504 set_error( iosb
->status
);