dwrite: Check for vertical variants only when asked.
[wine/zf.git] / server / async.c
blob1ac5117edb905521514d68fa24e8786c2abd0697
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 struct event *event;
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 */
70 &no_type, /* type */
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 /* close wait handle here to avoid extra server round trip */
122 if (async->wait_handle)
124 close_handle( async->thread->process, async->wait_handle );
125 async->wait_handle = 0;
128 if (async->status == STATUS_PENDING) make_wait_abandoned( entry );
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 );
138 if (async->queue)
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)
159 /* already terminated, just update status */
160 async->status = status;
161 return;
164 async->status = status;
165 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
167 if (!async->direct_result)
169 if (async->data.user)
171 apc_call_t data;
173 memset( &data, 0, sizeof(data) );
174 data.type = APC_ASYNC_IO;
175 data.async_io.user = async->data.user;
176 data.async_io.sb = async->data.iosb;
177 data.async_io.status = status;
178 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
180 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
183 async_reselect( async );
184 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
187 /* callback for timeout on an async request */
188 static void async_timeout( void *private )
190 struct async *async = private;
192 async->timeout = NULL;
193 async_terminate( async, async->timeout_status );
196 /* free an async queue, cancelling all async operations */
197 void free_async_queue( struct async_queue *queue )
199 struct async *async, *next;
201 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
203 grab_object( &async->obj );
204 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
205 async->fd = NULL;
206 async_terminate( async, STATUS_HANDLES_CLOSED );
207 async->queue = NULL;
208 release_object( &async->obj );
212 void queue_async( struct async_queue *queue, struct async *async )
214 /* fd will be set to NULL in free_async_queue when fd is destroyed */
215 release_object( async->fd );
217 async->queue = queue;
218 grab_object( async );
219 list_add_tail( &queue->queue, &async->queue_entry );
221 set_fd_signaled( async->fd, 0 );
224 /* create an async on a given queue of a fd */
225 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
227 struct event *event = NULL;
228 struct async *async;
230 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
231 return NULL;
233 if (!(async = alloc_object( &async_ops )))
235 if (event) release_object( event );
236 return NULL;
239 async->thread = (struct thread *)grab_object( thread );
240 async->event = event;
241 async->status = STATUS_PENDING;
242 async->data = *data;
243 async->timeout = NULL;
244 async->queue = NULL;
245 async->fd = (struct fd *)grab_object( fd );
246 async->signaled = 0;
247 async->pending = 1;
248 async->wait_handle = 0;
249 async->direct_result = 0;
250 async->completion = fd_get_completion( fd, &async->comp_key );
251 async->comp_flags = 0;
252 async->completion_callback = NULL;
253 async->completion_callback_private = NULL;
255 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
256 else async->iosb = NULL;
258 list_add_head( &thread->process->asyncs, &async->process_entry );
259 if (event) reset_event( event );
261 if (async->completion && data->apc)
263 release_object( async );
264 set_error( STATUS_INVALID_PARAMETER );
265 return NULL;
268 return async;
271 void set_async_pending( struct async *async, int signal )
273 if (async->status == STATUS_PENDING)
275 async->pending = 1;
276 if (signal && !async->signaled)
278 async->signaled = 1;
279 wake_up( &async->obj, 0 );
284 /* create an async associated with iosb for async-based requests
285 * returned async must be passed to async_handoff */
286 struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
288 struct async *async;
289 struct iosb *iosb;
291 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
292 return NULL;
294 async = create_async( fd, current, data, iosb );
295 release_object( iosb );
296 if (async)
298 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
300 release_object( async );
301 return NULL;
303 async->pending = 0;
304 async->direct_result = 1;
305 async->comp_flags = comp_flags;
307 return async;
310 /* return async object status and wait handle to client */
311 obj_handle_t async_handoff( struct async *async, int success, data_size_t *result, int force_blocking )
313 if (!success)
315 if (get_error() == STATUS_PENDING)
317 /* we don't know the result yet, so client needs to wait */
318 async->direct_result = 0;
319 return async->wait_handle;
321 close_handle( async->thread->process, async->wait_handle );
322 async->wait_handle = 0;
323 return 0;
326 if (get_error() != STATUS_PENDING)
328 /* status and data are already set and returned */
329 async_terminate( async, get_error() );
331 else if (async->iosb->status != STATUS_PENDING)
333 /* result is already available in iosb, return it */
334 if (async->iosb->out_data)
336 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
337 async->iosb->out_data = NULL;
341 if (async->iosb->status != STATUS_PENDING)
343 if (result) *result = async->iosb->result;
344 async->signaled = 1;
346 else
348 async->direct_result = 0;
349 async->pending = 1;
350 if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
352 close_handle( async->thread->process, async->wait_handle);
353 async->wait_handle = 0;
356 set_error( async->iosb->status );
357 return async->wait_handle;
360 /* set the timeout of an async operation */
361 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
363 if (async->timeout) remove_timeout_user( async->timeout );
364 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
365 else async->timeout = NULL;
366 async->timeout_status = status;
369 /* set a callback to be notified when the async is completed */
370 void async_set_completion_callback( struct async *async, async_completion_callback func, void *private )
372 async->completion_callback = func;
373 async->completion_callback_private = private;
376 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
377 apc_param_t information )
379 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
380 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
383 /* store the result of the client-side async callback */
384 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
386 struct async *async = (struct async *)obj;
388 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
390 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
392 if (status == STATUS_PENDING) /* restart it */
394 status = async->status;
395 async->status = STATUS_PENDING;
396 grab_object( async );
398 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
399 async_terminate( async, status );
400 else
401 async_reselect( async );
403 else
405 if (async->timeout) remove_timeout_user( async->timeout );
406 async->timeout = NULL;
407 async->status = status;
408 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
410 if (async->data.apc)
412 apc_call_t data;
413 memset( &data, 0, sizeof(data) );
414 data.type = APC_USER;
415 data.user.user.func = async->data.apc;
416 data.user.user.args[0] = async->data.apc_context;
417 data.user.user.args[1] = async->data.iosb;
418 data.user.user.args[2] = 0;
419 thread_queue_apc( NULL, async->thread, NULL, &data );
421 else if (async->data.apc_context && (async->pending ||
422 !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
424 add_async_completion( async, async->data.apc_context, status, total );
427 if (async->event) set_event( async->event );
428 else if (async->fd) set_fd_signaled( async->fd, 1 );
429 if (!async->signaled)
431 async->signaled = 1;
432 wake_up( &async->obj, 0 );
435 if (async->completion_callback)
436 async->completion_callback( async->completion_callback_private );
437 async->completion_callback = NULL;
441 /* check if an async operation is waiting to be alerted */
442 int async_waiting( struct async_queue *queue )
444 struct list *ptr;
445 struct async *async;
447 if (!(ptr = list_head( &queue->queue ))) return 0;
448 async = LIST_ENTRY( ptr, struct async, queue_entry );
449 return async->status == STATUS_PENDING;
452 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
454 struct async *async;
455 int woken = 0;
457 restart:
458 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
460 if (async->status == STATUS_CANCELLED) continue;
461 if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
462 (!thread || async->thread == thread) &&
463 (!iosb || async->data.iosb == iosb))
465 async_terminate( async, STATUS_CANCELLED );
466 woken++;
467 goto restart;
470 return woken;
473 void cancel_process_asyncs( struct process *process )
475 cancel_async( process, NULL, NULL, 0 );
478 /* wake up async operations on the queue */
479 void async_wake_up( struct async_queue *queue, unsigned int status )
481 struct list *ptr, *next;
483 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
485 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
486 async_terminate( async, status );
487 if (status == STATUS_ALERTED) break; /* only wake up the first one */
491 static void iosb_dump( struct object *obj, int verbose );
492 static void iosb_destroy( struct object *obj );
494 static const struct object_ops iosb_ops =
496 sizeof(struct iosb), /* size */
497 &no_type, /* type */
498 iosb_dump, /* dump */
499 no_add_queue, /* add_queue */
500 NULL, /* remove_queue */
501 NULL, /* signaled */
502 NULL, /* satisfied */
503 no_signal, /* signal */
504 no_get_fd, /* get_fd */
505 default_map_access, /* map_access */
506 default_get_sd, /* get_sd */
507 default_set_sd, /* set_sd */
508 no_get_full_name, /* get_full_name */
509 no_lookup_name, /* lookup_name */
510 no_link_name, /* link_name */
511 NULL, /* unlink_name */
512 no_open_file, /* open_file */
513 no_kernel_obj_list, /* get_kernel_obj_list */
514 no_close_handle, /* close_handle */
515 iosb_destroy /* destroy */
518 static void iosb_dump( struct object *obj, int verbose )
520 assert( obj->ops == &iosb_ops );
521 fprintf( stderr, "I/O status block\n" );
524 static void iosb_destroy( struct object *obj )
526 struct iosb *iosb = (struct iosb *)obj;
528 free( iosb->in_data );
529 free( iosb->out_data );
532 /* allocate iosb struct */
533 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
535 struct iosb *iosb;
537 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
539 iosb->status = STATUS_PENDING;
540 iosb->result = 0;
541 iosb->in_size = in_size;
542 iosb->in_data = NULL;
543 iosb->out_size = out_size;
544 iosb->out_data = NULL;
546 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
548 release_object( iosb );
549 iosb = NULL;
552 return iosb;
555 struct iosb *async_get_iosb( struct async *async )
557 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
560 struct thread *async_get_thread( struct async *async )
562 return async->thread;
565 int async_is_blocking( struct async *async )
567 return !async->event && !async->data.apc && !async->data.apc_context;
570 /* find the first pending async in queue */
571 struct async *find_pending_async( struct async_queue *queue )
573 struct async *async;
574 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
575 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
576 return NULL;
579 /* cancels all async I/O */
580 DECL_HANDLER(cancel_async)
582 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
583 struct thread *thread = req->only_thread ? current : NULL;
585 if (obj)
587 int count = cancel_async( current->process, obj, thread, req->iosb );
588 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
589 release_object( obj );
593 /* get async result from associated iosb */
594 DECL_HANDLER(get_async_result)
596 struct iosb *iosb = NULL;
597 struct async *async;
599 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
600 if (async->data.user == req->user_arg)
602 iosb = async->iosb;
603 break;
606 if (!iosb)
608 set_error( STATUS_INVALID_PARAMETER );
609 return;
612 if (iosb->out_data)
614 data_size_t size = min( iosb->out_size, get_reply_max_size() );
615 if (size)
617 set_reply_data_ptr( iosb->out_data, size );
618 iosb->out_data = NULL;
621 reply->size = iosb->result;
622 set_error( iosb->status );