wined3d: Avoid accessing gl_info in wined3d_buffer_init().
[wine.git] / server / async.c
blob5486601c6753fce03e6fa2401939899303845eb2
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 */
60 static void async_dump( struct object *obj, int verbose );
61 static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
62 static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
63 static void async_destroy( struct object *obj );
65 static const struct object_ops async_ops =
67 sizeof(struct async), /* size */
68 async_dump, /* dump */
69 no_get_type, /* get_type */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 async_signaled, /* signaled */
73 async_satisfied, /* satisfied */
74 no_signal, /* signal */
75 no_get_fd, /* get_fd */
76 no_map_access, /* map_access */
77 default_get_sd, /* get_sd */
78 default_set_sd, /* set_sd */
79 no_lookup_name, /* lookup_name */
80 no_link_name, /* link_name */
81 NULL, /* unlink_name */
82 no_open_file, /* open_file */
83 no_kernel_obj_list, /* get_kernel_obj_list */
84 no_close_handle, /* close_handle */
85 async_destroy /* destroy */
88 static inline void async_reselect( struct async *async )
90 if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
93 static void async_dump( struct object *obj, int verbose )
95 struct async *async = (struct async *)obj;
96 assert( obj->ops == &async_ops );
97 fprintf( stderr, "Async thread=%p\n", async->thread );
100 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
102 struct async *async = (struct async *)obj;
103 assert( obj->ops == &async_ops );
104 return async->signaled;
107 static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
109 struct async *async = (struct async *)obj;
110 assert( obj->ops == &async_ops );
112 if (async->direct_result)
114 async_set_result( &async->obj, async->iosb->status, async->iosb->result );
115 async->direct_result = 0;
118 /* close wait handle here to avoid extra server round trip */
119 if (async->wait_handle)
121 close_handle( async->thread->process, async->wait_handle );
122 async->wait_handle = 0;
125 if (async->status == STATUS_PENDING) make_wait_abandoned( entry );
128 static void async_destroy( struct object *obj )
130 struct async *async = (struct async *)obj;
131 assert( obj->ops == &async_ops );
133 list_remove( &async->process_entry );
135 if (async->queue)
137 list_remove( &async->queue_entry );
138 async_reselect( async );
140 else if (async->fd) release_object( async->fd );
142 if (async->timeout) remove_timeout_user( async->timeout );
143 if (async->completion) release_object( async->completion );
144 if (async->event) release_object( async->event );
145 if (async->iosb) release_object( async->iosb );
146 release_object( async->thread );
149 /* notifies client thread of new status of its async request */
150 void async_terminate( struct async *async, unsigned int status )
152 assert( status != STATUS_PENDING );
154 if (async->status != STATUS_PENDING)
156 /* already terminated, just update status */
157 async->status = status;
158 return;
161 async->status = status;
162 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
164 if (!async->direct_result)
166 if (async->data.user)
168 apc_call_t data;
170 memset( &data, 0, sizeof(data) );
171 data.type = APC_ASYNC_IO;
172 data.async_io.user = async->data.user;
173 data.async_io.sb = async->data.iosb;
174 data.async_io.status = status;
175 thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
177 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
180 async_reselect( async );
181 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
184 /* callback for timeout on an async request */
185 static void async_timeout( void *private )
187 struct async *async = private;
189 async->timeout = NULL;
190 async_terminate( async, async->timeout_status );
193 /* free an async queue, cancelling all async operations */
194 void free_async_queue( struct async_queue *queue )
196 struct async *async, *next;
198 LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
200 grab_object( &async->obj );
201 if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
202 async->fd = NULL;
203 async_terminate( async, STATUS_HANDLES_CLOSED );
204 async->queue = NULL;
205 release_object( &async->obj );
209 void queue_async( struct async_queue *queue, struct async *async )
211 /* fd will be set to NULL in free_async_queue when fd is destroyed */
212 release_object( async->fd );
214 async->queue = queue;
215 grab_object( async );
216 list_add_tail( &queue->queue, &async->queue_entry );
218 set_fd_signaled( async->fd, 0 );
221 /* create an async on a given queue of a fd */
222 struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
224 struct event *event = NULL;
225 struct async *async;
227 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
228 return NULL;
230 if (!(async = alloc_object( &async_ops )))
232 if (event) release_object( event );
233 return NULL;
236 async->thread = (struct thread *)grab_object( thread );
237 async->event = event;
238 async->status = STATUS_PENDING;
239 async->data = *data;
240 async->timeout = NULL;
241 async->queue = NULL;
242 async->fd = (struct fd *)grab_object( fd );
243 async->signaled = 0;
244 async->pending = 1;
245 async->wait_handle = 0;
246 async->direct_result = 0;
247 async->completion = fd_get_completion( fd, &async->comp_key );
248 async->comp_flags = 0;
250 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
251 else async->iosb = NULL;
253 list_add_head( &thread->process->asyncs, &async->process_entry );
254 if (event) reset_event( event );
256 if (async->completion && data->apc)
258 release_object( async );
259 set_error( STATUS_INVALID_PARAMETER );
260 return NULL;
263 return async;
266 void set_async_pending( struct async *async, int signal )
268 if (async->status == STATUS_PENDING)
270 async->pending = 1;
271 if (signal && !async->signaled)
273 async->signaled = 1;
274 wake_up( &async->obj, 0 );
279 /* create an async associated with iosb for async-based requests
280 * returned async must be passed to async_handoff */
281 struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
283 struct async *async;
284 struct iosb *iosb;
286 if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
287 return NULL;
289 async = create_async( fd, current, data, iosb );
290 release_object( iosb );
291 if (async)
293 if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
295 release_object( async );
296 return NULL;
298 async->pending = 0;
299 async->direct_result = 1;
300 async->comp_flags = comp_flags;
302 return async;
305 /* return async object status and wait handle to client */
306 obj_handle_t async_handoff( struct async *async, int success, data_size_t *result, int force_blocking )
308 if (!success)
310 if (get_error() == STATUS_PENDING)
312 /* we don't know the result yet, so client needs to wait */
313 async->direct_result = 0;
314 return async->wait_handle;
316 close_handle( async->thread->process, async->wait_handle );
317 async->wait_handle = 0;
318 return 0;
321 if (get_error() != STATUS_PENDING)
323 /* status and data are already set and returned */
324 async_terminate( async, get_error() );
326 else if (async->iosb->status != STATUS_PENDING)
328 /* result is already available in iosb, return it */
329 if (async->iosb->out_data)
331 set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
332 async->iosb->out_data = NULL;
336 if (async->iosb->status != STATUS_PENDING)
338 if (result) *result = async->iosb->result;
339 async->signaled = 1;
341 else
343 async->direct_result = 0;
344 async->pending = 1;
345 if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
347 close_handle( async->thread->process, async->wait_handle);
348 async->wait_handle = 0;
351 set_error( async->iosb->status );
352 return async->wait_handle;
355 /* set the timeout of an async operation */
356 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
358 if (async->timeout) remove_timeout_user( async->timeout );
359 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
360 else async->timeout = NULL;
361 async->timeout_status = status;
364 static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
365 apc_param_t information )
367 if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
368 if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
371 /* store the result of the client-side async callback */
372 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
374 struct async *async = (struct async *)obj;
376 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
378 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
380 if (status == STATUS_PENDING) /* restart it */
382 status = async->status;
383 async->status = STATUS_PENDING;
384 grab_object( async );
386 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
387 async_terminate( async, status );
388 else
389 async_reselect( async );
391 else
393 if (async->timeout) remove_timeout_user( async->timeout );
394 async->timeout = NULL;
395 async->status = status;
396 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
398 if (async->data.apc)
400 apc_call_t data;
401 memset( &data, 0, sizeof(data) );
402 data.type = APC_USER;
403 data.user.func = async->data.apc;
404 data.user.args[0] = async->data.apc_context;
405 data.user.args[1] = async->data.iosb;
406 data.user.args[2] = 0;
407 thread_queue_apc( NULL, async->thread, NULL, &data );
409 else if (async->data.apc_context && (async->pending ||
410 !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
412 add_async_completion( async, async->data.apc_context, status, total );
415 if (async->event) set_event( async->event );
416 else if (async->fd) set_fd_signaled( async->fd, 1 );
417 if (!async->signaled)
419 async->signaled = 1;
420 wake_up( &async->obj, 0 );
425 /* check if an async operation is waiting to be alerted */
426 int async_waiting( struct async_queue *queue )
428 struct list *ptr;
429 struct async *async;
431 if (!(ptr = list_head( &queue->queue ))) return 0;
432 async = LIST_ENTRY( ptr, struct async, queue_entry );
433 return async->status == STATUS_PENDING;
436 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
438 struct async *async;
439 int woken = 0;
441 restart:
442 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
444 if (async->status == STATUS_CANCELLED) continue;
445 if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
446 (!thread || async->thread == thread) &&
447 (!iosb || async->data.iosb == iosb))
449 async_terminate( async, STATUS_CANCELLED );
450 woken++;
451 goto restart;
454 return woken;
457 void cancel_process_asyncs( struct process *process )
459 cancel_async( process, NULL, NULL, 0 );
462 /* wake up async operations on the queue */
463 void async_wake_up( struct async_queue *queue, unsigned int status )
465 struct list *ptr, *next;
467 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
469 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
470 async_terminate( async, status );
471 if (status == STATUS_ALERTED) break; /* only wake up the first one */
475 static void iosb_dump( struct object *obj, int verbose );
476 static void iosb_destroy( struct object *obj );
478 static const struct object_ops iosb_ops =
480 sizeof(struct iosb), /* size */
481 iosb_dump, /* dump */
482 no_get_type, /* get_type */
483 no_add_queue, /* add_queue */
484 NULL, /* remove_queue */
485 NULL, /* signaled */
486 NULL, /* satisfied */
487 no_signal, /* signal */
488 no_get_fd, /* get_fd */
489 no_map_access, /* map_access */
490 default_get_sd, /* get_sd */
491 default_set_sd, /* set_sd */
492 no_lookup_name, /* lookup_name */
493 no_link_name, /* link_name */
494 NULL, /* unlink_name */
495 no_open_file, /* open_file */
496 no_kernel_obj_list, /* get_kernel_obj_list */
497 no_close_handle, /* close_handle */
498 iosb_destroy /* destroy */
501 static void iosb_dump( struct object *obj, int verbose )
503 assert( obj->ops == &iosb_ops );
504 fprintf( stderr, "I/O status block\n" );
507 static void iosb_destroy( struct object *obj )
509 struct iosb *iosb = (struct iosb *)obj;
511 free( iosb->in_data );
512 free( iosb->out_data );
515 /* allocate iosb struct */
516 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
518 struct iosb *iosb;
520 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
522 iosb->status = STATUS_PENDING;
523 iosb->result = 0;
524 iosb->in_size = in_size;
525 iosb->in_data = NULL;
526 iosb->out_size = out_size;
527 iosb->out_data = NULL;
529 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
531 release_object( iosb );
532 iosb = NULL;
535 return iosb;
538 struct iosb *async_get_iosb( struct async *async )
540 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
543 int async_is_blocking( struct async *async )
545 return !async->event && !async->data.apc && !async->data.apc_context;
548 /* find the first pending async in queue */
549 struct async *find_pending_async( struct async_queue *queue )
551 struct async *async;
552 LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
553 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
554 return NULL;
557 /* cancels all async I/O */
558 DECL_HANDLER(cancel_async)
560 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
561 struct thread *thread = req->only_thread ? current : NULL;
563 if (obj)
565 int count = cancel_async( current->process, obj, thread, req->iosb );
566 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
567 release_object( obj );
571 /* get async result from associated iosb */
572 DECL_HANDLER(get_async_result)
574 struct iosb *iosb = NULL;
575 struct async *async;
577 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
578 if (async->data.user == req->user_arg)
580 iosb = async->iosb;
581 break;
584 if (!iosb)
586 set_error( STATUS_INVALID_PARAMETER );
587 return;
590 if (iosb->out_data)
592 data_size_t size = min( iosb->out_size, get_reply_max_size() );
593 if (size)
595 set_reply_data_ptr( iosb->out_data, size );
596 iosb->out_data = NULL;
599 reply->size = iosb->result;
600 set_error( iosb->status );