wined3d: Avoid raw blits when resolving multisample textures.
[wine.git] / server / async.c
blob4f1bcf15f4232c387560d1bcced2df9fec0039a0
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 */
58 static void async_dump( struct object *obj, int verbose );
59 static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
60 static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
61 static void async_destroy( struct object *obj );
63 static const struct object_ops async_ops =
65 sizeof(struct async), /* size */
66 async_dump, /* dump */
67 no_get_type, /* get_type */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 async_signaled, /* signaled */
71 async_satisfied, /* satisfied */
72 no_signal, /* signal */
73 no_get_fd, /* get_fd */
74 no_map_access, /* map_access */
75 default_get_sd, /* get_sd */
76 default_set_sd, /* set_sd */
77 no_lookup_name, /* lookup_name */
78 no_link_name, /* link_name */
79 NULL, /* unlink_name */
80 no_open_file, /* open_file */
81 no_close_handle, /* close_handle */
82 async_destroy /* destroy */
85 static inline void async_reselect( struct async *async )
87 if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
90 static void async_dump( struct object *obj, int verbose )
92 struct async *async = (struct async *)obj;
93 assert( obj->ops == &async_ops );
94 fprintf( stderr, "Async thread=%p\n", async->thread );
97 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
99 struct async *async = (struct async *)obj;
100 assert( obj->ops == &async_ops );
101 return async->signaled;
104 static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
106 struct async *async = (struct async *)obj;
107 assert( obj->ops == &async_ops );
109 if (async->direct_result)
111 async_set_result( &async->obj, async->iosb->status, async->iosb->result );
112 async->direct_result = 0;
115 /* close wait handle here to avoid extra server round trip */
116 if (async->wait_handle)
118 close_handle( async->thread->process, async->wait_handle );
119 async->wait_handle = 0;
123 static void async_destroy( struct object *obj )
125 struct async *async = (struct async *)obj;
126 assert( obj->ops == &async_ops );
128 list_remove( &async->process_entry );
130 if (async->queue)
132 list_remove( &async->queue_entry );
133 async_reselect( async );
135 else if (async->fd) release_object( async->fd );
137 if (async->timeout) remove_timeout_user( async->timeout );
138 if (async->completion) release_object( async->completion );
139 if (async->event) release_object( async->event );
140 if (async->iosb) release_object( async->iosb );
141 release_object( async->thread );
144 /* notifies client thread of new status of its async request */
145 void async_terminate( struct async *async, unsigned int status )
147 assert( status != STATUS_PENDING );
149 if (async->status != STATUS_PENDING)
151 /* already terminated, just update status */
152 async->status = status;
153 return;
156 async->status = status;
157 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
159 if (!async->direct_result)
161 if (async->data.user)
163 apc_call_t data;
165 memset( &data, 0, sizeof(data) );
166 data.type = APC_ASYNC_IO;
167 data.async_io.user = async->data.user;
168 data.async_io.sb = async->data.iosb;
169 data.async_io.status = status;
170 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
172 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
175 async_reselect( async );
176 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
179 /* callback for timeout on an async request */
180 static void async_timeout( void *private )
182 struct async *async = private;
184 async->timeout = NULL;
185 async_terminate( async, async->timeout_status );
188 /* free an async queue, cancelling all async operations */
189 void free_async_queue( struct async_queue *queue )
191 struct async *async, *next;
193 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
195 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
196 async->fd = NULL;
197 async_terminate( async, STATUS_HANDLES_CLOSED );
198 async->queue = NULL;
202 void queue_async( struct async_queue *queue, struct async *async )
204 /* fd will be set to NULL in free_async_queue when fd is destroyed */
205 release_object( async->fd );
207 async->queue = queue;
208 grab_object( async );
209 list_add_tail( &queue->queue, &async->queue_entry );
211 set_fd_signaled( async->fd, 0 );
214 /* create an async on a given queue of a fd */
215 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
217 struct event *event = NULL;
218 struct async *async;
220 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
221 return NULL;
223 if (!(async = alloc_object( &async_ops )))
225 if (event) release_object( event );
226 return NULL;
229 async->thread = (struct thread *)grab_object( thread );
230 async->event = event;
231 async->status = STATUS_PENDING;
232 async->data = *data;
233 async->timeout = NULL;
234 async->queue = NULL;
235 async->fd = (struct fd *)grab_object( fd );
236 async->signaled = 0;
237 async->wait_handle = 0;
238 async->direct_result = 0;
239 async->completion = fd_get_completion( fd, &async->comp_key );
241 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
242 else async->iosb = NULL;
244 list_add_head( &thread->process->asyncs, &async->process_entry );
245 if (event) reset_event( event );
247 if (async->completion && data->apc)
249 release_object( async );
250 set_error( STATUS_INVALID_PARAMETER );
251 return NULL;
254 return async;
257 /* create an async associated with iosb for async-based requests
258 * returned async must be passed to async_handoff */
259 struct async *create_request_async( struct fd *fd, const async_data_t *data )
261 struct async *async;
262 struct iosb *iosb;
264 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
265 return NULL;
267 async = create_async( fd, current, data, iosb );
268 release_object( iosb );
269 if (async)
271 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
273 release_object( async );
274 return NULL;
276 async->direct_result = 1;
278 return async;
281 /* return async object status and wait handle to client */
282 obj_handle_t async_handoff( struct async *async, int success, data_size_t *result )
284 if (!success)
286 close_handle( async->thread->process, async->wait_handle );
287 async->wait_handle = 0;
288 return 0;
291 if (get_error() != STATUS_PENDING)
293 /* status and data are already set and returned */
294 async_terminate( async, get_error() );
296 else if (async->iosb->status != STATUS_PENDING)
298 /* result is already available in iosb, return it */
299 if (async->iosb->out_data)
301 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
302 async->iosb->out_data = NULL;
306 if (async->iosb->status != STATUS_PENDING)
308 if (result) *result = async->iosb->result;
309 async->signaled = 1;
311 else
313 async->direct_result = 0;
314 if (!async_is_blocking( async ))
316 close_handle( async->thread->process, async->wait_handle);
317 async->wait_handle = 0;
320 set_error( async->iosb->status );
321 return async->wait_handle;
324 /* set the timeout of an async operation */
325 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
327 if (async->timeout) remove_timeout_user( async->timeout );
328 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
329 else async->timeout = NULL;
330 async->timeout_status = status;
333 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
334 apc_param_t information )
336 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
337 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
340 /* store the result of the client-side async callback */
341 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
343 struct async *async = (struct async *)obj;
345 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
347 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
349 if (status == STATUS_PENDING) /* restart it */
351 status = async->status;
352 async->status = STATUS_PENDING;
353 grab_object( async );
355 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
356 async_terminate( async, status );
357 else
358 async_reselect( async );
360 else
362 if (async->timeout) remove_timeout_user( async->timeout );
363 async->timeout = NULL;
364 async->status = status;
365 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
367 if (async->data.apc)
369 apc_call_t data;
370 memset( &data, 0, sizeof(data) );
371 data.type = APC_USER;
372 data.user.func = async->data.apc;
373 data.user.args[0] = async->data.apc_context;
374 data.user.args[1] = async->data.iosb;
375 data.user.args[2] = 0;
376 thread_queue_apc( NULL, async->thread, NULL, &data );
378 else if (async->data.apc_context)
379 add_async_completion( async, async->data.apc_context, status, total );
381 if (async->event) set_event( async->event );
382 else if (async->fd) set_fd_signaled( async->fd, 1 );
383 if (!async->signaled)
385 async->signaled = 1;
386 wake_up( &async->obj, 0 );
391 /* check if an async operation is waiting to be alerted */
392 int async_waiting( struct async_queue *queue )
394 struct list *ptr;
395 struct async *async;
397 if (!(ptr = list_head( &queue->queue ))) return 0;
398 async = LIST_ENTRY( ptr, struct async, queue_entry );
399 return async->status == STATUS_PENDING;
402 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
404 struct async *async;
405 int woken = 0;
407 restart:
408 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
410 if (async->status == STATUS_CANCELLED) continue;
411 if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
412 (!thread || async->thread == thread) &&
413 (!iosb || async->data.iosb == iosb))
415 async_terminate( async, STATUS_CANCELLED );
416 woken++;
417 goto restart;
420 return woken;
423 void cancel_process_asyncs( struct process *process )
425 cancel_async( process, NULL, NULL, 0 );
428 /* wake up async operations on the queue */
429 void async_wake_up( struct async_queue *queue, unsigned int status )
431 struct list *ptr, *next;
433 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
435 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
436 async_terminate( async, status );
437 if (status == STATUS_ALERTED) break; /* only wake up the first one */
441 static void iosb_dump( struct object *obj, int verbose );
442 static void iosb_destroy( struct object *obj );
444 static const struct object_ops iosb_ops =
446 sizeof(struct iosb), /* size */
447 iosb_dump, /* dump */
448 no_get_type, /* get_type */
449 no_add_queue, /* add_queue */
450 NULL, /* remove_queue */
451 NULL, /* signaled */
452 NULL, /* satisfied */
453 no_signal, /* signal */
454 no_get_fd, /* get_fd */
455 no_map_access, /* map_access */
456 default_get_sd, /* get_sd */
457 default_set_sd, /* set_sd */
458 no_lookup_name, /* lookup_name */
459 no_link_name, /* link_name */
460 NULL, /* unlink_name */
461 no_open_file, /* open_file */
462 no_close_handle, /* close_handle */
463 iosb_destroy /* destroy */
466 static void iosb_dump( struct object *obj, int verbose )
468 assert( obj->ops == &iosb_ops );
469 fprintf( stderr, "I/O status block\n" );
472 static void iosb_destroy( struct object *obj )
474 struct iosb *iosb = (struct iosb *)obj;
476 free( iosb->in_data );
477 free( iosb->out_data );
480 /* allocate iosb struct */
481 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
483 struct iosb *iosb;
485 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
487 iosb->status = STATUS_PENDING;
488 iosb->result = 0;
489 iosb->in_size = in_size;
490 iosb->in_data = NULL;
491 iosb->out_size = out_size;
492 iosb->out_data = NULL;
494 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
496 release_object( iosb );
497 iosb = NULL;
500 return iosb;
503 struct iosb *async_get_iosb( struct async *async )
505 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
508 int async_is_blocking( struct async *async )
510 return !async->event && !async->data.apc && !async->data.apc_context;
513 /* find the first pending async in queue */
514 struct async *find_pending_async( struct async_queue *queue )
516 struct async *async;
517 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
518 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
519 return NULL;
522 /* cancels all async I/O */
523 DECL_HANDLER(cancel_async)
525 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
526 struct thread *thread = req->only_thread ? current : NULL;
528 if (obj)
530 int count = cancel_async( current->process, obj, thread, req->iosb );
531 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
532 release_object( obj );
536 /* get async result from associated iosb */
537 DECL_HANDLER(get_async_result)
539 struct iosb *iosb = NULL;
540 struct async *async;
542 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
543 if (async->data.user == req->user_arg)
545 iosb = async->iosb;
546 break;
549 if (!iosb)
551 set_error( STATUS_INVALID_PARAMETER );
552 return;
555 if (iosb->out_data)
557 data_size_t size = min( iosb->out_size, get_reply_max_size() );
558 if (size)
560 set_reply_data_ptr( iosb->out_data, size );
561 iosb->out_data = NULL;
564 reply->size = iosb->result;
565 set_error( iosb->status );