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;
41 unsigned int timeout_status; /* status to report upon timeout */
43 async_data_t data; /* data for async I/O call */
46 static void async_dump( struct object *obj, int verbose );
47 static void async_destroy( struct object *obj );
49 static const struct object_ops async_ops =
51 sizeof(struct async), /* size */
52 async_dump, /* dump */
53 no_add_queue, /* add_queue */
54 NULL, /* remove_queue */
57 no_signal, /* signal */
58 no_get_fd, /* get_fd */
59 no_map_access, /* map_access */
60 no_lookup_name, /* lookup_name */
61 no_open_file, /* open_file */
62 no_close_handle, /* close_handle */
63 async_destroy /* destroy */
69 struct object obj; /* object header */
70 struct fd *fd; /* file descriptor owning this queue */
71 struct list queue; /* queue of async objects */
74 static void async_queue_dump( struct object *obj, int verbose );
75 static void async_queue_destroy( struct object *obj );
77 static const struct object_ops async_queue_ops =
79 sizeof(struct async_queue), /* size */
80 async_queue_dump, /* dump */
81 no_add_queue, /* add_queue */
82 NULL, /* remove_queue */
85 no_signal, /* signal */
86 no_get_fd, /* get_fd */
87 no_map_access, /* map_access */
88 no_lookup_name, /* lookup_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 async_queue_destroy /* destroy */
95 static void async_dump( struct object *obj, int verbose )
97 struct async *async = (struct async *)obj;
98 assert( obj->ops == &async_ops );
99 fprintf( stderr, "Async thread=%p\n", async->thread );
102 static void async_destroy( struct object *obj )
104 struct async *async = (struct async *)obj;
105 assert( obj->ops == &async_ops );
107 if (async->timeout) remove_timeout_user( async->timeout );
108 if (async->event) release_object( async->event );
109 release_object( async->thread );
112 static void async_queue_dump( struct object *obj, int verbose )
114 struct async_queue *async_queue = (struct async_queue *)obj;
115 assert( obj->ops == &async_queue_ops );
116 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
119 static void async_queue_destroy( struct object *obj )
121 struct async_queue *async_queue = (struct async_queue *)obj;
122 assert( obj->ops == &async_queue_ops );
124 async_wake_up( async_queue, STATUS_HANDLES_CLOSED );
127 /* notifies client thread of new status of its async request */
128 /* destroys the server side of it */
129 static void async_terminate( struct async *async, unsigned int status )
133 memset( &data, 0, sizeof(data) );
134 data.type = APC_ASYNC_IO;
135 data.async_io.func = async->data.callback;
136 data.async_io.user = async->data.arg;
137 data.async_io.sb = async->data.iosb;
138 data.async_io.status = status;
139 thread_queue_apc( async->thread, &async->obj, &data );
141 if (async->timeout) remove_timeout_user( async->timeout );
142 async->timeout = NULL;
143 list_remove( &async->queue_entry );
144 release_object( async );
147 /* callback for timeout on an async request */
148 static void async_timeout( void *private )
150 struct async *async = private;
152 async->timeout = NULL;
153 async_terminate( async, async->timeout_status );
156 /* create a new async queue for a given fd */
157 struct async_queue *create_async_queue( struct fd *fd )
159 struct async_queue *queue = alloc_object( &async_queue_ops );
164 list_init( &queue->queue );
169 /* create an async on a given queue of a fd */
170 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
172 struct event *event = NULL;
175 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
178 if (!(async = alloc_object( &async_ops )))
180 if (event) release_object( event );
184 async->thread = (struct thread *)grab_object( thread );
185 async->event = event;
187 async->timeout = NULL;
189 list_add_tail( &queue->queue, &async->queue_entry );
190 grab_object( async );
192 if (event) reset_event( event );
196 /* set the timeout of an async operation */
197 void async_set_timeout( struct async *async, const struct timeval *timeout, unsigned int status )
199 if (async->timeout) remove_timeout_user( async->timeout );
200 if (timeout) async->timeout = add_timeout_user( timeout, async_timeout, async );
201 else async->timeout = NULL;
202 async->timeout_status = status;
205 /* store the result of the client-side async callback */
206 void async_set_result( struct object *obj, unsigned int status )
208 struct async *async = (struct async *)obj;
210 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
212 if (status == STATUS_PENDING)
214 /* FIXME: restart the async operation */
221 data.type = APC_USER;
222 data.user.func = async->data.apc;
223 data.user.args[0] = (unsigned long)async->data.apc_arg;
224 data.user.args[1] = (unsigned long)async->data.iosb;
225 data.user.args[2] = 0;
226 thread_queue_apc( async->thread, NULL, &data );
228 if (async->event) set_event( async->event );
232 /* check if an async operation is waiting to be alerted */
233 int async_waiting( struct async_queue *queue )
235 return queue && !list_empty( &queue->queue );
238 /* wake up async operations on the queue */
239 void async_wake_up( struct async_queue *queue, unsigned int status )
241 struct list *ptr, *next;
245 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
247 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
248 async_terminate( async, status );
249 if (status == STATUS_ALERTED) break; /* only wake up the first one */