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 struct completion
*completion
;
47 async_data_t data
; /* data for async I/O call */
50 static void async_dump( struct object
*obj
, int verbose
);
51 static void async_destroy( struct object
*obj
);
53 static const struct object_ops async_ops
=
55 sizeof(struct async
), /* size */
56 async_dump
, /* dump */
57 no_get_type
, /* get_type */
58 no_add_queue
, /* add_queue */
59 NULL
, /* remove_queue */
62 no_signal
, /* signal */
63 no_get_fd
, /* get_fd */
64 no_map_access
, /* map_access */
65 default_get_sd
, /* get_sd */
66 default_set_sd
, /* set_sd */
67 no_lookup_name
, /* lookup_name */
68 no_open_file
, /* open_file */
69 no_close_handle
, /* close_handle */
70 async_destroy
/* destroy */
76 struct object obj
; /* object header */
77 struct fd
*fd
; /* file descriptor owning this queue */
78 struct list queue
; /* queue of async objects */
81 static void async_queue_dump( struct object
*obj
, int verbose
);
83 static const struct object_ops async_queue_ops
=
85 sizeof(struct async_queue
), /* size */
86 async_queue_dump
, /* dump */
87 no_get_type
, /* get_type */
88 no_add_queue
, /* add_queue */
89 NULL
, /* remove_queue */
92 no_signal
, /* signal */
93 no_get_fd
, /* get_fd */
94 no_map_access
, /* map_access */
95 default_get_sd
, /* get_sd */
96 default_set_sd
, /* set_sd */
97 no_lookup_name
, /* lookup_name */
98 no_open_file
, /* open_file */
99 no_close_handle
, /* close_handle */
100 no_destroy
/* destroy */
104 static inline void async_event( struct async
*async
, int finished
)
106 if (async
->queue
->fd
) fd_async_event( async
->queue
->fd
, async
->queue
, async
, async
->status
, finished
);
109 static void async_dump( struct object
*obj
, int verbose
)
111 struct async
*async
= (struct async
*)obj
;
112 assert( obj
->ops
== &async_ops
);
113 fprintf( stderr
, "Async thread=%p\n", async
->thread
);
116 static void async_destroy( struct object
*obj
)
118 struct async
*async
= (struct async
*)obj
;
119 assert( obj
->ops
== &async_ops
);
121 list_remove( &async
->queue_entry
);
122 async_event( async
, TRUE
);
124 if (async
->timeout
) remove_timeout_user( async
->timeout
);
125 if (async
->event
) release_object( async
->event
);
126 if (async
->completion
) release_object( async
->completion
);
127 release_object( async
->queue
);
128 release_object( async
->thread
);
131 static void async_queue_dump( struct object
*obj
, int verbose
)
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
);
138 /* notifies client thread of new status of its async request */
139 void async_terminate( struct async
*async
, unsigned int status
)
143 assert( status
!= STATUS_PENDING
);
145 if (async
->status
!= STATUS_PENDING
)
147 /* already terminated, just update status */
148 async
->status
= status
;
152 if (async
->queue
->fd
)
153 status
= fd_async_terminated(async
->queue
->fd
, async
->queue
, async
, status
);
155 if (status
!= STATUS_PENDING
)
157 memset( &data
, 0, sizeof(data
) );
158 data
.type
= APC_ASYNC_IO
;
159 data
.async_io
.func
= async
->data
.callback
;
160 data
.async_io
.user
= async
->data
.arg
;
161 data
.async_io
.sb
= async
->data
.iosb
;
162 data
.async_io
.status
= status
;
163 thread_queue_apc( async
->thread
, &async
->obj
, &data
);
164 async
->status
= status
;
165 async_event( async
, FALSE
);
166 release_object( async
); /* so that it gets destroyed when the async is done */
169 async_event( async
, FALSE
);
172 /* callback for timeout on an async request */
173 static void async_timeout( void *private )
175 struct async
*async
= private;
177 async
->timeout
= NULL
;
178 async_terminate( async
, async
->timeout_status
);
181 /* create a new async queue for a given fd */
182 struct async_queue
*create_async_queue( struct fd
*fd
)
184 struct async_queue
*queue
= alloc_object( &async_queue_ops
);
189 list_init( &queue
->queue
);
194 /* free an async queue, cancelling all async operations */
195 void free_async_queue( struct async_queue
*queue
)
199 async_wake_up( queue
, STATUS_HANDLES_CLOSED
);
200 release_object( queue
);
203 /* create an async on a given queue of a fd */
204 struct async
*create_async( struct thread
*thread
, struct async_queue
*queue
, const async_data_t
*data
)
206 struct event
*event
= NULL
;
209 if (data
->event
&& !(event
= get_event_obj( thread
->process
, data
->event
, EVENT_MODIFY_STATE
)))
212 if (!(async
= alloc_object( &async_ops
)))
214 if (event
) release_object( event
);
218 async
->thread
= (struct thread
*)grab_object( thread
);
219 async
->event
= event
;
220 async
->status
= STATUS_PENDING
;
222 async
->timeout
= NULL
;
223 async
->queue
= (struct async_queue
*)grab_object( queue
);
224 async
->completion
= NULL
;
225 if (queue
->fd
) async
->completion
= fd_get_completion( queue
->fd
, &async
->comp_key
);
227 list_add_tail( &queue
->queue
, &async
->queue_entry
);
228 grab_object( async
);
230 if (queue
->fd
) set_fd_signaled( queue
->fd
, 0 );
231 if (event
) reset_event( event
);
235 /* set the timeout of an async operation */
236 void async_set_timeout( struct async
*async
, timeout_t timeout
, unsigned int status
)
238 if (async
->timeout
) remove_timeout_user( async
->timeout
);
239 if (timeout
!= TIMEOUT_INFINITE
) async
->timeout
= add_timeout_user( timeout
, async_timeout
, async
);
240 else async
->timeout
= NULL
;
241 async
->timeout_status
= status
;
244 /* store the result of the client-side async callback */
245 void async_set_result( struct object
*obj
, unsigned int status
, unsigned int total
, client_ptr_t apc
)
247 struct async
*async
= (struct async
*)obj
;
249 if (obj
->ops
!= &async_ops
) return; /* in case the client messed up the APC results */
251 assert( async
->status
!= STATUS_PENDING
); /* it must have been woken up if we get a result */
253 if (status
== STATUS_PENDING
) /* restart it */
255 status
= async
->status
;
256 async
->status
= STATUS_PENDING
;
257 grab_object( async
);
259 if (status
!= STATUS_ALERTED
) /* it was terminated in the meantime */
260 async_terminate( async
, status
);
262 async_event( async
, FALSE
);
266 if (async
->timeout
) remove_timeout_user( async
->timeout
);
267 async
->timeout
= NULL
;
268 async
->status
= status
;
269 if (async
->completion
&& async
->data
.cvalue
)
270 add_completion( async
->completion
, async
->comp_key
, async
->data
.cvalue
, status
, total
);
274 memset( &data
, 0, sizeof(data
) );
275 data
.type
= APC_USER
;
276 data
.user
.func
= apc
;
277 data
.user
.args
[0] = async
->data
.arg
;
278 data
.user
.args
[1] = async
->data
.iosb
;
279 data
.user
.args
[2] = 0;
280 thread_queue_apc( async
->thread
, NULL
, &data
);
282 if (async
->event
) set_event( async
->event
);
283 else if (async
->queue
->fd
) set_fd_signaled( async
->queue
->fd
, 1 );
287 /* check if there are any queued async operations */
288 int async_queued( struct async_queue
*queue
)
290 return queue
&& list_head( &queue
->queue
);
293 /* check if an async operation is waiting to be alerted */
294 int async_waiting( struct async_queue
*queue
)
299 if (!queue
) return 0;
300 if (!(ptr
= list_head( &queue
->queue
))) return 0;
301 async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
302 return async
->status
== STATUS_PENDING
;
305 int async_wake_up_by( struct async_queue
*queue
, struct process
*process
,
306 struct thread
*thread
, client_ptr_t iosb
, unsigned int status
)
308 struct list
*ptr
, *next
;
311 if (!queue
|| (!process
&& !thread
&& !iosb
)) return 0;
313 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
315 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
316 if ( (!process
|| async
->thread
->process
== process
) &&
317 (!thread
|| async
->thread
== thread
) &&
318 (!iosb
|| async
->data
.iosb
== iosb
) )
320 async_terminate( async
, status
);
327 /* wake up async operations on the queue */
328 void async_wake_up( struct async_queue
*queue
, unsigned int status
)
330 struct list
*ptr
, *next
;
334 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->queue
)
336 struct async
*async
= LIST_ENTRY( ptr
, struct async
, queue_entry
);
337 async_terminate( async
, status
);
338 if (status
== STATUS_ALERTED
) break; /* only wake up the first one */