async.c 19 KB

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