jscript: Simplify jsstr_release implementation.
[wine.git] / server / async.c
blob64aa27a2e27eeb5483e791cdf939d0770583424d
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 int signaled;
45 struct event *event;
46 async_data_t data; /* data for async I/O call */
49 static void async_dump( struct object *obj, int verbose );
50 static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
51 static void async_destroy( struct object *obj );
53 static const struct object_ops async_ops =
55 sizeof(struct async), /* size */
56 async_dump, /* dump */
57 no_get_type, /* get_type */
58 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 async_signaled, /* signaled */
61 no_satisfied, /* satisfied */
62 no_signal, /* signal */
63 no_get_fd, /* get_fd */
64 no_map_access, /* map_access */
65 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
67 no_lookup_name, /* lookup_name */
68 no_link_name, /* link_name */
69 NULL, /* unlink_name */
70 no_open_file, /* open_file */
71 no_close_handle, /* close_handle */
72 async_destroy /* destroy */
76 struct async_queue
78 struct object obj; /* object header */
79 struct fd *fd; /* file descriptor owning this queue */
80 struct completion *completion; /* completion associated with a recently closed file descriptor */
81 apc_param_t comp_key; /* completion key associated with a recently closed file descriptor */
82 struct list queue; /* queue of async objects */
85 static void async_queue_dump( struct object *obj, int verbose );
86 static void async_queue_destroy( struct object *obj );
88 static const struct object_ops async_queue_ops =
90 sizeof(struct async_queue), /* size */
91 async_queue_dump, /* dump */
92 no_get_type, /* get_type */
93 no_add_queue, /* add_queue */
94 NULL, /* remove_queue */
95 NULL, /* signaled */
96 NULL, /* satisfied */
97 no_signal, /* signal */
98 no_get_fd, /* get_fd */
99 no_map_access, /* map_access */
100 default_get_sd, /* get_sd */
101 default_set_sd, /* set_sd */
102 no_lookup_name, /* lookup_name */
103 no_link_name, /* link_name */
104 NULL, /* unlink_name */
105 no_open_file, /* open_file */
106 no_close_handle, /* close_handle */
107 async_queue_destroy /* destroy */
111 static inline void async_reselect( struct async *async )
113 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
116 static void async_dump( struct object *obj, int verbose )
118 struct async *async = (struct async *)obj;
119 assert( obj->ops == &async_ops );
120 fprintf( stderr, "Async thread=%p\n", async->thread );
123 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
125 struct async *async = (struct async *)obj;
126 assert( obj->ops == &async_ops );
127 return async->signaled;
130 static void async_destroy( struct object *obj )
132 struct async *async = (struct async *)obj;
133 assert( obj->ops == &async_ops );
135 list_remove( &async->queue_entry );
136 async_reselect( async );
138 if (async->timeout) remove_timeout_user( async->timeout );
139 if (async->event) release_object( async->event );
140 release_object( async->queue );
141 release_object( async->thread );
144 static void async_queue_dump( struct object *obj, int verbose )
146 struct async_queue *async_queue = (struct async_queue *)obj;
147 assert( obj->ops == &async_queue_ops );
148 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
151 static void async_queue_destroy( struct object *obj )
153 struct async_queue *async_queue = (struct async_queue *)obj;
154 assert( obj->ops == &async_queue_ops );
155 if (async_queue->completion) release_object( async_queue->completion );
158 /* notifies client thread of new status of its async request */
159 void async_terminate( struct async *async, unsigned int status )
161 assert( status != STATUS_PENDING );
163 if (async->status != STATUS_PENDING)
165 /* already terminated, just update status */
166 async->status = status;
167 return;
170 async->status = status;
172 if (async->data.callback)
174 apc_call_t data;
176 memset( &data, 0, sizeof(data) );
177 data.type = APC_ASYNC_IO;
178 data.async_io.func = async->data.callback;
179 data.async_io.user = async->data.arg;
180 data.async_io.sb = async->data.iosb;
181 data.async_io.status = status;
182 thread_queue_apc( async->thread, &async->obj, &data );
184 else async_set_result( &async->obj, STATUS_SUCCESS, 0, 0, 0 );
186 async_reselect( async );
187 release_object( async ); /* so that it gets destroyed when the async is done */
190 /* callback for timeout on an async request */
191 static void async_timeout( void *private )
193 struct async *async = private;
195 async->timeout = NULL;
196 async_terminate( async, async->timeout_status );
199 /* create a new async queue for a given fd */
200 struct async_queue *create_async_queue( struct fd *fd )
202 struct async_queue *queue = alloc_object( &async_queue_ops );
204 if (queue)
206 queue->fd = fd;
207 queue->completion = NULL;
208 list_init( &queue->queue );
210 return queue;
213 /* free an async queue, cancelling all async operations */
214 void free_async_queue( struct async_queue *queue )
216 if (!queue) return;
217 if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
218 queue->fd = NULL;
219 async_wake_up( queue, STATUS_HANDLES_CLOSED );
220 release_object( queue );
223 /* create an async on a given queue of a fd */
224 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
226 struct event *event = NULL;
227 struct async *async;
229 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
230 return NULL;
232 if (!(async = alloc_object( &async_ops )))
234 if (event) release_object( event );
235 return NULL;
238 async->thread = (struct thread *)grab_object( thread );
239 async->event = event;
240 async->status = STATUS_PENDING;
241 async->data = *data;
242 async->timeout = NULL;
243 async->queue = (struct async_queue *)grab_object( queue );
244 async->signaled = 0;
246 list_add_tail( &queue->queue, &async->queue_entry );
247 grab_object( async );
249 if (queue->fd) set_fd_signaled( queue->fd, 0 );
250 if (event) reset_event( event );
251 return async;
254 /* set the timeout of an async operation */
255 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
257 if (async->timeout) remove_timeout_user( async->timeout );
258 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
259 else async->timeout = NULL;
260 async->timeout_status = status;
263 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
264 apc_param_t information )
266 if (queue->fd)
268 apc_param_t ckey;
269 struct completion *completion = fd_get_completion( queue->fd, &ckey );
271 if (completion)
273 add_completion( completion, ckey, cvalue, status, information );
274 release_object( completion );
277 else if (queue->completion) add_completion( queue->completion, queue->comp_key,
278 cvalue, status, information );
281 /* store the result of the client-side async callback */
282 void async_set_result( struct object *obj, unsigned int status, apc_param_t total,
283 client_ptr_t apc, client_ptr_t apc_arg )
285 struct async *async = (struct async *)obj;
287 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
289 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
291 if (status == STATUS_PENDING) /* restart it */
293 status = async->status;
294 async->status = STATUS_PENDING;
295 grab_object( async );
297 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
298 async_terminate( async, status );
299 else
300 async_reselect( async );
302 else
304 if (async->timeout) remove_timeout_user( async->timeout );
305 async->timeout = NULL;
306 async->status = status;
307 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
309 if (async->data.cvalue) add_async_completion( async->queue, async->data.cvalue, status, total );
310 if (apc)
312 apc_call_t data;
313 memset( &data, 0, sizeof(data) );
314 data.type = APC_USER;
315 data.user.func = apc;
316 data.user.args[0] = apc_arg;
317 data.user.args[1] = async->data.iosb;
318 data.user.args[2] = 0;
319 thread_queue_apc( async->thread, NULL, &data );
321 if (async->event) set_event( async->event );
322 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
323 async->signaled = 1;
324 wake_up( &async->obj, 0 );
328 /* check if there are any queued async operations */
329 int async_queued( struct async_queue *queue )
331 return queue && list_head( &queue->queue );
334 /* check if an async operation is waiting to be alerted */
335 int async_waiting( struct async_queue *queue )
337 struct list *ptr;
338 struct async *async;
340 if (!queue) return 0;
341 if (!(ptr = list_head( &queue->queue ))) return 0;
342 async = LIST_ENTRY( ptr, struct async, queue_entry );
343 return async->status == STATUS_PENDING;
346 int async_wake_up_by( struct async_queue *queue, struct process *process,
347 struct thread *thread, client_ptr_t iosb, unsigned int status )
349 struct list *ptr, *next;
350 int woken = 0;
352 if (!queue || (!process && !thread && !iosb)) return 0;
354 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
356 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
357 if ( (!process || async->thread->process == process) &&
358 (!thread || async->thread == thread) &&
359 (!iosb || async->data.iosb == iosb) )
361 async_terminate( async, status );
362 woken++;
365 return woken;
368 /* wake up async operations on the queue */
369 void async_wake_up( struct async_queue *queue, unsigned int status )
371 struct list *ptr, *next;
373 if (!queue) return;
375 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
377 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
378 async_terminate( async, status );
379 if (status == STATUS_ALERTED) break; /* only wake up the first one */