server: Allow async i/o operations to send completion messages.
[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_add_queue,              /* add_queue */
56     NULL,                      /* remove_queue */
57     NULL,                      /* signaled */
58     NULL,                      /* satisfied */
59     no_signal,                 /* signal */
60     no_get_fd,                 /* get_fd */
61     no_map_access,             /* map_access */
62     default_get_sd,            /* get_sd */
63     default_set_sd,            /* set_sd */
64     no_lookup_name,            /* lookup_name */
65     no_open_file,              /* open_file */
66     no_close_handle,           /* close_handle */
67     async_destroy              /* destroy */
68 };
69
70
71 struct async_queue
72 {
73     struct object        obj;             /* object header */
74     struct fd           *fd;              /* file descriptor owning this queue */
75     struct list          queue;           /* queue of async objects */
76 };
77
78 static void async_queue_dump( struct object *obj, int verbose );
79
80 static const struct object_ops async_queue_ops =
81 {
82     sizeof(struct async_queue),      /* size */
83     async_queue_dump,                /* dump */
84     no_add_queue,                    /* add_queue */
85     NULL,                            /* remove_queue */
86     NULL,                            /* signaled */
87     NULL,                            /* satisfied */
88     no_signal,                       /* signal */
89     no_get_fd,                       /* get_fd */
90     no_map_access,                   /* map_access */
91     default_get_sd,                  /* get_sd */
92     default_set_sd,                  /* set_sd */
93     no_lookup_name,                  /* lookup_name */
94     no_open_file,                    /* open_file */
95     no_close_handle,                 /* close_handle */
96     no_destroy                       /* destroy */
97 };
98
99
100 static inline void async_reselect( struct async *async )
101 {
102     if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
103 }
104
105 static void async_dump( struct object *obj, int verbose )
106 {
107     struct async *async = (struct async *)obj;
108     assert( obj->ops == &async_ops );
109     fprintf( stderr, "Async thread=%p\n", async->thread );
110 }
111
112 static void async_destroy( struct object *obj )
113 {
114     struct async *async = (struct async *)obj;
115     assert( obj->ops == &async_ops );
116
117     list_remove( &async->queue_entry );
118     async_reselect( async );
119
120     if (async->timeout) remove_timeout_user( async->timeout );
121     if (async->event) release_object( async->event );
122     release_object( async->queue );
123     release_object( async->thread );
124 }
125
126 static void async_queue_dump( struct object *obj, int verbose )
127 {
128     struct async_queue *async_queue = (struct async_queue *)obj;
129     assert( obj->ops == &async_queue_ops );
130     fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
131 }
132
133 /* notifies client thread of new status of its async request */
134 void async_terminate( struct async *async, unsigned int status )
135 {
136     apc_call_t data;
137
138     assert( status != STATUS_PENDING );
139
140     if (async->status != STATUS_PENDING)
141     {
142         /* already terminated, just update status */
143         async->status = status;
144         return;
145     }
146
147     /* send error completion event */
148     if (status != STATUS_ALERTED && async->data.cvalue && async->queue && async->queue->fd)
149         fd_add_completion( async->queue->fd, async->data.cvalue, status, 0 );
150
151     memset( &data, 0, sizeof(data) );
152     data.type            = APC_ASYNC_IO;
153     data.async_io.func   = async->data.callback;
154     data.async_io.user   = async->data.arg;
155     data.async_io.sb     = async->data.iosb;
156     data.async_io.status = status;
157     thread_queue_apc( async->thread, &async->obj, &data );
158     async->status = status;
159     async_reselect( async );
160     release_object( async );  /* so that it gets destroyed when the async is done */
161 }
162
163 /* callback for timeout on an async request */
164 static void async_timeout( void *private )
165 {
166     struct async *async = private;
167
168     async->timeout = NULL;
169     async_terminate( async, async->timeout_status );
170 }
171
172 /* create a new async queue for a given fd */
173 struct async_queue *create_async_queue( struct fd *fd )
174 {
175     struct async_queue *queue = alloc_object( &async_queue_ops );
176
177     if (queue)
178     {
179         queue->fd = fd;
180         list_init( &queue->queue );
181     }
182     return queue;
183 }
184
185 /* free an async queue, cancelling all async operations */
186 void free_async_queue( struct async_queue *queue )
187 {
188     if (!queue) return;
189     queue->fd = NULL;
190     async_wake_up( queue, STATUS_HANDLES_CLOSED );
191     release_object( queue );
192 }
193
194 /* create an async on a given queue of a fd */
195 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
196 {
197     struct event *event = NULL;
198     struct async *async;
199
200     if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
201         return NULL;
202
203     if (!(async = alloc_object( &async_ops )))
204     {
205         if (event) release_object( event );
206         return NULL;
207     }
208
209     async->thread  = (struct thread *)grab_object( thread );
210     async->event   = event;
211     async->status  = STATUS_PENDING;
212     async->data    = *data;
213     async->timeout = NULL;
214     async->queue   = (struct async_queue *)grab_object( queue );
215
216     list_add_tail( &queue->queue, &async->queue_entry );
217     grab_object( async );
218
219     if (queue->fd) set_fd_signaled( queue->fd, 0 );
220     if (event) reset_event( event );
221     return async;
222 }
223
224 /* set the timeout of an async operation */
225 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
226 {
227     if (async->timeout) remove_timeout_user( async->timeout );
228     if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
229     else async->timeout = NULL;
230     async->timeout_status = status;
231 }
232
233 /* store the result of the client-side async callback */
234 void async_set_result( struct object *obj, unsigned int status )
235 {
236     struct async *async = (struct async *)obj;
237
238     if (obj->ops != &async_ops) return;  /* in case the client messed up the APC results */
239
240     assert( async->status != STATUS_PENDING );  /* it must have been woken up if we get a result */
241
242     if (status == STATUS_PENDING)  /* restart it */
243     {
244         status = async->status;
245         async->status = STATUS_PENDING;
246         grab_object( async );
247
248         if (status != STATUS_ALERTED)  /* it was terminated in the meantime */
249             async_terminate( async, status );
250         else
251             async_reselect( async );
252     }
253     else
254     {
255         if (async->timeout) remove_timeout_user( async->timeout );
256         async->timeout = NULL;
257         async->status = status;
258         if (async->data.cvalue && async->queue && async->queue->fd)
259             fd_add_completion( async->queue->fd, async->data.cvalue, status, 0 ); /* TODO pass Information field */
260         if (async->data.apc)
261         {
262             apc_call_t data;
263             data.type         = APC_USER;
264             data.user.func    = async->data.apc;
265             data.user.args[0] = (unsigned long)async->data.arg;
266             data.user.args[1] = (unsigned long)async->data.iosb;
267             data.user.args[2] = 0;
268             thread_queue_apc( async->thread, NULL, &data );
269         }
270         if (async->event) set_event( async->event );
271         else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
272     }
273 }
274
275 /* check if an async operation is waiting to be alerted */
276 int async_waiting( struct async_queue *queue )
277 {
278     struct list *ptr;
279     struct async *async;
280
281     if (!queue) return 0;
282     if (!(ptr = list_head( &queue->queue ))) return 0;
283     async = LIST_ENTRY( ptr, struct async, queue_entry );
284     return async->status == STATUS_PENDING;
285 }
286
287 /* wake up async operations on the queue */
288 void async_wake_up( struct async_queue *queue, unsigned int status )
289 {
290     struct list *ptr, *next;
291
292     if (!queue) return;
293
294     LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
295     {
296         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
297         async_terminate( async, status );
298         if (status == STATUS_ALERTED) break;  /* only wake up the first one */
299     }
300 }