async.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include "ntstatus.h"
  25. #define WIN32_NO_STATUS
  26. #include "windef.h"
  27. #include "winternl.h"
  28. #include "object.h"
  29. #include "file.h"
  30. #include "request.h"
  31. #include "process.h"
  32. #include "handle.h"
  33. struct async
  34. {
  35. struct object obj; /* object header */
  36. struct thread *thread; /* owning thread */
  37. struct list queue_entry; /* entry in async queue list */
  38. struct list process_entry; /* entry in process list */
  39. struct async_queue *queue; /* queue containing this async */
  40. struct fd *fd; /* fd associated with an unqueued 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. struct iosb *iosb; /* I/O status block */
  46. obj_handle_t wait_handle; /* pre-allocated wait handle */
  47. unsigned int initial_status; /* status returned from initial request */
  48. unsigned int signaled :1;
  49. unsigned int pending :1; /* request successfully queued, but pending */
  50. unsigned int direct_result :1;/* a flag if we're passing result directly from request instead of APC */
  51. unsigned int alerted :1; /* fd is signaled, but we are waiting for client-side I/O */
  52. unsigned int terminated :1; /* async has been terminated */
  53. unsigned int canceled :1; /* have we already queued cancellation for this async? */
  54. unsigned int unknown_status :1; /* initial status is not known yet */
  55. unsigned int blocking :1; /* async is blocking */
  56. struct completion *completion; /* completion associated with fd */
  57. apc_param_t comp_key; /* completion key associated with fd */
  58. unsigned int comp_flags; /* completion flags */
  59. async_completion_callback completion_callback; /* callback to be called on completion */
  60. void *completion_callback_private; /* argument to completion_callback */
  61. };
  62. static void async_dump( struct object *obj, int verbose );
  63. static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
  64. static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
  65. static void async_destroy( struct object *obj );
  66. static const struct object_ops async_ops =
  67. {
  68. sizeof(struct async), /* size */
  69. &no_type, /* type */
  70. async_dump, /* dump */
  71. add_queue, /* add_queue */
  72. remove_queue, /* remove_queue */
  73. async_signaled, /* signaled */
  74. async_satisfied, /* satisfied */
  75. no_signal, /* signal */
  76. no_get_fd, /* get_fd */
  77. default_map_access, /* map_access */
  78. default_get_sd, /* get_sd */
  79. default_set_sd, /* set_sd */
  80. no_get_full_name, /* get_full_name */
  81. no_lookup_name, /* lookup_name */
  82. no_link_name, /* link_name */
  83. NULL, /* unlink_name */
  84. no_open_file, /* open_file */
  85. no_kernel_obj_list, /* get_kernel_obj_list */
  86. no_close_handle, /* close_handle */
  87. async_destroy /* destroy */
  88. };
  89. static inline void async_reselect( struct async *async )
  90. {
  91. if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
  92. }
  93. static void async_dump( struct object *obj, int verbose )
  94. {
  95. struct async *async = (struct async *)obj;
  96. assert( obj->ops == &async_ops );
  97. fprintf( stderr, "Async thread=%p\n", async->thread );
  98. }
  99. static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
  100. {
  101. struct async *async = (struct async *)obj;
  102. assert( obj->ops == &async_ops );
  103. return async->signaled;
  104. }
  105. static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
  106. {
  107. struct async *async = (struct async *)obj;
  108. assert( obj->ops == &async_ops );
  109. /* we only return an async handle for asyncs created via create_request_async() */
  110. assert( async->iosb );
  111. if (async->direct_result)
  112. {
  113. async_set_result( &async->obj, async->iosb->status, async->iosb->result );
  114. async->direct_result = 0;
  115. }
  116. if (async->initial_status == STATUS_PENDING && async->blocking)
  117. set_wait_status( entry, async->iosb->status );
  118. else
  119. set_wait_status( entry, async->initial_status );
  120. /* close wait handle here to avoid extra server round trip */
  121. if (async->wait_handle)
  122. {
  123. close_handle( async->thread->process, async->wait_handle );
  124. async->wait_handle = 0;
  125. }
  126. }
  127. static void async_destroy( struct object *obj )
  128. {
  129. struct async *async = (struct async *)obj;
  130. assert( obj->ops == &async_ops );
  131. list_remove( &async->process_entry );
  132. if (async->queue)
  133. {
  134. list_remove( &async->queue_entry );
  135. async_reselect( async );
  136. }
  137. else if (async->fd) release_object( async->fd );
  138. if (async->timeout) remove_timeout_user( async->timeout );
  139. if (async->completion) release_object( async->completion );
  140. if (async->event) release_object( async->event );
  141. if (async->iosb) release_object( async->iosb );
  142. release_object( async->thread );
  143. }
  144. /* notifies client thread of new status of its async request */
  145. void async_terminate( struct async *async, unsigned int status )
  146. {
  147. struct iosb *iosb = async->iosb;
  148. if (async->terminated) return;
  149. async->terminated = 1;
  150. if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
  151. if (status == STATUS_ALERTED)
  152. async->alerted = 1;
  153. /* if no APC could be queued (e.g. the process is terminated),
  154. * thread_queue_apc() may trigger async_set_result(), which may drop the
  155. * last reference to the async, so grab a temporary reference here */
  156. grab_object( async );
  157. if (!async->direct_result)
  158. {
  159. apc_call_t data;
  160. memset( &data, 0, sizeof(data) );
  161. data.type = APC_ASYNC_IO;
  162. data.async_io.user = async->data.user;
  163. data.async_io.result = iosb ? iosb->result : 0;
  164. /* this can happen if the initial status was unknown (i.e. for device
  165. * files). the client should not fill the IOSB in this case; pass it as
  166. * NULL to communicate that.
  167. * note that we check the IOSB status and not the initial status */
  168. if (NT_ERROR( status ) && (!is_fd_overlapped( async->fd ) || !async->pending))
  169. data.async_io.sb = 0;
  170. else
  171. data.async_io.sb = async->data.iosb;
  172. /* if there is output data, the client needs to make an extra request
  173. * to retrieve it; use STATUS_ALERTED to signal this case */
  174. if (iosb && iosb->out_data)
  175. data.async_io.status = STATUS_ALERTED;
  176. else
  177. data.async_io.status = status;
  178. thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
  179. }
  180. async_reselect( async );
  181. release_object( async );
  182. }
  183. /* callback for timeout on an async request */
  184. static void async_timeout( void *private )
  185. {
  186. struct async *async = private;
  187. async->timeout = NULL;
  188. async_terminate( async, async->timeout_status );
  189. }
  190. /* free an async queue, cancelling all async operations */
  191. void free_async_queue( struct async_queue *queue )
  192. {
  193. struct async *async, *next;
  194. LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
  195. {
  196. if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  197. async->fd = NULL;
  198. async_terminate( async, STATUS_HANDLES_CLOSED );
  199. async->queue = NULL;
  200. release_object( &async->obj );
  201. }
  202. }
  203. void queue_async( struct async_queue *queue, struct async *async )
  204. {
  205. /* fd will be set to NULL in free_async_queue when fd is destroyed */
  206. release_object( async->fd );
  207. async->queue = queue;
  208. grab_object( async );
  209. list_add_tail( &queue->queue, &async->queue_entry );
  210. set_fd_signaled( async->fd, 0 );
  211. }
  212. /* create an async on a given queue of a fd */
  213. struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
  214. {
  215. struct event *event = NULL;
  216. struct async *async;
  217. if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
  218. return NULL;
  219. if (!(async = alloc_object( &async_ops )))
  220. {
  221. if (event) release_object( event );
  222. return NULL;
  223. }
  224. async->thread = (struct thread *)grab_object( thread );
  225. async->event = event;
  226. async->data = *data;
  227. async->timeout = NULL;
  228. async->queue = NULL;
  229. async->fd = (struct fd *)grab_object( fd );
  230. async->initial_status = STATUS_PENDING;
  231. async->signaled = 0;
  232. async->pending = 1;
  233. async->wait_handle = 0;
  234. async->direct_result = 0;
  235. async->alerted = 0;
  236. async->terminated = 0;
  237. async->canceled = 0;
  238. async->unknown_status = 0;
  239. async->blocking = !is_fd_overlapped( fd );
  240. async->completion = fd_get_completion( fd, &async->comp_key );
  241. async->comp_flags = 0;
  242. async->completion_callback = NULL;
  243. async->completion_callback_private = NULL;
  244. if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
  245. else async->iosb = NULL;
  246. list_add_head( &thread->process->asyncs, &async->process_entry );
  247. if (event) reset_event( event );
  248. if (async->completion && data->apc)
  249. {
  250. release_object( async );
  251. set_error( STATUS_INVALID_PARAMETER );
  252. return NULL;
  253. }
  254. return async;
  255. }
  256. /* set the initial status of an async whose status was previously unknown
  257. * the initial status may be STATUS_PENDING */
  258. void async_set_initial_status( struct async *async, unsigned int status )
  259. {
  260. async->initial_status = status;
  261. async->unknown_status = 0;
  262. }
  263. void set_async_pending( struct async *async )
  264. {
  265. if (!async->terminated)
  266. async->pending = 1;
  267. }
  268. void async_wake_obj( struct async *async )
  269. {
  270. assert( !async->unknown_status );
  271. if (!async->blocking)
  272. {
  273. async->signaled = 1;
  274. wake_up( &async->obj, 0 );
  275. }
  276. }
  277. static void async_call_completion_callback( struct async *async )
  278. {
  279. if (async->completion_callback)
  280. async->completion_callback( async->completion_callback_private );
  281. async->completion_callback = NULL;
  282. }
  283. /* return async object status and wait handle to client */
  284. obj_handle_t async_handoff( struct async *async, data_size_t *result, int force_blocking )
  285. {
  286. async->blocking = force_blocking || async->blocking;
  287. if (async->unknown_status)
  288. {
  289. /* even the initial status is not known yet */
  290. set_error( STATUS_PENDING );
  291. return async->wait_handle;
  292. }
  293. if (get_error() == STATUS_ALERTED)
  294. {
  295. /* give the client opportunity to complete synchronously. after the
  296. * client performs the I/O, it reports the result back to the server
  297. * via the set_async_direct_result request. if it turns out that the
  298. * I/O request is not actually immediately satiable, the client may
  299. * then choose to re-queue the async by reporting STATUS_PENDING
  300. * instead.
  301. *
  302. * since we're deferring the initial I/O (to the client), we mark the
  303. * async as having unknown initial status (unknown_status = 1). note
  304. * that we don't reuse async_set_unknown_status() here. this is because
  305. * the one responsible for performing the I/O is not the device driver,
  306. * but instead the client that requested the I/O in the first place.
  307. *
  308. * also, async_set_unknown_status() would set direct_result to zero
  309. * forcing APC_ASYNC_IO to fire in async_terminate(), which is not
  310. * useful due to subtle semantic differences between synchronous and
  311. * asynchronous completion.
  312. */
  313. async->unknown_status = 1;
  314. async_terminate( async, STATUS_ALERTED );
  315. return async->wait_handle;
  316. }
  317. async->initial_status = get_error();
  318. if (!async->pending && NT_ERROR( get_error() ))
  319. {
  320. async->iosb->status = get_error();
  321. async_call_completion_callback( async );
  322. close_handle( async->thread->process, async->wait_handle );
  323. async->wait_handle = 0;
  324. return 0;
  325. }
  326. if (get_error() != STATUS_PENDING)
  327. {
  328. /* status and data are already set and returned */
  329. async_terminate( async, get_error() );
  330. }
  331. else if (async->iosb->status != STATUS_PENDING)
  332. {
  333. /* result is already available in iosb, return it */
  334. if (async->iosb->out_data)
  335. {
  336. set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
  337. async->iosb->out_data = NULL;
  338. }
  339. }
  340. if (async->iosb->status != STATUS_PENDING)
  341. {
  342. if (result) *result = async->iosb->result;
  343. async->signaled = 1;
  344. }
  345. else
  346. {
  347. async->direct_result = 0;
  348. async->pending = 1;
  349. if (!async->blocking)
  350. {
  351. close_handle( async->thread->process, async->wait_handle);
  352. async->wait_handle = 0;
  353. }
  354. }
  355. async->initial_status = async->iosb->status;
  356. set_error( async->iosb->status );
  357. return async->wait_handle;
  358. }
  359. /* complete a request-based async with a pre-allocated buffer */
  360. void async_request_complete( struct async *async, unsigned int status, data_size_t result,
  361. data_size_t out_size, void *out_data )
  362. {
  363. struct iosb *iosb = async_get_iosb( async );
  364. /* the async may have already been canceled */
  365. if (iosb->status != STATUS_PENDING)
  366. {
  367. release_object( iosb );
  368. free( out_data );
  369. return;
  370. }
  371. iosb->status = status;
  372. iosb->result = result;
  373. iosb->out_data = out_data;
  374. iosb->out_size = out_size;
  375. release_object( iosb );
  376. async_terminate( async, status );
  377. }
  378. /* complete a request-based async */
  379. void async_request_complete_alloc( struct async *async, unsigned int status, data_size_t result,
  380. data_size_t out_size, const void *out_data )
  381. {
  382. void *out_data_copy = NULL;
  383. if (out_size && !(out_data_copy = memdup( out_data, out_size )))
  384. {
  385. async_terminate( async, STATUS_NO_MEMORY );
  386. return;
  387. }
  388. async_request_complete( async, status, result, out_size, out_data_copy );
  389. }
  390. /* mark an async as having unknown initial status */
  391. void async_set_unknown_status( struct async *async )
  392. {
  393. async->unknown_status = 1;
  394. async->direct_result = 0;
  395. }
  396. /* set the timeout of an async operation */
  397. void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
  398. {
  399. if (async->timeout) remove_timeout_user( async->timeout );
  400. if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
  401. else async->timeout = NULL;
  402. async->timeout_status = status;
  403. }
  404. /* set a callback to be notified when the async is completed */
  405. void async_set_completion_callback( struct async *async, async_completion_callback func, void *private )
  406. {
  407. async->completion_callback = func;
  408. async->completion_callback_private = private;
  409. }
  410. static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
  411. apc_param_t information )
  412. {
  413. if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  414. if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
  415. }
  416. /* store the result of the client-side async callback */
  417. void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
  418. {
  419. struct async *async = (struct async *)obj;
  420. if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
  421. assert( async->terminated ); /* it must have been woken up if we get a result */
  422. if (async->unknown_status) async_set_initial_status( async, status );
  423. if (async->alerted && status == STATUS_PENDING) /* restart it */
  424. {
  425. async->terminated = 0;
  426. async->alerted = 0;
  427. async_reselect( async );
  428. }
  429. else
  430. {
  431. if (async->timeout) remove_timeout_user( async->timeout );
  432. async->timeout = NULL;
  433. async->terminated = 1;
  434. if (async->iosb) async->iosb->status = status;
  435. /* don't signal completion if the async failed synchronously
  436. * this can happen if the initial status was unknown (i.e. for device files)
  437. * note that we check the IOSB status here, not the initial status */
  438. if (async->pending || !NT_ERROR( status ))
  439. {
  440. if (async->data.apc)
  441. {
  442. apc_call_t data;
  443. memset( &data, 0, sizeof(data) );
  444. data.type = APC_USER;
  445. data.user.func = async->data.apc;
  446. data.user.args[0] = async->data.apc_context;
  447. data.user.args[1] = async->data.iosb;
  448. data.user.args[2] = 0;
  449. thread_queue_apc( NULL, async->thread, NULL, &data );
  450. }
  451. else if (async->data.apc_context && (async->pending ||
  452. !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
  453. {
  454. add_async_completion( async, async->data.apc_context, status, total );
  455. }
  456. if (async->event) set_event( async->event );
  457. else if (async->fd) set_fd_signaled( async->fd, 1 );
  458. }
  459. if (!async->signaled)
  460. {
  461. async->signaled = 1;
  462. wake_up( &async->obj, 0 );
  463. }
  464. async_call_completion_callback( async );
  465. if (async->queue)
  466. {
  467. list_remove( &async->queue_entry );
  468. async_reselect( async );
  469. async->fd = NULL;
  470. async->queue = NULL;
  471. release_object( async );
  472. }
  473. }
  474. }
  475. /* check if an async operation is waiting to be alerted */
  476. int async_waiting( struct async_queue *queue )
  477. {
  478. struct list *ptr;
  479. struct async *async;
  480. if (!(ptr = list_head( &queue->queue ))) return 0;
  481. async = LIST_ENTRY( ptr, struct async, queue_entry );
  482. return !async->terminated;
  483. }
  484. static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
  485. {
  486. struct async *async;
  487. int woken = 0;
  488. /* FIXME: it would probably be nice to replace the "canceled" flag with a
  489. * single LIST_FOR_EACH_ENTRY_SAFE, but currently cancelling an async can
  490. * cause other asyncs to be removed via async_reselect() */
  491. restart:
  492. LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
  493. {
  494. if (async->terminated || async->canceled) continue;
  495. if ((!obj || (get_fd_user( async->fd ) == obj)) &&
  496. (!thread || async->thread == thread) &&
  497. (!iosb || async->data.iosb == iosb))
  498. {
  499. async->canceled = 1;
  500. fd_cancel_async( async->fd, async );
  501. woken++;
  502. goto restart;
  503. }
  504. }
  505. return woken;
  506. }
  507. void cancel_process_asyncs( struct process *process )
  508. {
  509. cancel_async( process, NULL, NULL, 0 );
  510. }
  511. /* wake up async operations on the queue */
  512. void async_wake_up( struct async_queue *queue, unsigned int status )
  513. {
  514. struct list *ptr, *next;
  515. LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
  516. {
  517. struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
  518. async_terminate( async, status );
  519. if (status == STATUS_ALERTED) break; /* only wake up the first one */
  520. }
  521. }
  522. static void iosb_dump( struct object *obj, int verbose );
  523. static void iosb_destroy( struct object *obj );
  524. static const struct object_ops iosb_ops =
  525. {
  526. sizeof(struct iosb), /* size */
  527. &no_type, /* type */
  528. iosb_dump, /* dump */
  529. no_add_queue, /* add_queue */
  530. NULL, /* remove_queue */
  531. NULL, /* signaled */
  532. NULL, /* satisfied */
  533. no_signal, /* signal */
  534. no_get_fd, /* get_fd */
  535. default_map_access, /* map_access */
  536. default_get_sd, /* get_sd */
  537. default_set_sd, /* set_sd */
  538. no_get_full_name, /* get_full_name */
  539. no_lookup_name, /* lookup_name */
  540. no_link_name, /* link_name */
  541. NULL, /* unlink_name */
  542. no_open_file, /* open_file */
  543. no_kernel_obj_list, /* get_kernel_obj_list */
  544. no_close_handle, /* close_handle */
  545. iosb_destroy /* destroy */
  546. };
  547. static void iosb_dump( struct object *obj, int verbose )
  548. {
  549. assert( obj->ops == &iosb_ops );
  550. fprintf( stderr, "I/O status block\n" );
  551. }
  552. static void iosb_destroy( struct object *obj )
  553. {
  554. struct iosb *iosb = (struct iosb *)obj;
  555. free( iosb->in_data );
  556. free( iosb->out_data );
  557. }
  558. /* allocate iosb struct */
  559. static struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
  560. {
  561. struct iosb *iosb;
  562. if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
  563. iosb->status = STATUS_PENDING;
  564. iosb->result = 0;
  565. iosb->in_size = in_size;
  566. iosb->in_data = NULL;
  567. iosb->out_size = out_size;
  568. iosb->out_data = NULL;
  569. if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
  570. {
  571. release_object( iosb );
  572. iosb = NULL;
  573. }
  574. return iosb;
  575. }
  576. /* create an async associated with iosb for async-based requests
  577. * returned async must be passed to async_handoff */
  578. struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
  579. {
  580. struct async *async;
  581. struct iosb *iosb;
  582. if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
  583. return NULL;
  584. async = create_async( fd, current, data, iosb );
  585. release_object( iosb );
  586. if (async)
  587. {
  588. if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
  589. {
  590. release_object( async );
  591. return NULL;
  592. }
  593. async->pending = 0;
  594. async->direct_result = 1;
  595. async->comp_flags = comp_flags;
  596. }
  597. return async;
  598. }
  599. struct iosb *async_get_iosb( struct async *async )
  600. {
  601. return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
  602. }
  603. struct thread *async_get_thread( struct async *async )
  604. {
  605. return async->thread;
  606. }
  607. /* find the first pending async in queue */
  608. struct async *find_pending_async( struct async_queue *queue )
  609. {
  610. struct async *async;
  611. LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
  612. if (!async->terminated) return (struct async *)grab_object( async );
  613. return NULL;
  614. }
  615. /* cancels all async I/O */
  616. DECL_HANDLER(cancel_async)
  617. {
  618. struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
  619. struct thread *thread = req->only_thread ? current : NULL;
  620. if (obj)
  621. {
  622. int count = cancel_async( current->process, obj, thread, req->iosb );
  623. if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
  624. release_object( obj );
  625. }
  626. }
  627. /* get async result from associated iosb */
  628. DECL_HANDLER(get_async_result)
  629. {
  630. struct iosb *iosb = NULL;
  631. struct async *async;
  632. LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
  633. if (async->data.user == req->user_arg)
  634. {
  635. iosb = async->iosb;
  636. break;
  637. }
  638. if (!iosb)
  639. {
  640. set_error( STATUS_INVALID_PARAMETER );
  641. return;
  642. }
  643. if (iosb->out_data)
  644. {
  645. data_size_t size = min( iosb->out_size, get_reply_max_size() );
  646. if (size)
  647. {
  648. set_reply_data_ptr( iosb->out_data, size );
  649. iosb->out_data = NULL;
  650. }
  651. }
  652. set_error( iosb->status );
  653. }
  654. /* notify direct completion of async and close the wait handle if not blocking */
  655. DECL_HANDLER(set_async_direct_result)
  656. {
  657. struct async *async = (struct async *)get_handle_obj( current->process, req->handle, 0, &async_ops );
  658. unsigned int status = req->status;
  659. if (!async) return;
  660. if (!async->unknown_status || !async->terminated || !async->alerted)
  661. {
  662. set_error( STATUS_INVALID_PARAMETER );
  663. release_object( &async->obj );
  664. return;
  665. }
  666. if (status == STATUS_PENDING)
  667. {
  668. async->direct_result = 0;
  669. async->pending = 1;
  670. }
  671. else if (req->mark_pending)
  672. {
  673. async->pending = 1;
  674. }
  675. /* if the I/O has completed successfully (or unsuccessfully, and
  676. * async->pending is set), the client would have already set the IOSB.
  677. * therefore, we can do async_set_result() directly and let the client skip
  678. * waiting on wait_handle.
  679. */
  680. async_set_result( &async->obj, status, req->information );
  681. /* close wait handle here to avoid extra server round trip, if the I/O
  682. * either has completed, or is pending and not blocking.
  683. */
  684. if (status != STATUS_PENDING || !async->blocking)
  685. {
  686. close_handle( async->thread->process, async->wait_handle );
  687. async->wait_handle = 0;
  688. }
  689. /* report back to the client whether the wait handle has been closed.
  690. * handle will be 0 if closed by us; otherwise the original value is
  691. * retained
  692. */
  693. reply->handle = async->wait_handle;
  694. release_object( &async->obj );
  695. }