Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Server-side async I/O support |
| 3 | * |
| 4 | * Copyright (C) 2007 Alexandre Julliard |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | |
| 26 | #include "ntstatus.h" |
| 27 | #define WIN32_NO_STATUS |
| 28 | #include "windef.h" |
| 29 | #include "winternl.h" |
| 30 | |
| 31 | #include "object.h" |
| 32 | #include "file.h" |
| 33 | #include "request.h" |
| 34 | |
| 35 | struct async |
| 36 | { |
| 37 | struct object obj; /* object header */ |
| 38 | struct thread *thread; /* owning thread */ |
Alexandre Julliard | b2cba95 | 2007-04-03 19:36:07 +0200 | [diff] [blame] | 39 | struct list queue_entry; /* entry in async queue list */ |
| 40 | struct async_queue *queue; /* queue containing this async */ |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 41 | unsigned int status; /* current status */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 42 | struct timeout_user *timeout; |
Alexandre Julliard | 4e5c703 | 2007-04-03 19:12:31 +0200 | [diff] [blame] | 43 | unsigned int timeout_status; /* status to report upon timeout */ |
Alexandre Julliard | fa4679f | 2007-03-21 14:27:52 +0100 | [diff] [blame] | 44 | struct event *event; |
Andrey Turkin | 3afbee5 | 2007-12-17 22:06:17 +0300 | [diff] [blame] | 45 | struct completion *completion; |
Alexandre Julliard | dc7f170 | 2008-12-15 13:29:38 +0100 | [diff] [blame] | 46 | apc_param_t comp_key; |
Alexandre Julliard | 111610c | 2007-03-20 20:21:12 +0100 | [diff] [blame] | 47 | async_data_t data; /* data for async I/O call */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static void async_dump( struct object *obj, int verbose ); |
| 51 | static void async_destroy( struct object *obj ); |
| 52 | |
| 53 | static const struct object_ops async_ops = |
| 54 | { |
| 55 | sizeof(struct async), /* size */ |
| 56 | async_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 57 | no_get_type, /* get_type */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 58 | no_add_queue, /* add_queue */ |
| 59 | NULL, /* remove_queue */ |
| 60 | NULL, /* signaled */ |
| 61 | NULL, /* satisfied */ |
| 62 | no_signal, /* signal */ |
| 63 | no_get_fd, /* get_fd */ |
| 64 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 65 | default_get_sd, /* get_sd */ |
| 66 | default_set_sd, /* set_sd */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 67 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 68 | no_open_file, /* open_file */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 69 | no_close_handle, /* close_handle */ |
| 70 | async_destroy /* destroy */ |
| 71 | }; |
| 72 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 73 | |
| 74 | struct async_queue |
| 75 | { |
| 76 | struct object obj; /* object header */ |
| 77 | struct fd *fd; /* file descriptor owning this queue */ |
| 78 | struct list queue; /* queue of async objects */ |
| 79 | }; |
| 80 | |
| 81 | static void async_queue_dump( struct object *obj, int verbose ); |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 82 | |
| 83 | static const struct object_ops async_queue_ops = |
| 84 | { |
| 85 | sizeof(struct async_queue), /* size */ |
| 86 | async_queue_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 87 | no_get_type, /* get_type */ |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 88 | no_add_queue, /* add_queue */ |
| 89 | NULL, /* remove_queue */ |
| 90 | NULL, /* signaled */ |
| 91 | NULL, /* satisfied */ |
| 92 | no_signal, /* signal */ |
| 93 | no_get_fd, /* get_fd */ |
| 94 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 95 | default_get_sd, /* get_sd */ |
| 96 | default_set_sd, /* set_sd */ |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 97 | no_lookup_name, /* lookup_name */ |
| 98 | no_open_file, /* open_file */ |
| 99 | no_close_handle, /* close_handle */ |
Alexandre Julliard | b2cba95 | 2007-04-03 19:36:07 +0200 | [diff] [blame] | 100 | no_destroy /* destroy */ |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 104 | static inline void async_reselect( struct async *async ) |
| 105 | { |
| 106 | if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue ); |
| 107 | } |
| 108 | |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 109 | static void async_dump( struct object *obj, int verbose ) |
| 110 | { |
| 111 | struct async *async = (struct async *)obj; |
| 112 | assert( obj->ops == &async_ops ); |
| 113 | fprintf( stderr, "Async thread=%p\n", async->thread ); |
| 114 | } |
| 115 | |
| 116 | static void async_destroy( struct object *obj ) |
| 117 | { |
| 118 | struct async *async = (struct async *)obj; |
| 119 | assert( obj->ops == &async_ops ); |
| 120 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 121 | list_remove( &async->queue_entry ); |
| 122 | async_reselect( async ); |
| 123 | |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 124 | if (async->timeout) remove_timeout_user( async->timeout ); |
Alexandre Julliard | fa4679f | 2007-03-21 14:27:52 +0100 | [diff] [blame] | 125 | if (async->event) release_object( async->event ); |
Andrey Turkin | 3afbee5 | 2007-12-17 22:06:17 +0300 | [diff] [blame] | 126 | if (async->completion) release_object( async->completion ); |
Alexandre Julliard | b2cba95 | 2007-04-03 19:36:07 +0200 | [diff] [blame] | 127 | release_object( async->queue ); |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 128 | release_object( async->thread ); |
| 129 | } |
| 130 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 131 | static void async_queue_dump( struct object *obj, int verbose ) |
| 132 | { |
| 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 ); |
| 136 | } |
| 137 | |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 138 | /* notifies client thread of new status of its async request */ |
Alexandre Julliard | 61e08b3 | 2007-05-08 20:37:21 +0200 | [diff] [blame] | 139 | void async_terminate( struct async *async, unsigned int status ) |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 140 | { |
| 141 | apc_call_t data; |
| 142 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 143 | assert( status != STATUS_PENDING ); |
| 144 | |
| 145 | if (async->status != STATUS_PENDING) |
| 146 | { |
| 147 | /* already terminated, just update status */ |
| 148 | async->status = status; |
| 149 | return; |
| 150 | } |
| 151 | |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 152 | memset( &data, 0, sizeof(data) ); |
| 153 | data.type = APC_ASYNC_IO; |
Alexandre Julliard | 111610c | 2007-03-20 20:21:12 +0100 | [diff] [blame] | 154 | data.async_io.func = async->data.callback; |
| 155 | data.async_io.user = async->data.arg; |
| 156 | data.async_io.sb = async->data.iosb; |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 157 | data.async_io.status = status; |
| 158 | thread_queue_apc( async->thread, &async->obj, &data ); |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 159 | async->status = status; |
| 160 | async_reselect( async ); |
| 161 | release_object( async ); /* so that it gets destroyed when the async is done */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* callback for timeout on an async request */ |
| 165 | static void async_timeout( void *private ) |
| 166 | { |
| 167 | struct async *async = private; |
| 168 | |
| 169 | async->timeout = NULL; |
Alexandre Julliard | 4e5c703 | 2007-04-03 19:12:31 +0200 | [diff] [blame] | 170 | async_terminate( async, async->timeout_status ); |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 173 | /* create a new async queue for a given fd */ |
| 174 | struct async_queue *create_async_queue( struct fd *fd ) |
| 175 | { |
| 176 | struct async_queue *queue = alloc_object( &async_queue_ops ); |
| 177 | |
| 178 | if (queue) |
| 179 | { |
| 180 | queue->fd = fd; |
| 181 | list_init( &queue->queue ); |
| 182 | } |
| 183 | return queue; |
| 184 | } |
| 185 | |
Alexandre Julliard | b2cba95 | 2007-04-03 19:36:07 +0200 | [diff] [blame] | 186 | /* free an async queue, cancelling all async operations */ |
| 187 | void free_async_queue( struct async_queue *queue ) |
| 188 | { |
| 189 | if (!queue) return; |
| 190 | queue->fd = NULL; |
| 191 | async_wake_up( queue, STATUS_HANDLES_CLOSED ); |
| 192 | release_object( queue ); |
| 193 | } |
| 194 | |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 195 | /* create an async on a given queue of a fd */ |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 196 | struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data ) |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 197 | { |
Alexandre Julliard | fa4679f | 2007-03-21 14:27:52 +0100 | [diff] [blame] | 198 | struct event *event = NULL; |
| 199 | struct async *async; |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 200 | |
Alexandre Julliard | fa4679f | 2007-03-21 14:27:52 +0100 | [diff] [blame] | 201 | if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE ))) |
| 202 | return NULL; |
| 203 | |
| 204 | if (!(async = alloc_object( &async_ops ))) |
| 205 | { |
| 206 | if (event) release_object( event ); |
| 207 | return NULL; |
| 208 | } |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 209 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 210 | async->thread = (struct thread *)grab_object( thread ); |
| 211 | async->event = event; |
| 212 | async->status = STATUS_PENDING; |
| 213 | async->data = *data; |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 214 | async->timeout = NULL; |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 215 | async->queue = (struct async_queue *)grab_object( queue ); |
Alexandre Julliard | c69468d | 2007-12-18 19:44:00 +0100 | [diff] [blame] | 216 | async->completion = NULL; |
Alexandre Julliard | 50171c5 | 2009-03-02 20:34:39 +0100 | [diff] [blame] | 217 | if (queue->fd) async->completion = fd_get_completion( queue->fd, &async->comp_key ); |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 218 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 219 | list_add_tail( &queue->queue, &async->queue_entry ); |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 220 | grab_object( async ); |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 221 | |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 222 | if (queue->fd) set_fd_signaled( queue->fd, 0 ); |
Alexandre Julliard | fa4679f | 2007-03-21 14:27:52 +0100 | [diff] [blame] | 223 | if (event) reset_event( event ); |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 224 | return async; |
| 225 | } |
| 226 | |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 227 | /* set the timeout of an async operation */ |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 228 | void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status ) |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 229 | { |
| 230 | if (async->timeout) remove_timeout_user( async->timeout ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 231 | if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async ); |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 232 | else async->timeout = NULL; |
Alexandre Julliard | 4e5c703 | 2007-04-03 19:12:31 +0200 | [diff] [blame] | 233 | async->timeout_status = status; |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 236 | /* store the result of the client-side async callback */ |
Alexandre Julliard | 6db2010 | 2008-12-30 21:09:41 +0100 | [diff] [blame] | 237 | void async_set_result( struct object *obj, unsigned int status, unsigned int total, client_ptr_t apc ) |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 238 | { |
| 239 | struct async *async = (struct async *)obj; |
| 240 | |
| 241 | if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */ |
| 242 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 243 | assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */ |
| 244 | |
| 245 | if (status == STATUS_PENDING) /* restart it */ |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 246 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 247 | status = async->status; |
| 248 | async->status = STATUS_PENDING; |
| 249 | grab_object( async ); |
| 250 | |
| 251 | if (status != STATUS_ALERTED) /* it was terminated in the meantime */ |
| 252 | async_terminate( async, status ); |
| 253 | else |
| 254 | async_reselect( async ); |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 258 | if (async->timeout) remove_timeout_user( async->timeout ); |
| 259 | async->timeout = NULL; |
| 260 | async->status = status; |
Andrey Turkin | 3afbee5 | 2007-12-17 22:06:17 +0300 | [diff] [blame] | 261 | if (async->completion && async->data.cvalue) |
| 262 | add_completion( async->completion, async->comp_key, async->data.cvalue, status, total ); |
Alexandre Julliard | f507ccb | 2008-12-30 20:51:55 +0100 | [diff] [blame] | 263 | if (apc) |
Alexandre Julliard | c16eb8e | 2007-03-27 16:42:27 +0200 | [diff] [blame] | 264 | { |
| 265 | apc_call_t data; |
Rob Shearman | 0b0c75e | 2007-12-27 13:50:35 +0000 | [diff] [blame] | 266 | memset( &data, 0, sizeof(data) ); |
Alexandre Julliard | c16eb8e | 2007-03-27 16:42:27 +0200 | [diff] [blame] | 267 | data.type = APC_USER; |
Alexandre Julliard | f507ccb | 2008-12-30 20:51:55 +0100 | [diff] [blame] | 268 | data.user.func = apc; |
Alexandre Julliard | 6db2010 | 2008-12-30 21:09:41 +0100 | [diff] [blame] | 269 | data.user.args[0] = async->data.arg; |
| 270 | data.user.args[1] = async->data.iosb; |
Alexandre Julliard | c16eb8e | 2007-03-27 16:42:27 +0200 | [diff] [blame] | 271 | data.user.args[2] = 0; |
| 272 | thread_queue_apc( async->thread, NULL, &data ); |
| 273 | } |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 274 | if (async->event) set_event( async->event ); |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 275 | else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 ); |
Alexandre Julliard | 8adce77 | 2007-03-21 14:28:23 +0100 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 279 | /* check if an async operation is waiting to be alerted */ |
| 280 | int async_waiting( struct async_queue *queue ) |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 281 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 282 | struct list *ptr; |
| 283 | struct async *async; |
| 284 | |
| 285 | if (!queue) return 0; |
| 286 | if (!(ptr = list_head( &queue->queue ))) return 0; |
| 287 | async = LIST_ENTRY( ptr, struct async, queue_entry ); |
| 288 | return async->status == STATUS_PENDING; |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 291 | /* wake up async operations on the queue */ |
| 292 | void async_wake_up( struct async_queue *queue, unsigned int status ) |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 293 | { |
| 294 | struct list *ptr, *next; |
| 295 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 296 | if (!queue) return; |
| 297 | |
| 298 | LIST_FOR_EACH_SAFE( ptr, next, &queue->queue ) |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 299 | { |
| 300 | struct async *async = LIST_ENTRY( ptr, struct async, queue_entry ); |
| 301 | async_terminate( async, status ); |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 302 | if (status == STATUS_ALERTED) break; /* only wake up the first one */ |
Alexandre Julliard | 06db705 | 2007-03-20 19:23:59 +0100 | [diff] [blame] | 303 | } |
| 304 | } |