wintrust: Create a dummy context to force creation of MachineGuid registry key.
[wine.git] / server / async.c
blobd2da976969d221fbf6fc1c120d2690a8d9cad593
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_open_file, /* open_file */
69 no_close_handle, /* close_handle */
70 async_destroy /* destroy */
74 struct async_queue
76 struct object obj; /* object header */
77 struct fd *fd; /* file descriptor owning this queue */
78 struct completion *completion; /* completion associated with a recently closed file descriptor */
79 apc_param_t comp_key; /* completion key associated with a recently closed file descriptor */
80 struct list queue; /* queue of async objects */
83 static void async_queue_dump( struct object *obj, int verbose );
84 static void async_queue_destroy( struct object *obj );
86 static const struct object_ops async_queue_ops =
88 sizeof(struct async_queue), /* size */
89 async_queue_dump, /* dump */
90 no_get_type, /* get_type */
91 no_add_queue, /* add_queue */
92 NULL, /* remove_queue */
93 NULL, /* signaled */
94 NULL, /* satisfied */
95 no_signal, /* signal */
96 no_get_fd, /* get_fd */
97 no_map_access, /* map_access */
98 default_get_sd, /* get_sd */
99 default_set_sd, /* set_sd */
100 no_lookup_name, /* lookup_name */
101 no_open_file, /* open_file */
102 no_close_handle, /* close_handle */
103 async_queue_destroy /* destroy */
107 static inline void async_reselect( struct async *async )
109 if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
112 static void async_dump( struct object *obj, int verbose )
114 struct async *async = (struct async *)obj;
115 assert( obj->ops == &async_ops );
116 fprintf( stderr, "Async thread=%p\n", async->thread );
119 static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
121 struct async *async = (struct async *)obj;
122 assert( obj->ops == &async_ops );
123 return async->signaled;
126 static void async_destroy( struct object *obj )
128 struct async *async = (struct async *)obj;
129 assert( obj->ops == &async_ops );
131 list_remove( &async->queue_entry );
132 async_reselect( async );
134 if (async->timeout) remove_timeout_user( async->timeout );
135 if (async->event) release_object( async->event );
136 release_object( async->queue );
137 release_object( async->thread );
140 static void async_queue_dump( struct object *obj, int verbose )
142 struct async_queue *async_queue = (struct async_queue *)obj;
143 assert( obj->ops == &async_queue_ops );
144 fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
147 static void async_queue_destroy( struct object *obj )
149 struct async_queue *async_queue = (struct async_queue *)obj;
150 assert( obj->ops == &async_queue_ops );
151 if (async_queue->completion) release_object( async_queue->completion );
154 /* notifies client thread of new status of its async request */
155 void async_terminate( struct async *async, unsigned int status )
157 assert( status != STATUS_PENDING );
159 if (async->status != STATUS_PENDING)
161 /* already terminated, just update status */
162 async->status = status;
163 return;
166 async->status = status;
168 if (async->data.callback)
170 apc_call_t data;
172 memset( &data, 0, sizeof(data) );
173 data.type = APC_ASYNC_IO;
174 data.async_io.func = async->data.callback;
175 data.async_io.user = async->data.arg;
176 data.async_io.sb = async->data.iosb;
177 data.async_io.status = status;
178 thread_queue_apc( async->thread, &async->obj, &data );
180 else async_set_result( &async->obj, STATUS_SUCCESS, 0, 0, 0 );
182 async_reselect( async );
183 release_object( async ); /* so that it gets destroyed when the async is done */
186 /* callback for timeout on an async request */
187 static void async_timeout( void *private )
189 struct async *async = private;
191 async->timeout = NULL;
192 async_terminate( async, async->timeout_status );
195 /* create a new async queue for a given fd */
196 struct async_queue *create_async_queue( struct fd *fd )
198 struct async_queue *queue = alloc_object( &async_queue_ops );
200 if (queue)
202 queue->fd = fd;
203 queue->completion = NULL;
204 list_init( &queue->queue );
206 return queue;
209 /* free an async queue, cancelling all async operations */
210 void free_async_queue( struct async_queue *queue )
212 if (!queue) return;
213 if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
214 queue->fd = NULL;
215 async_wake_up( queue, STATUS_HANDLES_CLOSED );
216 release_object( queue );
219 /* create an async on a given queue of a fd */
220 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
222 struct event *event = NULL;
223 struct async *async;
225 if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
226 return NULL;
228 if (!(async = alloc_object( &async_ops )))
230 if (event) release_object( event );
231 return NULL;
234 async->thread = (struct thread *)grab_object( thread );
235 async->event = event;
236 async->status = STATUS_PENDING;
237 async->data = *data;
238 async->timeout = NULL;
239 async->queue = (struct async_queue *)grab_object( queue );
240 async->signaled = 0;
242 list_add_tail( &queue->queue, &async->queue_entry );
243 grab_object( async );
245 if (queue->fd) set_fd_signaled( queue->fd, 0 );
246 if (event) reset_event( event );
247 return async;
250 /* set the timeout of an async operation */
251 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
253 if (async->timeout) remove_timeout_user( async->timeout );
254 if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
255 else async->timeout = NULL;
256 async->timeout_status = status;
259 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
260 apc_param_t information )
262 if (queue->fd)
264 apc_param_t ckey;
265 struct completion *completion = fd_get_completion( queue->fd, &ckey );
267 if (completion)
269 add_completion( completion, ckey, cvalue, status, information );
270 release_object( completion );
273 else if (queue->completion) add_completion( queue->completion, queue->comp_key,
274 cvalue, status, information );
277 /* store the result of the client-side async callback */
278 void async_set_result( struct object *obj, unsigned int status, apc_param_t total,
279 client_ptr_t apc, client_ptr_t apc_arg )
281 struct async *async = (struct async *)obj;
283 if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
285 assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
287 if (status == STATUS_PENDING) /* restart it */
289 status = async->status;
290 async->status = STATUS_PENDING;
291 grab_object( async );
293 if (status != STATUS_ALERTED) /* it was terminated in the meantime */
294 async_terminate( async, status );
295 else
296 async_reselect( async );
298 else
300 if (async->timeout) remove_timeout_user( async->timeout );
301 async->timeout = NULL;
302 async->status = status;
303 if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
305 if (async->data.cvalue) add_async_completion( async->queue, async->data.cvalue, status, total );
306 if (apc)
308 apc_call_t data;
309 memset( &data, 0, sizeof(data) );
310 data.type = APC_USER;
311 data.user.func = apc;
312 data.user.args[0] = apc_arg;
313 data.user.args[1] = async->data.iosb;
314 data.user.args[2] = 0;
315 thread_queue_apc( async->thread, NULL, &data );
317 if (async->event) set_event( async->event );
318 else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
319 async->signaled = 1;
320 wake_up( &async->obj, 0 );
324 /* check if there are any queued async operations */
325 int async_queued( struct async_queue *queue )
327 return queue && list_head( &queue->queue );
330 /* check if an async operation is waiting to be alerted */
331 int async_waiting( struct async_queue *queue )
333 struct list *ptr;
334 struct async *async;
336 if (!queue) return 0;
337 if (!(ptr = list_head( &queue->queue ))) return 0;
338 async = LIST_ENTRY( ptr, struct async, queue_entry );
339 return async->status == STATUS_PENDING;
342 int async_wake_up_by( struct async_queue *queue, struct process *process,
343 struct thread *thread, client_ptr_t iosb, unsigned int status )
345 struct list *ptr, *next;
346 int woken = 0;
348 if (!queue || (!process && !thread && !iosb)) return 0;
350 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
352 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
353 if ( (!process || async->thread->process == process) &&
354 (!thread || async->thread == thread) &&
355 (!iosb || async->data.iosb == iosb) )
357 async_terminate( async, status );
358 woken++;
361 return woken;
364 /* wake up async operations on the queue */
365 void async_wake_up( struct async_queue *queue, unsigned int status )
367 struct list *ptr, *next;
369 if (!queue) return;
371 LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
373 struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
374 async_terminate( async, status );
375 if (status == STATUS_ALERTED) break; /* only wake up the first one */