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
27 #define WIN32_NO_STATUS
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 */
45 async_data_t data
; /* data for async I/O call */
48 static void async_dump( struct object
*obj
, int verbose
);
49 static void async_destroy( struct object
*obj
);
51 static const struct object_ops async_ops
=
53 sizeof(struct async
), /* size */
54 async_dump
, /* dump */
55 no_add_queue
, /* add_queue */
56 NULL
, /* remove_queue */
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 */
73 struct object obj
; /* object header */
74 struct fd
*fd
; /* file descriptor owning this queue */
75 struct list queue
; /* queue of async objects */
78 static void async_queue_dump( struct object
*obj
, int verbose
);
80 static const struct object_ops async_queue_ops
=
82 sizeof(struct async_queue
), /* size */
83 async_queue_dump
, /* dump */
84 no_add_queue
, /* add_queue */
85 NULL
, /* remove_queue */
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 */
100 static inline void async_reselect( struct async
*async
)
102 if (async
->queue
->fd
) fd_reselect_async( async
->queue
->fd
, async
->queue
);
105 static void async_dump( struct object
*obj
, int verbose
)
107 struct async
*async
= (struct async
*)obj
;
108 assert( obj
->ops
== &async_ops
);
109 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
112 static void async_destroy( struct object
*obj
)
114 struct async
*async
= (struct async
*)obj
;
115 assert( obj
->ops
== &async_ops
);
117 list_remove( &async
->queue_entry
);
118 async_reselect( async
);
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
);
126 static void async_queue_dump( struct object
*obj
, int verbose
)
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
);
133 /* notifies client thread of new status of its async request */
134 void async_terminate( struct async
*async
, unsigned int status
)
138 assert( status
!= STATUS_PENDING
);
140 if (async
->status
!= STATUS_PENDING
)
142 /* already terminated, just update status */
143 async
->status
= status
;
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 );
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 */
163 /* callback for timeout on an async request */
164 static void async_timeout( void *private )
166 struct async
*async
= private;
168 async
->timeout
= NULL
;
169 async_terminate( async
, async
->timeout_status
);
172 /* create a new async queue for a given fd */
173 struct async_queue
*create_async_queue( struct fd
*fd
)
175 struct async_queue
*queue
= alloc_object( &async_queue_ops
);
180 list_init( &queue
->queue
);
185 /* free an async queue, cancelling all async operations */
186 void free_async_queue( struct async_queue
*queue
)
190 async_wake_up( queue
, STATUS_HANDLES_CLOSED
);
191 release_object( queue
);
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
)
197 struct event
*event
= NULL
;
200 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
203 if (!(async
= alloc_object( &async_ops
)))
205 if (event
) release_object( event
);
209 async
->thread
= (struct thread
*)grab_object( thread
);
210 async
->event
= event
;
211 async
->status
= STATUS_PENDING
;
213 async
->timeout
= NULL
;
214 async
->queue
= (struct async_queue
*)grab_object( queue
);
216 list_add_tail( &queue
->queue
, &async
->queue_entry
);
217 grab_object( async
);
219 if (queue
->fd
) set_fd_signaled( queue
->fd
, 0 );
220 if (event
) reset_event( event
);
224 /* set the timeout of an async operation */
225 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
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
;
233 /* store the result of the client-side async callback */
234 void async_set_result( struct object
*obj
, unsigned int status
, unsigned long total
)
236 struct async
*async
= (struct async
*)obj
;
238 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
240 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
242 if (status
== STATUS_PENDING
) /* restart it */
244 status
= async
->status
;
245 async
->status
= STATUS_PENDING
;
246 grab_object( async
);
248 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
249 async_terminate( async
, status
);
251 async_reselect( async
);
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
, total
);
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
);
270 if (async
->event
) set_event( async
->event
);
271 else if (async
->queue
->fd
) set_fd_signaled( async
->queue
->fd
, 1 );
275 /* check if an async operation is waiting to be alerted */
276 int async_waiting( struct async_queue
*queue
)
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
;
287 /* wake up async operations on the queue */
288 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
290 struct list
*ptr
, *next
;
294 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
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 */