push 583922ece91f6a3e6a32c772f3574cd16e6257fa
[wine/hacks.git] / server / async.c
blob3a99f80a46a3507b1b1ac49e59c84f6437fec885
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_add_queue, /* add_queue */
56 NULL, /* remove_queue */
57 NULL, /* signaled */
58 NULL, /* satisfied */
59 no_signal, /* signal */
60 no_get_fd, /* get_fd */
61 no_map_access, /* map_access */
62 default_get_sd, /* get_sd */
63 default_set_sd, /* set_sd */
64 no_lookup_name, /* lookup_name */
65 no_open_file, /* open_file */
66 no_close_handle, /* close_handle */
67 async_destroy /* destroy */
71 struct async_queue
73 struct object obj; /* object header */
74 struct fd *fd; /* file descriptor owning this queue */
75 struct list queue; /* queue of async objects */
78 static void async_queue_dump( struct object *obj, int verbose );
80 static const struct object_ops async_queue_ops =
82 sizeof(struct async_queue), /* size */
83 async_queue_dump, /* dump */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 NULL, /* satisfied */
88 no_signal, /* signal */
89 no_get_fd, /* get_fd */
90 no_map_access, /* map_access */
91 default_get_sd, /* get_sd */
92 default_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_open_file, /* open_file */
95 no_close_handle, /* close_handle */
96 no_destroy /* destroy */
100 static inline void async_reselect( struct async *async )
102 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
105 static void async_dump( struct object *obj, int verbose )
107 struct async *async = (struct async *)obj;
108 assert( obj->ops == &async_ops );
109 fprintf( stderr, "Async thread=%p\n", async->thread );
112 static void async_destroy( struct object *obj )
114 struct async *async = (struct async *)obj;
115 assert( obj->ops == &async_ops );
117 list_remove( &async->queue_entry );
118 async_reselect( async );
120 if (async->timeout) remove_timeout_user( async->timeout );
121 if (async->event) release_object( async->event );
122 release_object( async->queue );
123 release_object( async->thread );
126 static void async_queue_dump( struct object *obj, int verbose )
128 struct async_queue *async_queue = (struct async_queue *)obj;
129 assert( obj->ops == &async_queue_ops );
130 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
133 /* notifies client thread of new status of its async request */
134 void async_terminate( struct async *async, unsigned int status )
136 apc_call_t data;
138 assert( status != STATUS_PENDING );
140 if (async->status != STATUS_PENDING)
142 /* already terminated, just update status */
143 async->status = status;
144 return;
147 memset( &data, 0, sizeof(data) );
148 data.type = APC_ASYNC_IO;
149 data.async_io.func = async->data.callback;
150 data.async_io.user = async->data.arg;
151 data.async_io.sb = async->data.iosb;
152 data.async_io.status = status;
153 thread_queue_apc( async->thread, &async->obj, &data );
154 async->status = status;
155 async_reselect( async );
156 release_object( async ); /* so that it gets destroyed when the async is done */
159 /* callback for timeout on an async request */
160 static void async_timeout( void *private )
162 struct async *async = private;
164 async->timeout = NULL;
165 async_terminate( async, async->timeout_status );
168 /* create a new async queue for a given fd */
169 struct async_queue *create_async_queue( struct fd *fd )
171 struct async_queue *queue = alloc_object( &async_queue_ops );
173 if (queue)
175 queue->fd = fd;
176 list_init( &queue->queue );
178 return queue;
181 /* free an async queue, cancelling all async operations */
182 void free_async_queue( struct async_queue *queue )
184 if (!queue) return;
185 queue->fd = NULL;
186 async_wake_up( queue, STATUS_HANDLES_CLOSED );
187 release_object( queue );
190 /* create an async on a given queue of a fd */
191 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
193 struct event *event = NULL;
194 struct async *async;
196 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
197 return NULL;
199 if (!(async = alloc_object( &async_ops )))
201 if (event) release_object( event );
202 return NULL;
205 async->thread = (struct thread *)grab_object( thread );
206 async->event = event;
207 async->status = STATUS_PENDING;
208 async->data = *data;
209 async->timeout = NULL;
210 async->queue = (struct async_queue *)grab_object( queue );
212 list_add_tail( &queue->queue, &async->queue_entry );
213 grab_object( async );
215 if (queue->fd) set_fd_signaled( queue->fd, 0 );
216 if (event) reset_event( event );
217 return async;
220 /* set the timeout of an async operation */
221 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
223 if (async->timeout) remove_timeout_user( async->timeout );
224 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
225 else async->timeout = NULL;
226 async->timeout_status = status;
229 /* store the result of the client-side async callback */
230 void async_set_result( struct object *obj, unsigned int status )
232 struct async *async = (struct async *)obj;
234 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
236 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
238 if (status == STATUS_PENDING) /* restart it */
240 status = async->status;
241 async->status = STATUS_PENDING;
242 grab_object( async );
244 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
245 async_terminate( async, status );
246 else
247 async_reselect( async );
249 else
251 if (async->timeout) remove_timeout_user( async->timeout );
252 async->timeout = NULL;
253 async->status = status;
254 if (async->data.apc)
256 apc_call_t data;
257 data.type = APC_USER;
258 data.user.func = async->data.apc;
259 data.user.args[0] = (unsigned long)async->data.arg;
260 data.user.args[1] = (unsigned long)async->data.iosb;
261 data.user.args[2] = 0;
262 thread_queue_apc( async->thread, NULL, &data );
264 if (async->event) set_event( async->event );
265 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
269 /* check if an async operation is waiting to be alerted */
270 int async_waiting( struct async_queue *queue )
272 struct list *ptr;
273 struct async *async;
275 if (!queue) return 0;
276 if (!(ptr = list_head( &queue->queue ))) return 0;
277 async = LIST_ENTRY( ptr, struct async, queue_entry );
278 return async->status == STATUS_PENDING;
281 /* wake up async operations on the queue */
282 void async_wake_up( struct async_queue *queue, unsigned int status )
284 struct list *ptr, *next;
286 if (!queue) return;
288 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
290 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
291 async_terminate( async, status );
292 if (status == STATUS_ALERTED) break; /* only wake up the first one */