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