iphlpapi: Add partial implementation of GetIfTable2Ex.
[wine.git] / server / async.c
blobf9d8af36b957807c91d1ec572d94bd45a52125d3
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 unsigned int status; /* current status */
45 struct timeout_user *timeout;
46 unsigned int timeout_status; /* status to report upon timeout */
47 int signaled;
48 struct event *event;
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 */
80 struct async_queue
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 */
99 NULL, /* signaled */
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 && 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 );
141 if (async->queue)
143 list_remove( &async->queue_entry );
144 async_reselect( async );
145 release_object( async->queue );
148 if (async->timeout) remove_timeout_user( async->timeout );
149 if (async->event) release_object( async->event );
150 if (async->iosb) release_object( async->iosb );
151 release_object( async->thread );
154 static void async_queue_dump( struct object *obj, int verbose )
156 struct async_queue *async_queue = (struct async_queue *)obj;
157 assert( obj->ops == &async_queue_ops );
158 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
161 static void async_queue_destroy( struct object *obj )
163 struct async_queue *async_queue = (struct async_queue *)obj;
164 assert( obj->ops == &async_queue_ops );
165 if (async_queue->completion) release_object( async_queue->completion );
168 /* notifies client thread of new status of its async request */
169 void async_terminate( struct async *async, unsigned int status )
171 assert( status != STATUS_PENDING );
173 if (async->status != STATUS_PENDING)
175 /* already terminated, just update status */
176 async->status = status;
177 return;
180 async->status = status;
181 if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
183 if (async->data.user)
185 apc_call_t data;
187 memset( &data, 0, sizeof(data) );
188 data.type = APC_ASYNC_IO;
189 data.async_io.user = async->data.user;
190 data.async_io.sb = async->data.iosb;
191 data.async_io.status = status;
192 thread_queue_apc( async->thread, &async->obj, &data );
194 else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
196 async_reselect( async );
197 if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
200 /* callback for timeout on an async request */
201 static void async_timeout( void *private )
203 struct async *async = private;
205 async->timeout = NULL;
206 async_terminate( async, async->timeout_status );
209 /* create a new async queue for a given fd */
210 struct async_queue *create_async_queue( struct fd *fd )
212 struct async_queue *queue = alloc_object( &async_queue_ops );
214 if (queue)
216 queue->fd = fd;
217 queue->completion = NULL;
218 list_init( &queue->queue );
220 return queue;
223 /* free an async queue, cancelling all async operations */
224 void free_async_queue( struct async_queue *queue )
226 if (!queue) return;
227 if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
228 queue->fd = NULL;
229 async_wake_up( queue, STATUS_HANDLES_CLOSED );
230 release_object( queue );
233 void queue_async( struct async_queue *queue, struct async *async )
235 async->queue = (struct async_queue *)grab_object( queue );
237 grab_object( async );
238 list_add_tail( &queue->queue, &async->queue_entry );
240 if (queue->fd) set_fd_signaled( queue->fd, 0 );
243 /* create an async on a given queue of a fd */
244 struct async *create_async( struct thread *thread, const async_data_t *data, struct iosb *iosb )
246 struct event *event = NULL;
247 struct async *async;
249 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
250 return NULL;
252 if (!(async = alloc_object( &async_ops )))
254 if (event) release_object( event );
255 return NULL;
258 async->thread = (struct thread *)grab_object( thread );
259 async->event = event;
260 async->status = STATUS_PENDING;
261 async->data = *data;
262 async->timeout = NULL;
263 async->queue = NULL;
264 async->signaled = 0;
266 if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
267 else async->iosb = NULL;
269 list_add_head( &thread->process->asyncs, &async->process_entry );
270 if (event) reset_event( event );
272 return async;
275 /* set the timeout of an async operation */
276 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
278 if (async->timeout) remove_timeout_user( async->timeout );
279 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
280 else async->timeout = NULL;
281 async->timeout_status = status;
284 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
285 apc_param_t information )
287 if (queue->fd)
289 apc_param_t ckey;
290 struct completion *completion = fd_get_completion( queue->fd, &ckey );
292 if (completion)
294 add_completion( completion, ckey, cvalue, status, information );
295 release_object( completion );
298 else if (queue->completion) add_completion( queue->completion, queue->comp_key,
299 cvalue, status, information );
302 /* store the result of the client-side async callback */
303 void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
305 struct async *async = (struct async *)obj;
307 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
309 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
311 if (status == STATUS_PENDING) /* restart it */
313 status = async->status;
314 async->status = STATUS_PENDING;
315 grab_object( async );
317 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
318 async_terminate( async, status );
319 else
320 async_reselect( async );
322 else
324 if (async->timeout) remove_timeout_user( async->timeout );
325 async->timeout = NULL;
326 async->status = status;
327 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
329 if (async->queue && !async->data.apc && async->data.apc_context)
330 add_async_completion( async->queue, async->data.apc_context, status, total );
331 if (async->data.apc)
333 apc_call_t data;
334 memset( &data, 0, sizeof(data) );
335 data.type = APC_USER;
336 data.user.func = async->data.apc;
337 data.user.args[0] = async->data.apc_context;
338 data.user.args[1] = async->data.iosb;
339 data.user.args[2] = 0;
340 thread_queue_apc( async->thread, NULL, &data );
342 if (async->event) set_event( async->event );
343 else if (async->queue && async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
344 async->signaled = 1;
345 wake_up( &async->obj, 0 );
349 /* check if there are any queued async operations */
350 int async_queued( struct async_queue *queue )
352 return queue && list_head( &queue->queue );
355 /* check if an async operation is waiting to be alerted */
356 int async_waiting( struct async_queue *queue )
358 struct list *ptr;
359 struct async *async;
361 if (!queue) return 0;
362 if (!(ptr = list_head( &queue->queue ))) return 0;
363 async = LIST_ENTRY( ptr, struct async, queue_entry );
364 return async->status == STATUS_PENDING;
367 static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
369 struct async *async;
370 int woken = 0;
372 restart:
373 LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
375 if (async->status == STATUS_CANCELLED) continue;
376 if ((!obj || (async->queue && async->queue->fd && get_fd_user( async->queue->fd ) == obj)) &&
377 (!thread || async->thread == thread) &&
378 (!iosb || async->data.iosb == iosb))
380 async_terminate( async, STATUS_CANCELLED );
381 woken++;
382 goto restart;
385 return woken;
388 void cancel_process_asyncs( struct process *process )
390 cancel_async( process, NULL, NULL, 0 );
393 /* wake up async operations on the queue */
394 void async_wake_up( struct async_queue *queue, unsigned int status )
396 struct list *ptr, *next;
398 if (!queue) return;
400 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
402 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
403 async_terminate( async, status );
404 if (status == STATUS_ALERTED) break; /* only wake up the first one */
408 static void iosb_dump( struct object *obj, int verbose );
409 static void iosb_destroy( struct object *obj );
411 static const struct object_ops iosb_ops =
413 sizeof(struct iosb), /* size */
414 iosb_dump, /* dump */
415 no_get_type, /* get_type */
416 no_add_queue, /* add_queue */
417 NULL, /* remove_queue */
418 NULL, /* signaled */
419 NULL, /* satisfied */
420 no_signal, /* signal */
421 no_get_fd, /* get_fd */
422 no_map_access, /* map_access */
423 default_get_sd, /* get_sd */
424 default_set_sd, /* set_sd */
425 no_lookup_name, /* lookup_name */
426 no_link_name, /* link_name */
427 NULL, /* unlink_name */
428 no_open_file, /* open_file */
429 no_close_handle, /* close_handle */
430 iosb_destroy /* destroy */
433 static void iosb_dump( struct object *obj, int verbose )
435 assert( obj->ops == &iosb_ops );
436 fprintf( stderr, "I/O status block\n" );
439 static void iosb_destroy( struct object *obj )
441 struct iosb *iosb = (struct iosb *)obj;
443 free( iosb->in_data );
444 free( iosb->out_data );
447 /* allocate iosb struct */
448 struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
450 struct iosb *iosb;
452 if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
454 iosb->status = STATUS_PENDING;
455 iosb->result = 0;
456 iosb->in_size = in_size;
457 iosb->in_data = NULL;
458 iosb->out_size = out_size;
459 iosb->out_data = NULL;
461 if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
463 release_object( iosb );
464 iosb = NULL;
467 return iosb;
470 struct iosb *async_get_iosb( struct async *async )
472 return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
475 int async_is_blocking( struct async *async )
477 return !async->event && !async->data.apc && !async->data.apc_context;
480 /* find the first pending async in queue */
481 struct async *find_pending_async( struct async_queue *queue )
483 struct async *async;
484 if (queue) LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
485 if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
486 return NULL;
489 /* cancels all async I/O */
490 DECL_HANDLER(cancel_async)
492 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
493 struct thread *thread = req->only_thread ? current : NULL;
495 if (obj)
497 int count = cancel_async( current->process, obj, thread, req->iosb );
498 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
499 release_object( obj );
503 /* get async result from associated iosb */
504 DECL_HANDLER(get_async_result)
506 struct iosb *iosb = NULL;
507 struct async *async;
509 LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
510 if (async->data.user == req->user_arg)
512 iosb = async->iosb;
513 break;
516 if (!iosb)
518 set_error( STATUS_INVALID_PARAMETER );
519 return;
522 if (iosb->out_data)
524 data_size_t size = min( iosb->out_size, get_reply_max_size() );
525 if (size)
527 set_reply_data_ptr( iosb->out_data, size );
528 iosb->out_data = NULL;
531 reply->size = iosb->result;
532 set_error( iosb->status );