server: Fix the handling of the signaled status for file descriptors.
[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     struct timeout_user *timeout;
42     unsigned int         timeout_status;  /* status to report upon timeout */
43     struct event        *event;
44     async_data_t         data;            /* data for async I/O call */
45 };
46
47 static void async_dump( struct object *obj, int verbose );
48 static void async_destroy( struct object *obj );
49
50 static const struct object_ops async_ops =
51 {
52     sizeof(struct async),      /* size */
53     async_dump,                /* dump */
54     no_add_queue,              /* add_queue */
55     NULL,                      /* remove_queue */
56     NULL,                      /* signaled */
57     NULL,                      /* satisfied */
58     no_signal,                 /* signal */
59     no_get_fd,                 /* get_fd */
60     no_map_access,             /* map_access */
61     no_lookup_name,            /* lookup_name */
62     no_open_file,              /* open_file */
63     no_close_handle,           /* close_handle */
64     async_destroy              /* destroy */
65 };
66
67
68 struct async_queue
69 {
70     struct object        obj;             /* object header */
71     struct fd           *fd;              /* file descriptor owning this queue */
72     struct list          queue;           /* queue of async objects */
73 };
74
75 static void async_queue_dump( struct object *obj, int verbose );
76
77 static const struct object_ops async_queue_ops =
78 {
79     sizeof(struct async_queue),      /* size */
80     async_queue_dump,                /* dump */
81     no_add_queue,                    /* add_queue */
82     NULL,                            /* remove_queue */
83     NULL,                            /* signaled */
84     NULL,                            /* satisfied */
85     no_signal,                       /* signal */
86     no_get_fd,                       /* get_fd */
87     no_map_access,                   /* map_access */
88     no_lookup_name,                  /* lookup_name */
89     no_open_file,                    /* open_file */
90     no_close_handle,                 /* close_handle */
91     no_destroy                       /* destroy */
92 };
93
94
95 static void async_dump( struct object *obj, int verbose )
96 {
97     struct async *async = (struct async *)obj;
98     assert( obj->ops == &async_ops );
99     fprintf( stderr, "Async thread=%p\n", async->thread );
100 }
101
102 static void async_destroy( struct object *obj )
103 {
104     struct async *async = (struct async *)obj;
105     assert( obj->ops == &async_ops );
106
107     if (async->timeout) remove_timeout_user( async->timeout );
108     if (async->event) release_object( async->event );
109     release_object( async->queue );
110     async->queue = NULL;
111     release_object( async->thread );
112 }
113
114 static void async_queue_dump( struct object *obj, int verbose )
115 {
116     struct async_queue *async_queue = (struct async_queue *)obj;
117     assert( obj->ops == &async_queue_ops );
118     fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
119 }
120
121 /* notifies client thread of new status of its async request */
122 /* destroys the server side of it */
123 static void async_terminate( struct async *async, unsigned int status )
124 {
125     apc_call_t data;
126
127     memset( &data, 0, sizeof(data) );
128     data.type            = APC_ASYNC_IO;
129     data.async_io.func   = async->data.callback;
130     data.async_io.user   = async->data.arg;
131     data.async_io.sb     = async->data.iosb;
132     data.async_io.status = status;
133     thread_queue_apc( async->thread, &async->obj, &data );
134
135     if (async->timeout) remove_timeout_user( async->timeout );
136     async->timeout = NULL;
137     list_remove( &async->queue_entry );
138     release_object( async );
139 }
140
141 /* callback for timeout on an async request */
142 static void async_timeout( void *private )
143 {
144     struct async *async = private;
145
146     async->timeout = NULL;
147     async_terminate( async, async->timeout_status );
148 }
149
150 /* create a new async queue for a given fd */
151 struct async_queue *create_async_queue( struct fd *fd )
152 {
153     struct async_queue *queue = alloc_object( &async_queue_ops );
154
155     if (queue)
156     {
157         queue->fd = fd;
158         list_init( &queue->queue );
159     }
160     return queue;
161 }
162
163 /* free an async queue, cancelling all async operations */
164 void free_async_queue( struct async_queue *queue )
165 {
166     if (!queue) return;
167     queue->fd = NULL;
168     async_wake_up( queue, STATUS_HANDLES_CLOSED );
169     release_object( queue );
170 }
171
172 /* create an async on a given queue of a fd */
173 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
174 {
175     struct event *event = NULL;
176     struct async *async;
177
178     if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
179         return NULL;
180
181     if (!(async = alloc_object( &async_ops )))
182     {
183         if (event) release_object( event );
184         return NULL;
185     }
186
187     async->thread = (struct thread *)grab_object( thread );
188     async->event = event;
189     async->data = *data;
190     async->timeout = NULL;
191     async->queue = (struct async_queue *)grab_object( queue );
192
193     list_add_tail( &queue->queue, &async->queue_entry );
194     grab_object( async );
195
196     if (queue->fd) set_fd_signaled( queue->fd, 0 );
197     if (event) reset_event( event );
198     return async;
199 }
200
201 /* set the timeout of an async operation */
202 void async_set_timeout( struct async *async, const struct timeval *timeout, unsigned int status )
203 {
204     if (async->timeout) remove_timeout_user( async->timeout );
205     if (timeout) async->timeout = add_timeout_user( timeout, async_timeout, async );
206     else async->timeout = NULL;
207     async->timeout_status = status;
208 }
209
210 /* store the result of the client-side async callback */
211 void async_set_result( struct object *obj, unsigned int status )
212 {
213     struct async *async = (struct async *)obj;
214
215     if (obj->ops != &async_ops) return;  /* in case the client messed up the APC results */
216
217     if (status == STATUS_PENDING)
218     {
219         /* FIXME: restart the async operation */
220     }
221     else
222     {
223         if (async->data.apc)
224         {
225             apc_call_t data;
226             data.type         = APC_USER;
227             data.user.func    = async->data.apc;
228             data.user.args[0] = (unsigned long)async->data.apc_arg;
229             data.user.args[1] = (unsigned long)async->data.iosb;
230             data.user.args[2] = 0;
231             thread_queue_apc( async->thread, NULL, &data );
232         }
233         if (async->event) set_event( async->event );
234         else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
235     }
236 }
237
238 /* check if an async operation is waiting to be alerted */
239 int async_waiting( struct async_queue *queue )
240 {
241     return queue && !list_empty( &queue->queue );
242 }
243
244 /* wake up async operations on the queue */
245 void async_wake_up( struct async_queue *queue, unsigned int status )
246 {
247     struct list *ptr, *next;
248
249     if (!queue) return;
250
251     LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
252     {
253         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
254         async_terminate( async, status );
255         if (status == STATUS_ALERTED) break;  /* only wake up the first one */
256     }
257 }