include: Fix ASM_CFI definition.
[wine.git] / server / async.c
blobb88dd168075e4172f0a9c229354e53a7063cfd57
1 /*
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
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
31 #include "object.h"
32 #include "file.h"
33 #include "request.h"
34 #include "process.h"
35 #include "handle.h"
37 struct async
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 */
48 int signaled;
49 struct event *event;
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_kernel_obj_list, /* get_kernel_obj_list */
83 no_close_handle, /* close_handle */
84 async_destroy /* destroy */
87 static inline void async_reselect( struct async *async )
89 if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
92 static void async_dump( struct object *obj, int verbose )
94 struct async *async = (struct async *)obj;
95 assert( obj->ops == &async_ops );
96 fprintf( stderr, "Async thread=%p\n", async->thread );
99 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
101 struct async *async = (struct async *)obj;
102 assert( obj->ops == &async_ops );
103 return async->signaled;
106 static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
108 struct async *async = (struct async *)obj;
109 assert( obj->ops == &async_ops );
111 if (async->direct_result)
113 async_set_result( &async->obj, async->iosb->status, async->iosb->result );
114 async->direct_result = 0;
117 /* close wait handle here to avoid extra server round trip */
118 if (async->wait_handle)
120 close_handle( async->thread->process, async->wait_handle );
121 async->wait_handle = 0;
125 static void async_destroy( struct object *obj )
127 struct async *async = (struct async *)obj;
128 assert( obj->ops == &async_ops );
130 list_remove( &async->process_entry );
132 if (async->queue)
134 list_remove( &async->queue_entry );
135 async_reselect( async );
137 else if (async->fd) release_object( async->fd );
139 if (async->timeout) remove_timeout_user( async->timeout );
140 if (async->completion) release_object( async->completion );
141 if (async->event) release_object( async->event );
142 if (async->iosb) release_object( async->iosb );
143 release_object( async->thread );
146 /* notifies client thread of new status of its async request */
147 void async_terminate( struct async *async, unsigned int status )
149 assert( status != STATUS_PENDING );
151 if (async->status != STATUS_PENDING)
153 /* already terminated, just update status */
154 async->status = status;
155 return;
158 async->status = status;
159 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
161 if (!async->direct_result)
163 if (async->data.user)
165 apc_call_t data;
167 memset( &data, 0, sizeof(data) );
168 data.type = APC_ASYNC_IO;
169 data.async_io.user = async->data.user;
170 data.async_io.sb = async->data.iosb;
171 data.async_io.status = status;
172 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
174 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
177 async_reselect( async );
178 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
181 /* callback for timeout on an async request */
182 static void async_timeout( void *private )
184 struct async *async = private;
186 async->timeout = NULL;
187 async_terminate( async, async->timeout_status );
190 /* free an async queue, cancelling all async operations */
191 void free_async_queue( struct async_queue *queue )
193 struct async *async, *next;
195 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
197 grab_object( &async->obj );
198 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
199 async->fd = NULL;
200 async_terminate( async, STATUS_HANDLES_CLOSED );
201 async->queue = NULL;
202 release_object( &async->obj );
206 void queue_async( struct async_queue *queue, struct async *async )
208 /* fd will be set to NULL in free_async_queue when fd is destroyed */
209 release_object( async->fd );
211 async->queue = queue;
212 grab_object( async );
213 list_add_tail( &queue->queue, &async->queue_entry );
215 set_fd_signaled( async->fd, 0 );
218 /* create an async on a given queue of a fd */
219 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
221 struct event *event = NULL;
222 struct async *async;
224 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
225 return NULL;
227 if (!(async = alloc_object( &async_ops )))
229 if (event) release_object( event );
230 return NULL;
233 async->thread = (struct thread *)grab_object( thread );
234 async->event = event;
235 async->status = STATUS_PENDING;
236 async->data = *data;
237 async->timeout = NULL;
238 async->queue = NULL;
239 async->fd = (struct fd *)grab_object( fd );
240 async->signaled = 0;
241 async->wait_handle = 0;
242 async->direct_result = 0;
243 async->completion = fd_get_completion( fd, &async->comp_key );
244 async->comp_flags = 0;
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 );
256 return NULL;
259 return async;
262 /* create an async associated with iosb for async-based requests
263 * returned async must be passed to async_handoff */
264 struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
266 struct async *async;
267 struct iosb *iosb;
269 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
270 return NULL;
272 async = create_async( fd, current, data, iosb );
273 release_object( iosb );
274 if (async)
276 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
278 release_object( async );
279 return NULL;
281 async->direct_result = 1;
282 async->comp_flags = comp_flags;
284 return async;
287 /* return async object status and wait handle to client */
288 obj_handle_t async_handoff( struct async *async, int success, data_size_t *result, int force_blocking )
290 if (!success)
292 close_handle( async->thread->process, async->wait_handle );
293 async->wait_handle = 0;
294 return 0;
297 if (get_error() != STATUS_PENDING)
299 /* status and data are already set and returned */
300 async_terminate( async, get_error() );
302 else if (async->iosb->status != STATUS_PENDING)
304 /* result is already available in iosb, return it */
305 if (async->iosb->out_data)
307 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
308 async->iosb->out_data = NULL;
312 if (async->iosb->status != STATUS_PENDING)
314 if (result) *result = async->iosb->result;
315 async->signaled = 1;
317 else
319 async->direct_result = 0;
320 if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
322 close_handle( async->thread->process, async->wait_handle);
323 async->wait_handle = 0;
326 set_error( async->iosb->status );
327 return async->wait_handle;
330 /* set the timeout of an async operation */
331 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
333 if (async->timeout) remove_timeout_user( async->timeout );
334 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
335 else async->timeout = NULL;
336 async->timeout_status = status;
339 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
340 apc_param_t information )
342 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
343 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
346 /* store the result of the client-side async callback */
347 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
349 struct async *async = (struct async *)obj;
351 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
353 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
355 if (status == STATUS_PENDING) /* restart it */
357 status = async->status;
358 async->status = STATUS_PENDING;
359 grab_object( async );
361 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
362 async_terminate( async, status );
363 else
364 async_reselect( async );
366 else
368 if (async->timeout) remove_timeout_user( async->timeout );
369 async->timeout = NULL;
370 async->status = status;
371 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
373 if (async->data.apc)
375 apc_call_t data;
376 memset( &data, 0, sizeof(data) );
377 data.type = APC_USER;
378 data.user.func = async->data.apc;
379 data.user.args[0] = async->data.apc_context;
380 data.user.args[1] = async->data.iosb;
381 data.user.args[2] = 0;
382 thread_queue_apc( NULL, async->thread, NULL, &data );
384 else if (async->data.apc_context && (!async->direct_result ||
385 !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
387 add_async_completion( async, async->data.apc_context, status, total );
390 if (async->event) set_event( async->event );
391 else if (async->fd) set_fd_signaled( async->fd, 1 );
392 if (!async->signaled)
394 async->signaled = 1;
395 wake_up( &async->obj, 0 );
400 /* check if an async operation is waiting to be alerted */
401 int async_waiting( struct async_queue *queue )
403 struct list *ptr;
404 struct async *async;
406 if (!(ptr = list_head( &queue->queue ))) return 0;
407 async = LIST_ENTRY( ptr, struct async, queue_entry );
408 return async->status == STATUS_PENDING;
411 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
413 struct async *async;
414 int woken = 0;
416 restart:
417 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
419 if (async->status == STATUS_CANCELLED) continue;
420 if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
421 (!thread || async->thread == thread) &&
422 (!iosb || async->data.iosb == iosb))
424 async_terminate( async, STATUS_CANCELLED );
425 woken++;
426 goto restart;
429 return woken;
432 void cancel_process_asyncs( struct process *process )
434 cancel_async( process, NULL, NULL, 0 );
437 /* wake up async operations on the queue */
438 void async_wake_up( struct async_queue *queue, unsigned int status )
440 struct list *ptr, *next;
442 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
444 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
445 async_terminate( async, status );
446 if (status == STATUS_ALERTED) break; /* only wake up the first one */
450 static void iosb_dump( struct object *obj, int verbose );
451 static void iosb_destroy( struct object *obj );
453 static const struct object_ops iosb_ops =
455 sizeof(struct iosb), /* size */
456 iosb_dump, /* dump */
457 no_get_type, /* get_type */
458 no_add_queue, /* add_queue */
459 NULL, /* remove_queue */
460 NULL, /* signaled */
461 NULL, /* satisfied */
462 no_signal, /* signal */
463 no_get_fd, /* get_fd */
464 no_map_access, /* map_access */
465 default_get_sd, /* get_sd */
466 default_set_sd, /* set_sd */
467 no_lookup_name, /* lookup_name */
468 no_link_name, /* link_name */
469 NULL, /* unlink_name */
470 no_open_file, /* open_file */
471 no_kernel_obj_list, /* get_kernel_obj_list */
472 no_close_handle, /* close_handle */
473 iosb_destroy /* destroy */
476 static void iosb_dump( struct object *obj, int verbose )
478 assert( obj->ops == &iosb_ops );
479 fprintf( stderr, "I/O status block\n" );
482 static void iosb_destroy( struct object *obj )
484 struct iosb *iosb = (struct iosb *)obj;
486 free( iosb->in_data );
487 free( iosb->out_data );
490 /* allocate iosb struct */
491 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
493 struct iosb *iosb;
495 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
497 iosb->status = STATUS_PENDING;
498 iosb->result = 0;
499 iosb->in_size = in_size;
500 iosb->in_data = NULL;
501 iosb->out_size = out_size;
502 iosb->out_data = NULL;
504 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
506 release_object( iosb );
507 iosb = NULL;
510 return iosb;
513 struct iosb *async_get_iosb( struct async *async )
515 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
518 int async_is_blocking( struct async *async )
520 return !async->event && !async->data.apc && !async->data.apc_context;
523 /* find the first pending async in queue */
524 struct async *find_pending_async( struct async_queue *queue )
526 struct async *async;
527 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
528 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
529 return NULL;
532 /* cancels all async I/O */
533 DECL_HANDLER(cancel_async)
535 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
536 struct thread *thread = req->only_thread ? current : NULL;
538 if (obj)
540 int count = cancel_async( current->process, obj, thread, req->iosb );
541 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
542 release_object( obj );
546 /* get async result from associated iosb */
547 DECL_HANDLER(get_async_result)
549 struct iosb *iosb = NULL;
550 struct async *async;
552 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
553 if (async->data.user == req->user_arg)
555 iosb = async->iosb;
556 break;
559 if (!iosb)
561 set_error( STATUS_INVALID_PARAMETER );
562 return;
565 if (iosb->out_data)
567 data_size_t size = min( iosb->out_size, get_reply_max_size() );
568 if (size)
570 set_reply_data_ptr( iosb->out_data, size );
571 iosb->out_data = NULL;
574 reply->size = iosb->result;
575 set_error( iosb->status );