ntoskrnl: Add support for read and write requests.
[wine/multimedia.git] / server / async.c
blobe1650fe857ff4b6c7ff409a935fed32ce4d56950
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"
35 struct async
37 struct object obj; /* object header */
38 struct thread *thread; /* owning thread */
39 struct list queue_entry; /* entry in async queue list */
40 struct async_queue *queue; /* queue containing this async */
41 unsigned int status; /* current status */
42 struct timeout_user *timeout;
43 unsigned int timeout_status; /* status to report upon timeout */
44 struct event *event;
45 async_data_t data; /* data for async I/O call */
48 static void async_dump( struct object *obj, int verbose );
49 static void async_destroy( struct object *obj );
51 static const struct object_ops async_ops =
53 sizeof(struct async), /* size */
54 async_dump, /* dump */
55 no_get_type, /* get_type */
56 no_add_queue, /* add_queue */
57 NULL, /* remove_queue */
58 NULL, /* signaled */
59 NULL, /* satisfied */
60 no_signal, /* signal */
61 no_get_fd, /* get_fd */
62 no_map_access, /* map_access */
63 default_get_sd, /* get_sd */
64 default_set_sd, /* set_sd */
65 no_lookup_name, /* lookup_name */
66 no_open_file, /* open_file */
67 no_close_handle, /* close_handle */
68 async_destroy /* destroy */
72 struct async_queue
74 struct object obj; /* object header */
75 struct fd *fd; /* file descriptor owning this queue */
76 struct completion *completion; /* completion associated with a recently closed file descriptor */
77 apc_param_t comp_key; /* completion key associated with a recently closed file descriptor */
78 struct list queue; /* queue of async objects */
81 static void async_queue_dump( struct object *obj, int verbose );
82 static void async_queue_destroy( struct object *obj );
84 static const struct object_ops async_queue_ops =
86 sizeof(struct async_queue), /* size */
87 async_queue_dump, /* dump */
88 no_get_type, /* get_type */
89 no_add_queue, /* add_queue */
90 NULL, /* remove_queue */
91 NULL, /* signaled */
92 NULL, /* satisfied */
93 no_signal, /* signal */
94 no_get_fd, /* get_fd */
95 no_map_access, /* map_access */
96 default_get_sd, /* get_sd */
97 default_set_sd, /* set_sd */
98 no_lookup_name, /* lookup_name */
99 no_open_file, /* open_file */
100 no_close_handle, /* close_handle */
101 async_queue_destroy /* destroy */
105 static inline void async_reselect( struct async *async )
107 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
110 static void async_dump( struct object *obj, int verbose )
112 struct async *async = (struct async *)obj;
113 assert( obj->ops == &async_ops );
114 fprintf( stderr, "Async thread=%p\n", async->thread );
117 static void async_destroy( struct object *obj )
119 struct async *async = (struct async *)obj;
120 assert( obj->ops == &async_ops );
122 list_remove( &async->queue_entry );
123 async_reselect( async );
125 if (async->timeout) remove_timeout_user( async->timeout );
126 if (async->event) release_object( async->event );
127 release_object( async->queue );
128 release_object( async->thread );
131 static void async_queue_dump( struct object *obj, int verbose )
133 struct async_queue *async_queue = (struct async_queue *)obj;
134 assert( obj->ops == &async_queue_ops );
135 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
138 static void async_queue_destroy( struct object *obj )
140 struct async_queue *async_queue = (struct async_queue *)obj;
141 assert( obj->ops == &async_queue_ops );
142 if (async_queue->completion) release_object( async_queue->completion );
145 /* notifies client thread of new status of its async request */
146 void async_terminate( struct async *async, unsigned int status )
148 assert( status != STATUS_PENDING );
150 if (async->status != STATUS_PENDING)
152 /* already terminated, just update status */
153 async->status = status;
154 return;
157 async->status = status;
159 if (async->data.callback)
161 apc_call_t data;
163 memset( &data, 0, sizeof(data) );
164 data.type = APC_ASYNC_IO;
165 data.async_io.func = async->data.callback;
166 data.async_io.user = async->data.arg;
167 data.async_io.sb = async->data.iosb;
168 data.async_io.status = status;
169 thread_queue_apc( async->thread, &async->obj, &data );
171 else async_set_result( &async->obj, STATUS_SUCCESS, 0, 0, 0 );
173 async_reselect( async );
174 release_object( async ); /* so that it gets destroyed when the async is done */
177 /* callback for timeout on an async request */
178 static void async_timeout( void *private )
180 struct async *async = private;
182 async->timeout = NULL;
183 async_terminate( async, async->timeout_status );
186 /* create a new async queue for a given fd */
187 struct async_queue *create_async_queue( struct fd *fd )
189 struct async_queue *queue = alloc_object( &async_queue_ops );
191 if (queue)
193 queue->fd = fd;
194 queue->completion = NULL;
195 list_init( &queue->queue );
197 return queue;
200 /* free an async queue, cancelling all async operations */
201 void free_async_queue( struct async_queue *queue )
203 if (!queue) return;
204 if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
205 queue->fd = NULL;
206 async_wake_up( queue, STATUS_HANDLES_CLOSED );
207 release_object( queue );
210 /* create an async on a given queue of a fd */
211 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
213 struct event *event = NULL;
214 struct async *async;
216 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
217 return NULL;
219 if (!(async = alloc_object( &async_ops )))
221 if (event) release_object( event );
222 return NULL;
225 async->thread = (struct thread *)grab_object( thread );
226 async->event = event;
227 async->status = STATUS_PENDING;
228 async->data = *data;
229 async->timeout = NULL;
230 async->queue = (struct async_queue *)grab_object( queue );
232 list_add_tail( &queue->queue, &async->queue_entry );
233 grab_object( async );
235 if (queue->fd) set_fd_signaled( queue->fd, 0 );
236 if (event) reset_event( event );
237 return async;
240 /* set the timeout of an async operation */
241 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
243 if (async->timeout) remove_timeout_user( async->timeout );
244 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
245 else async->timeout = NULL;
246 async->timeout_status = status;
249 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
250 apc_param_t information )
252 if (queue->fd)
254 apc_param_t ckey;
255 struct completion *completion = fd_get_completion( queue->fd, &ckey );
257 if (completion)
259 add_completion( completion, ckey, cvalue, status, information );
260 release_object( completion );
263 else if (queue->completion) add_completion( queue->completion, queue->comp_key,
264 cvalue, status, information );
267 /* store the result of the client-side async callback */
268 void async_set_result( struct object *obj, unsigned int status, apc_param_t total,
269 client_ptr_t apc, client_ptr_t apc_arg )
271 struct async *async = (struct async *)obj;
273 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
275 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
277 if (status == STATUS_PENDING) /* restart it */
279 status = async->status;
280 async->status = STATUS_PENDING;
281 grab_object( async );
283 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
284 async_terminate( async, status );
285 else
286 async_reselect( async );
288 else
290 if (async->timeout) remove_timeout_user( async->timeout );
291 async->timeout = NULL;
292 async->status = status;
293 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
295 if (async->data.cvalue) add_async_completion( async->queue, async->data.cvalue, status, total );
296 if (apc)
298 apc_call_t data;
299 memset( &data, 0, sizeof(data) );
300 data.type = APC_USER;
301 data.user.func = apc;
302 data.user.args[0] = apc_arg;
303 data.user.args[1] = async->data.iosb;
304 data.user.args[2] = 0;
305 thread_queue_apc( async->thread, NULL, &data );
307 if (async->event) set_event( async->event );
308 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
312 /* check if there are any queued async operations */
313 int async_queued( struct async_queue *queue )
315 return queue && list_head( &queue->queue );
318 /* check if an async operation is waiting to be alerted */
319 int async_waiting( struct async_queue *queue )
321 struct list *ptr;
322 struct async *async;
324 if (!queue) return 0;
325 if (!(ptr = list_head( &queue->queue ))) return 0;
326 async = LIST_ENTRY( ptr, struct async, queue_entry );
327 return async->status == STATUS_PENDING;
330 int async_wake_up_by( struct async_queue *queue, struct process *process,
331 struct thread *thread, client_ptr_t iosb, unsigned int status )
333 struct list *ptr, *next;
334 int woken = 0;
336 if (!queue || (!process && !thread && !iosb)) return 0;
338 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
340 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
341 if ( (!process || async->thread->process == process) &&
342 (!thread || async->thread == thread) &&
343 (!iosb || async->data.iosb == iosb) )
345 async_terminate( async, status );
346 woken++;
349 return woken;
352 /* wake up async operations on the queue */
353 void async_wake_up( struct async_queue *queue, unsigned int status )
355 struct list *ptr, *next;
357 if (!queue) return;
359 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
361 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
362 async_terminate( async, status );
363 if (status == STATUS_ALERTED) break; /* only wake up the first one */