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 file descriptor queue */
40 struct timeout_user *timeout;
42 async_data_t data; /* data for async I/O call */
45 static void async_dump( struct object *obj, int verbose );
46 static void async_destroy( struct object *obj );
48 static const struct object_ops async_ops =
50 sizeof(struct async), /* size */
51 async_dump, /* dump */
52 no_add_queue, /* add_queue */
53 NULL, /* remove_queue */
56 no_signal, /* signal */
57 no_get_fd, /* get_fd */
58 no_map_access, /* map_access */
59 no_lookup_name, /* lookup_name */
60 no_open_file, /* open_file */
61 no_close_handle, /* close_handle */
62 async_destroy /* destroy */
68 struct object obj; /* object header */
69 struct fd *fd; /* file descriptor owning this queue */
70 struct list queue; /* queue of async objects */
73 static void async_queue_dump( struct object *obj, int verbose );
74 static void async_queue_destroy( struct object *obj );
76 static const struct object_ops async_queue_ops =
78 sizeof(struct async_queue), /* size */
79 async_queue_dump, /* dump */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
84 no_signal, /* signal */
85 no_get_fd, /* get_fd */
86 no_map_access, /* map_access */
87 no_lookup_name, /* lookup_name */
88 no_open_file, /* open_file */
89 no_close_handle, /* close_handle */
90 async_queue_destroy /* destroy */
94 static void async_dump( struct object *obj, int verbose )
96 struct async *async = (struct async *)obj;
97 assert( obj->ops == &async_ops );
98 fprintf( stderr, "Async thread=%p\n", async->thread );
101 static void async_destroy( struct object *obj )
103 struct async *async = (struct async *)obj;
104 assert( obj->ops == &async_ops );
106 if (async->timeout) remove_timeout_user( async->timeout );
107 if (async->event) release_object( async->event );
108 release_object( async->thread );
111 static void async_queue_dump( struct object *obj, int verbose )
113 struct async_queue *async_queue = (struct async_queue *)obj;
114 assert( obj->ops == &async_queue_ops );
115 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
118 static void async_queue_destroy( struct object *obj )
120 struct async_queue *async_queue = (struct async_queue *)obj;
121 assert( obj->ops == &async_queue_ops );
123 async_wake_up( async_queue, STATUS_HANDLES_CLOSED );
126 /* notifies client thread of new status of its async request */
127 /* destroys the server side of it */
128 static void async_terminate( struct async *async, unsigned int status )
132 memset( &data, 0, sizeof(data) );
133 data.type = APC_ASYNC_IO;
134 data.async_io.func = async->data.callback;
135 data.async_io.user = async->data.arg;
136 data.async_io.sb = async->data.iosb;
137 data.async_io.status = status;
138 thread_queue_apc( async->thread, &async->obj, &data );
140 if (async->timeout) remove_timeout_user( async->timeout );
141 async->timeout = NULL;
142 list_remove( &async->queue_entry );
143 release_object( async );
146 /* callback for timeout on an async request */
147 static void async_timeout( void *private )
149 struct async *async = private;
151 async->timeout = NULL;
152 async_terminate( async, STATUS_TIMEOUT );
155 /* create a new async queue for a given fd */
156 struct async_queue *create_async_queue( struct fd *fd )
158 struct async_queue *queue = alloc_object( &async_queue_ops );
163 list_init( &queue->queue );
168 /* create an async on a given queue of a fd */
169 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
171 struct event *event = NULL;
174 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
177 if (!(async = alloc_object( &async_ops )))
179 if (event) release_object( event );
183 async->thread = (struct thread *)grab_object( thread );
184 async->event = event;
186 async->timeout = NULL;
188 list_add_tail( &queue->queue, &async->queue_entry );
189 grab_object( async );
191 if (event) reset_event( event );
195 /* set the timeout of an async operation */
196 void async_set_timeout( struct async *async, const struct timeval *timeout )
198 if (async->timeout) remove_timeout_user( async->timeout );
199 if (timeout) async->timeout = add_timeout_user( timeout, async_timeout, async );
200 else async->timeout = NULL;
203 /* store the result of the client-side async callback */
204 void async_set_result( struct object *obj, unsigned int status )
206 struct async *async = (struct async *)obj;
208 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
210 if (status == STATUS_PENDING)
212 /* FIXME: restart the async operation */
219 data.type = APC_USER;
220 data.user.func = async->data.apc;
221 data.user.args[0] = (unsigned long)async->data.apc_arg;
222 data.user.args[1] = (unsigned long)async->data.iosb;
223 data.user.args[2] = 0;
224 thread_queue_apc( async->thread, NULL, &data );
226 if (async->event) set_event( async->event );
230 /* check if an async operation is waiting to be alerted */
231 int async_waiting( struct async_queue *queue )
233 return queue && !list_empty( &queue->queue );
236 /* wake up async operations on the queue */
237 void async_wake_up( struct async_queue *queue, unsigned int status )
239 struct list *ptr, *next;
243 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
245 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
246 async_terminate( async, status );
247 if (status == STATUS_ALERTED) break; /* only wake up the first one */