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
27 #define WIN32_NO_STATUS
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 */
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 */
59 no_signal, /* signal */
60 no_get_fd, /* get_fd */
61 no_map_access, /* map_access */
62 no_lookup_name, /* lookup_name */
63 no_open_file, /* open_file */
64 no_close_handle, /* close_handle */
65 async_destroy /* destroy */
71 struct object obj; /* object header */
72 struct fd *fd; /* file descriptor owning this queue */
73 struct list queue; /* queue of async objects */
76 static void async_queue_dump( struct object *obj, int verbose );
78 static const struct object_ops async_queue_ops =
80 sizeof(struct async_queue), /* size */
81 async_queue_dump, /* dump */
82 no_add_queue, /* add_queue */
83 NULL, /* remove_queue */
86 no_signal, /* signal */
87 no_get_fd, /* get_fd */
88 no_map_access, /* map_access */
89 no_lookup_name, /* lookup_name */
90 no_open_file, /* open_file */
91 no_close_handle, /* close_handle */
92 no_destroy /* destroy */
96 static inline void async_reselect( struct async *async )
98 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
101 static void async_dump( struct object *obj, int verbose )
103 struct async *async = (struct async *)obj;
104 assert( obj->ops == &async_ops );
105 fprintf( stderr, "Async thread=%p\n", async->thread );
108 static void async_destroy( struct object *obj )
110 struct async *async = (struct async *)obj;
111 assert( obj->ops == &async_ops );
113 list_remove( &async->queue_entry );
114 async_reselect( async );
116 if (async->timeout) remove_timeout_user( async->timeout );
117 if (async->event) release_object( async->event );
118 release_object( async->queue );
119 release_object( async->thread );
122 static void async_queue_dump( struct object *obj, int verbose )
124 struct async_queue *async_queue = (struct async_queue *)obj;
125 assert( obj->ops == &async_queue_ops );
126 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
129 /* notifies client thread of new status of its async request */
130 void async_terminate( struct async *async, unsigned int status )
134 assert( status != STATUS_PENDING );
136 if (async->status != STATUS_PENDING)
138 /* already terminated, just update status */
139 async->status = status;
143 memset( &data, 0, sizeof(data) );
144 data.type = APC_ASYNC_IO;
145 data.async_io.func = async->data.callback;
146 data.async_io.user = async->data.arg;
147 data.async_io.sb = async->data.iosb;
148 data.async_io.status = status;
149 thread_queue_apc( async->thread, &async->obj, &data );
150 async->status = status;
151 async_reselect( async );
152 release_object( async ); /* so that it gets destroyed when the async is done */
155 /* callback for timeout on an async request */
156 static void async_timeout( void *private )
158 struct async *async = private;
160 async->timeout = NULL;
161 async_terminate( async, async->timeout_status );
164 /* create a new async queue for a given fd */
165 struct async_queue *create_async_queue( struct fd *fd )
167 struct async_queue *queue = alloc_object( &async_queue_ops );
172 list_init( &queue->queue );
177 /* free an async queue, cancelling all async operations */
178 void free_async_queue( struct async_queue *queue )
182 async_wake_up( queue, STATUS_HANDLES_CLOSED );
183 release_object( queue );
186 /* create an async on a given queue of a fd */
187 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
189 struct event *event = NULL;
192 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
195 if (!(async = alloc_object( &async_ops )))
197 if (event) release_object( event );
201 async->thread = (struct thread *)grab_object( thread );
202 async->event = event;
203 async->status = STATUS_PENDING;
205 async->timeout = NULL;
206 async->queue = (struct async_queue *)grab_object( queue );
208 list_add_tail( &queue->queue, &async->queue_entry );
209 grab_object( async );
211 if (queue->fd) set_fd_signaled( queue->fd, 0 );
212 if (event) reset_event( event );
216 /* set the timeout of an async operation */
217 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
219 if (async->timeout) remove_timeout_user( async->timeout );
220 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
221 else async->timeout = NULL;
222 async->timeout_status = status;
225 /* store the result of the client-side async callback */
226 void async_set_result( struct object *obj, unsigned int status )
228 struct async *async = (struct async *)obj;
230 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
232 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
234 if (status == STATUS_PENDING) /* restart it */
236 status = async->status;
237 async->status = STATUS_PENDING;
238 grab_object( async );
240 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
241 async_terminate( async, status );
243 async_reselect( async );
247 if (async->timeout) remove_timeout_user( async->timeout );
248 async->timeout = NULL;
249 async->status = status;
253 data.type = APC_USER;
254 data.user.func = async->data.apc;
255 data.user.args[0] = (unsigned long)async->data.apc_arg;
256 data.user.args[1] = (unsigned long)async->data.iosb;
257 data.user.args[2] = 0;
258 thread_queue_apc( async->thread, NULL, &data );
260 if (async->event) set_event( async->event );
261 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
265 /* check if an async operation is waiting to be alerted */
266 int async_waiting( struct async_queue *queue )
271 if (!queue) return 0;
272 if (!(ptr = list_head( &queue->queue ))) return 0;
273 async = LIST_ENTRY( ptr, struct async, queue_entry );
274 return async->status == STATUS_PENDING;
277 /* wake up async operations on the queue */
278 void async_wake_up( struct async_queue *queue, unsigned int status )
280 struct list *ptr, *next;
284 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
286 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
287 async_terminate( async, status );
288 if (status == STATUS_ALERTED) break; /* only wake up the first one */