Release 1.5.29.
[wine] / server / async.c
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 */
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     struct event        *event;
45     async_data_t         data;            /* data for async I/O call */
46 };
47
48 static void async_dump( struct object *obj, int verbose );
49 static void async_destroy( struct object *obj );
50
51 static const struct object_ops async_ops =
52 {
53     sizeof(struct async),      /* size */
54     async_dump,                /* dump */
55     no_get_type,               /* get_type */
56     no_add_queue,              /* add_queue */
57     NULL,                      /* remove_queue */
58     NULL,                      /* signaled */
59     NULL,                      /* satisfied */
60     no_signal,                 /* signal */
61     no_get_fd,                 /* get_fd */
62     no_map_access,             /* map_access */
63     default_get_sd,            /* get_sd */
64     default_set_sd,            /* set_sd */
65     no_lookup_name,            /* lookup_name */
66     no_open_file,              /* open_file */
67     no_close_handle,           /* close_handle */
68     async_destroy              /* destroy */
69 };
70
71
72 struct async_queue
73 {
74     struct object        obj;             /* object header */
75     struct fd           *fd;              /* file descriptor owning this queue */
76     struct completion   *completion;      /* completion associated with a recently closed file descriptor */
77     apc_param_t          comp_key;        /* completion key associated with a recently closed file descriptor */
78     struct list          queue;           /* queue of async objects */
79 };
80
81 static void async_queue_dump( struct object *obj, int verbose );
82 static void async_queue_destroy( struct object *obj );
83
84 static const struct object_ops async_queue_ops =
85 {
86     sizeof(struct async_queue),      /* size */
87     async_queue_dump,                /* dump */
88     no_get_type,                     /* get_type */
89     no_add_queue,                    /* add_queue */
90     NULL,                            /* remove_queue */
91     NULL,                            /* signaled */
92     NULL,                            /* satisfied */
93     no_signal,                       /* signal */
94     no_get_fd,                       /* get_fd */
95     no_map_access,                   /* map_access */
96     default_get_sd,                  /* get_sd */
97     default_set_sd,                  /* set_sd */
98     no_lookup_name,                  /* lookup_name */
99     no_open_file,                    /* open_file */
100     no_close_handle,                 /* close_handle */
101     async_queue_destroy              /* destroy */
102 };
103
104
105 static inline void async_reselect( struct async *async )
106 {
107     if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
108 }
109
110 static void async_dump( struct object *obj, int verbose )
111 {
112     struct async *async = (struct async *)obj;
113     assert( obj->ops == &async_ops );
114     fprintf( stderr, "Async thread=%p\n", async->thread );
115 }
116
117 static void async_destroy( struct object *obj )
118 {
119     struct async *async = (struct async *)obj;
120     assert( obj->ops == &async_ops );
121
122     list_remove( &async->queue_entry );
123     async_reselect( async );
124
125     if (async->timeout) remove_timeout_user( async->timeout );
126     if (async->event) release_object( async->event );
127     release_object( async->queue );
128     release_object( async->thread );
129 }
130
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
138 static void async_queue_destroy( struct object *obj )
139 {
140     struct async_queue *async_queue = (struct async_queue *)obj;
141     assert( obj->ops == &async_queue_ops );
142     if (async_queue->completion) release_object( async_queue->completion );
143 }
144
145 /* notifies client thread of new status of its async request */
146 void async_terminate( struct async *async, unsigned int status )
147 {
148     apc_call_t data;
149
150     assert( status != STATUS_PENDING );
151
152     if (async->status != STATUS_PENDING)
153     {
154         /* already terminated, just update status */
155         async->status = status;
156         return;
157     }
158
159     memset( &data, 0, sizeof(data) );
160     data.type            = APC_ASYNC_IO;
161     data.async_io.func   = async->data.callback;
162     data.async_io.user   = async->data.arg;
163     data.async_io.sb     = async->data.iosb;
164     data.async_io.status = status;
165     thread_queue_apc( async->thread, &async->obj, &data );
166     async->status = status;
167     async_reselect( async );
168     release_object( async );  /* so that it gets destroyed when the async is done */
169 }
170
171 /* callback for timeout on an async request */
172 static void async_timeout( void *private )
173 {
174     struct async *async = private;
175
176     async->timeout = NULL;
177     async_terminate( async, async->timeout_status );
178 }
179
180 /* create a new async queue for a given fd */
181 struct async_queue *create_async_queue( struct fd *fd )
182 {
183     struct async_queue *queue = alloc_object( &async_queue_ops );
184
185     if (queue)
186     {
187         queue->fd = fd;
188         queue->completion = NULL;
189         list_init( &queue->queue );
190     }
191     return queue;
192 }
193
194 /* free an async queue, cancelling all async operations */
195 void free_async_queue( struct async_queue *queue )
196 {
197     if (!queue) return;
198     if (queue->fd) queue->completion = fd_get_completion( queue->fd, &queue->comp_key );
199     queue->fd = NULL;
200     async_wake_up( queue, STATUS_HANDLES_CLOSED );
201     release_object( queue );
202 }
203
204 /* create an async on a given queue of a fd */
205 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
206 {
207     struct event *event = NULL;
208     struct async *async;
209
210     if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
211         return NULL;
212
213     if (!(async = alloc_object( &async_ops )))
214     {
215         if (event) release_object( event );
216         return NULL;
217     }
218
219     async->thread  = (struct thread *)grab_object( thread );
220     async->event   = event;
221     async->status  = STATUS_PENDING;
222     async->data    = *data;
223     async->timeout = NULL;
224     async->queue   = (struct async_queue *)grab_object( queue );
225
226     list_add_tail( &queue->queue, &async->queue_entry );
227     grab_object( async );
228
229     if (queue->fd) set_fd_signaled( queue->fd, 0 );
230     if (event) reset_event( event );
231     return async;
232 }
233
234 /* set the timeout of an async operation */
235 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
236 {
237     if (async->timeout) remove_timeout_user( async->timeout );
238     if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
239     else async->timeout = NULL;
240     async->timeout_status = status;
241 }
242
243 static void add_async_completion( struct async_queue *queue, apc_param_t cvalue, unsigned int status,
244                                   unsigned int information )
245 {
246     if (status == STATUS_MORE_PROCESSING_REQUIRED)
247         return; /* The async callback has successfully finished but no completion should be reported */
248
249     if (queue->fd)
250     {
251         apc_param_t ckey;
252         struct completion *completion = fd_get_completion( queue->fd, &ckey );
253
254         if (completion)
255         {
256             add_completion( completion, ckey, cvalue, status, information );
257             release_object( completion );
258         }
259     }
260     else if (queue->completion) add_completion( queue->completion, queue->comp_key,
261                                                 cvalue, status, information );
262 }
263
264 /* store the result of the client-side async callback */
265 void async_set_result( struct object *obj, unsigned int status, unsigned int total, client_ptr_t apc )
266 {
267     struct async *async = (struct async *)obj;
268
269     if (obj->ops != &async_ops) return;  /* in case the client messed up the APC results */
270
271     assert( async->status != STATUS_PENDING );  /* it must have been woken up if we get a result */
272
273     if (status == STATUS_PENDING)  /* restart it */
274     {
275         status = async->status;
276         async->status = STATUS_PENDING;
277         grab_object( async );
278
279         if (status != STATUS_ALERTED)  /* it was terminated in the meantime */
280             async_terminate( async, status );
281         else
282             async_reselect( async );
283     }
284     else
285     {
286         if (async->timeout) remove_timeout_user( async->timeout );
287         async->timeout = NULL;
288         async->status = status;
289         if (async->data.cvalue) add_async_completion( async->queue, async->data.cvalue, status, total );
290         if (apc)
291         {
292             apc_call_t data;
293             memset( &data, 0, sizeof(data) );
294             data.type         = APC_USER;
295             data.user.func    = apc;
296             data.user.args[0] = async->data.arg;
297             data.user.args[1] = async->data.iosb;
298             data.user.args[2] = 0;
299             thread_queue_apc( async->thread, NULL, &data );
300         }
301         if (async->event) set_event( async->event );
302         else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
303     }
304 }
305
306 /* check if there are any queued async operations */
307 int async_queued( struct async_queue *queue )
308 {
309     return queue && list_head( &queue->queue );
310 }
311
312 /* check if an async operation is waiting to be alerted */
313 int async_waiting( struct async_queue *queue )
314 {
315     struct list *ptr;
316     struct async *async;
317
318     if (!queue) return 0;
319     if (!(ptr = list_head( &queue->queue ))) return 0;
320     async = LIST_ENTRY( ptr, struct async, queue_entry );
321     return async->status == STATUS_PENDING;
322 }
323
324 int async_wake_up_by( struct async_queue *queue, struct process *process,
325                       struct thread *thread, client_ptr_t iosb, unsigned int status )
326 {
327     struct list *ptr, *next;
328     int woken = 0;
329
330     if (!queue || (!process && !thread && !iosb)) return 0;
331
332     LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
333     {
334         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
335         if ( (!process || async->thread->process == process) &&
336              (!thread || async->thread == thread) &&
337              (!iosb || async->data.iosb == iosb) )
338         {
339             async_terminate( async, status );
340             woken++;
341         }
342     }
343     return woken;
344 }
345
346 /* wake up async operations on the queue */
347 void async_wake_up( struct async_queue *queue, unsigned int status )
348 {
349     struct list *ptr, *next;
350
351     if (!queue) return;
352
353     LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
354     {
355         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
356         async_terminate( async, status );
357         if (status == STATUS_ALERTED) break;  /* only wake up the first one */
358     }
359 }