async.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. 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. struct iosb *iosb; /* I/O status block */
  47. obj_handle_t wait_handle; /* pre-allocated wait handle */
  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. struct completion *completion; /* completion associated with fd */
  52. apc_param_t comp_key; /* completion key associated with fd */
  53. unsigned int comp_flags; /* completion flags */
  54. async_completion_callback completion_callback; /* callback to be called on completion */
  55. void *completion_callback_private; /* argument to completion_callback */
  56. };
  57. static void async_dump( struct object *obj, int verbose );
  58. static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
  59. static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
  60. static void async_destroy( struct object *obj );
  61. static const struct object_ops async_ops =
  62. {
  63. sizeof(struct async), /* size */
  64. &no_type, /* type */
  65. async_dump, /* dump */
  66. add_queue, /* add_queue */
  67. remove_queue, /* remove_queue */
  68. async_signaled, /* signaled */
  69. async_satisfied, /* satisfied */
  70. no_signal, /* signal */
  71. no_get_fd, /* get_fd */
  72. default_map_access, /* map_access */
  73. default_get_sd, /* get_sd */
  74. default_set_sd, /* set_sd */
  75. no_get_full_name, /* get_full_name */
  76. no_lookup_name, /* lookup_name */
  77. no_link_name, /* link_name */
  78. NULL, /* unlink_name */
  79. no_open_file, /* open_file */
  80. no_kernel_obj_list, /* get_kernel_obj_list */
  81. no_close_handle, /* close_handle */
  82. async_destroy /* destroy */
  83. };
  84. static inline void async_reselect( struct async *async )
  85. {
  86. if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
  87. }
  88. static void async_dump( struct object *obj, int verbose )
  89. {
  90. struct async *async = (struct async *)obj;
  91. assert( obj->ops == &async_ops );
  92. fprintf( stderr, "Async thread=%p\n", async->thread );
  93. }
  94. static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
  95. {
  96. struct async *async = (struct async *)obj;
  97. assert( obj->ops == &async_ops );
  98. return async->signaled;
  99. }
  100. static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
  101. {
  102. struct async *async = (struct async *)obj;
  103. assert( obj->ops == &async_ops );
  104. if (async->direct_result)
  105. {
  106. async_set_result( &async->obj, async->iosb->status, async->iosb->result );
  107. async->direct_result = 0;
  108. }
  109. /* close wait handle here to avoid extra server round trip */
  110. if (async->wait_handle)
  111. {
  112. close_handle( async->thread->process, async->wait_handle );
  113. async->wait_handle = 0;
  114. }
  115. if (async->status == STATUS_PENDING) make_wait_abandoned( entry );
  116. }
  117. static void async_destroy( struct object *obj )
  118. {
  119. struct async *async = (struct async *)obj;
  120. assert( obj->ops == &async_ops );
  121. list_remove( &async->process_entry );
  122. if (async->queue)
  123. {
  124. list_remove( &async->queue_entry );
  125. async_reselect( async );
  126. }
  127. else if (async->fd) release_object( async->fd );
  128. if (async->timeout) remove_timeout_user( async->timeout );
  129. if (async->completion) release_object( async->completion );
  130. if (async->event) release_object( async->event );
  131. if (async->iosb) release_object( async->iosb );
  132. release_object( async->thread );
  133. }
  134. /* notifies client thread of new status of its async request */
  135. void async_terminate( struct async *async, unsigned int status )
  136. {
  137. assert( status != STATUS_PENDING );
  138. if (async->status != STATUS_PENDING)
  139. {
  140. /* already terminated, just update status */
  141. async->status = status;
  142. return;
  143. }
  144. async->status = status;
  145. if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
  146. if (!async->direct_result)
  147. {
  148. if (async->data.user)
  149. {
  150. apc_call_t data;
  151. memset( &data, 0, sizeof(data) );
  152. data.type = APC_ASYNC_IO;
  153. data.async_io.user = async->data.user;
  154. data.async_io.sb = async->data.iosb;
  155. data.async_io.status = status;
  156. thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
  157. }
  158. else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
  159. }
  160. async_reselect( async );
  161. if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
  162. }
  163. /* callback for timeout on an async request */
  164. static void async_timeout( void *private )
  165. {
  166. struct async *async = private;
  167. async->timeout = NULL;
  168. async_terminate( async, async->timeout_status );
  169. }
  170. /* free an async queue, cancelling all async operations */
  171. void free_async_queue( struct async_queue *queue )
  172. {
  173. struct async *async, *next;
  174. LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
  175. {
  176. grab_object( &async->obj );
  177. if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  178. async->fd = NULL;
  179. async_terminate( async, STATUS_HANDLES_CLOSED );
  180. async->queue = NULL;
  181. release_object( &async->obj );
  182. }
  183. }
  184. void queue_async( struct async_queue *queue, struct async *async )
  185. {
  186. /* fd will be set to NULL in free_async_queue when fd is destroyed */
  187. release_object( async->fd );
  188. async->queue = queue;
  189. grab_object( async );
  190. list_add_tail( &queue->queue, &async->queue_entry );
  191. set_fd_signaled( async->fd, 0 );
  192. }
  193. /* create an async on a given queue of a fd */
  194. struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
  195. {
  196. struct event *event = NULL;
  197. struct async *async;
  198. if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
  199. return NULL;
  200. if (!(async = alloc_object( &async_ops )))
  201. {
  202. if (event) release_object( event );
  203. return NULL;
  204. }
  205. async->thread = (struct thread *)grab_object( thread );
  206. async->event = event;
  207. async->status = STATUS_PENDING;
  208. async->data = *data;
  209. async->timeout = NULL;
  210. async->queue = NULL;
  211. async->fd = (struct fd *)grab_object( fd );
  212. async->signaled = 0;
  213. async->pending = 1;
  214. async->wait_handle = 0;
  215. async->direct_result = 0;
  216. async->completion = fd_get_completion( fd, &async->comp_key );
  217. async->comp_flags = 0;
  218. async->completion_callback = NULL;
  219. async->completion_callback_private = NULL;
  220. if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
  221. else async->iosb = NULL;
  222. list_add_head( &thread->process->asyncs, &async->process_entry );
  223. if (event) reset_event( event );
  224. if (async->completion && data->apc)
  225. {
  226. release_object( async );
  227. set_error( STATUS_INVALID_PARAMETER );
  228. return NULL;
  229. }
  230. return async;
  231. }
  232. void set_async_pending( struct async *async, int signal )
  233. {
  234. if (async->status == STATUS_PENDING)
  235. {
  236. async->pending = 1;
  237. if (signal && !async->signaled)
  238. {
  239. async->signaled = 1;
  240. wake_up( &async->obj, 0 );
  241. }
  242. }
  243. }
  244. /* create an async associated with iosb for async-based requests
  245. * returned async must be passed to async_handoff */
  246. struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
  247. {
  248. struct async *async;
  249. struct iosb *iosb;
  250. if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
  251. return NULL;
  252. async = create_async( fd, current, data, iosb );
  253. release_object( iosb );
  254. if (async)
  255. {
  256. if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
  257. {
  258. release_object( async );
  259. return NULL;
  260. }
  261. async->pending = 0;
  262. async->direct_result = 1;
  263. async->comp_flags = comp_flags;
  264. }
  265. return async;
  266. }
  267. /* return async object status and wait handle to client */
  268. obj_handle_t async_handoff( struct async *async, int success, data_size_t *result, int force_blocking )
  269. {
  270. if (!success)
  271. {
  272. if (get_error() == STATUS_PENDING)
  273. {
  274. /* we don't know the result yet, so client needs to wait */
  275. async->direct_result = 0;
  276. return async->wait_handle;
  277. }
  278. close_handle( async->thread->process, async->wait_handle );
  279. async->wait_handle = 0;
  280. return 0;
  281. }
  282. if (get_error() != STATUS_PENDING)
  283. {
  284. /* status and data are already set and returned */
  285. async_terminate( async, get_error() );
  286. }
  287. else if (async->iosb->status != STATUS_PENDING)
  288. {
  289. /* result is already available in iosb, return it */
  290. if (async->iosb->out_data)
  291. {
  292. set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
  293. async->iosb->out_data = NULL;
  294. }
  295. }
  296. if (async->iosb->status != STATUS_PENDING)
  297. {
  298. if (result) *result = async->iosb->result;
  299. async->signaled = 1;
  300. }
  301. else
  302. {
  303. async->direct_result = 0;
  304. async->pending = 1;
  305. if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
  306. {
  307. close_handle( async->thread->process, async->wait_handle);
  308. async->wait_handle = 0;
  309. }
  310. }
  311. set_error( async->iosb->status );
  312. return async->wait_handle;
  313. }
  314. /* set the timeout of an async operation */
  315. void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
  316. {
  317. if (async->timeout) remove_timeout_user( async->timeout );
  318. if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
  319. else async->timeout = NULL;
  320. async->timeout_status = status;
  321. }
  322. /* set a callback to be notified when the async is completed */
  323. void async_set_completion_callback( struct async *async, async_completion_callback func, void *private )
  324. {
  325. async->completion_callback = func;
  326. async->completion_callback_private = private;
  327. }
  328. static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
  329. apc_param_t information )
  330. {
  331. if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  332. if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
  333. }
  334. /* store the result of the client-side async callback */
  335. void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
  336. {
  337. struct async *async = (struct async *)obj;
  338. if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
  339. assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
  340. if (status == STATUS_PENDING) /* restart it */
  341. {
  342. status = async->status;
  343. async->status = STATUS_PENDING;
  344. grab_object( async );
  345. if (status != STATUS_ALERTED) /* it was terminated in the meantime */
  346. async_terminate( async, status );
  347. else
  348. async_reselect( async );
  349. }
  350. else
  351. {
  352. if (async->timeout) remove_timeout_user( async->timeout );
  353. async->timeout = NULL;
  354. async->status = status;
  355. if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
  356. if (async->data.apc)
  357. {
  358. apc_call_t data;
  359. memset( &data, 0, sizeof(data) );
  360. data.type = APC_USER;
  361. data.user.user.func = async->data.apc;
  362. data.user.user.args[0] = async->data.apc_context;
  363. data.user.user.args[1] = async->data.iosb;
  364. data.user.user.args[2] = 0;
  365. thread_queue_apc( NULL, async->thread, NULL, &data );
  366. }
  367. else if (async->data.apc_context && (async->pending ||
  368. !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
  369. {
  370. add_async_completion( async, async->data.apc_context, status, total );
  371. }
  372. if (async->event) set_event( async->event );
  373. else if (async->fd) set_fd_signaled( async->fd, 1 );
  374. if (!async->signaled)
  375. {
  376. async->signaled = 1;
  377. wake_up( &async->obj, 0 );
  378. }
  379. if (async->completion_callback)
  380. async->completion_callback( async->completion_callback_private );
  381. async->completion_callback = NULL;
  382. }
  383. }
  384. /* check if an async operation is waiting to be alerted */
  385. int async_waiting( struct async_queue *queue )
  386. {
  387. struct list *ptr;
  388. struct async *async;
  389. if (!(ptr = list_head( &queue->queue ))) return 0;
  390. async = LIST_ENTRY( ptr, struct async, queue_entry );
  391. return async->status == STATUS_PENDING;
  392. }
  393. static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
  394. {
  395. struct async *async;
  396. int woken = 0;
  397. restart:
  398. LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
  399. {
  400. if (async->status == STATUS_CANCELLED) continue;
  401. if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
  402. (!thread || async->thread == thread) &&
  403. (!iosb || async->data.iosb == iosb))
  404. {
  405. async_terminate( async, STATUS_CANCELLED );
  406. woken++;
  407. goto restart;
  408. }
  409. }
  410. return woken;
  411. }
  412. void cancel_process_asyncs( struct process *process )
  413. {
  414. cancel_async( process, NULL, NULL, 0 );
  415. }
  416. /* wake up async operations on the queue */
  417. void async_wake_up( struct async_queue *queue, unsigned int status )
  418. {
  419. struct list *ptr, *next;
  420. LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
  421. {
  422. struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
  423. async_terminate( async, status );
  424. if (status == STATUS_ALERTED) break; /* only wake up the first one */
  425. }
  426. }
  427. static void iosb_dump( struct object *obj, int verbose );
  428. static void iosb_destroy( struct object *obj );
  429. static const struct object_ops iosb_ops =
  430. {
  431. sizeof(struct iosb), /* size */
  432. &no_type, /* type */
  433. iosb_dump, /* dump */
  434. no_add_queue, /* add_queue */
  435. NULL, /* remove_queue */
  436. NULL, /* signaled */
  437. NULL, /* satisfied */
  438. no_signal, /* signal */
  439. no_get_fd, /* get_fd */
  440. default_map_access, /* map_access */
  441. default_get_sd, /* get_sd */
  442. default_set_sd, /* set_sd */
  443. no_get_full_name, /* get_full_name */
  444. no_lookup_name, /* lookup_name */
  445. no_link_name, /* link_name */
  446. NULL, /* unlink_name */
  447. no_open_file, /* open_file */
  448. no_kernel_obj_list, /* get_kernel_obj_list */
  449. no_close_handle, /* close_handle */
  450. iosb_destroy /* destroy */
  451. };
  452. static void iosb_dump( struct object *obj, int verbose )
  453. {
  454. assert( obj->ops == &iosb_ops );
  455. fprintf( stderr, "I/O status block\n" );
  456. }
  457. static void iosb_destroy( struct object *obj )
  458. {
  459. struct iosb *iosb = (struct iosb *)obj;
  460. free( iosb->in_data );
  461. free( iosb->out_data );
  462. }
  463. /* allocate iosb struct */
  464. struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
  465. {
  466. struct iosb *iosb;
  467. if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
  468. iosb->status = STATUS_PENDING;
  469. iosb->result = 0;
  470. iosb->in_size = in_size;
  471. iosb->in_data = NULL;
  472. iosb->out_size = out_size;
  473. iosb->out_data = NULL;
  474. if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
  475. {
  476. release_object( iosb );
  477. iosb = NULL;
  478. }
  479. return iosb;
  480. }
  481. struct iosb *async_get_iosb( struct async *async )
  482. {
  483. return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
  484. }
  485. struct thread *async_get_thread( struct async *async )
  486. {
  487. return async->thread;
  488. }
  489. int async_is_blocking( struct async *async )
  490. {
  491. return !async->event && !async->data.apc && !async->data.apc_context;
  492. }
  493. /* find the first pending async in queue */
  494. struct async *find_pending_async( struct async_queue *queue )
  495. {
  496. struct async *async;
  497. LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
  498. if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
  499. return NULL;
  500. }
  501. /* cancels all async I/O */
  502. DECL_HANDLER(cancel_async)
  503. {
  504. struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
  505. struct thread *thread = req->only_thread ? current : NULL;
  506. if (obj)
  507. {
  508. int count = cancel_async( current->process, obj, thread, req->iosb );
  509. if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
  510. release_object( obj );
  511. }
  512. }
  513. /* get async result from associated iosb */
  514. DECL_HANDLER(get_async_result)
  515. {
  516. struct iosb *iosb = NULL;
  517. struct async *async;
  518. LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
  519. if (async->data.user == req->user_arg)
  520. {
  521. iosb = async->iosb;
  522. break;
  523. }
  524. if (!iosb)
  525. {
  526. set_error( STATUS_INVALID_PARAMETER );
  527. return;
  528. }
  529. if (iosb->out_data)
  530. {
  531. data_size_t size = min( iosb->out_size, get_reply_max_size() );
  532. if (size)
  533. {
  534. set_reply_data_ptr( iosb->out_data, size );
  535. iosb->out_data = NULL;
  536. }
  537. }
  538. reply->size = iosb->result;
  539. set_error( iosb->status );
  540. }