completion.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Server-side IO completion ports implementation
  3. *
  4. * Copyright (C) 2007 Andrey Turkin
  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. */
  21. /* FIXMEs:
  22. * - built-in wait queues used which means:
  23. * + threads are awaken FIFO and not LIFO as native does
  24. * + "max concurrent active threads" parameter not used
  25. * + completion handle is waitable, while native isn't
  26. */
  27. #include "config.h"
  28. #include "wine/port.h"
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include "ntstatus.h"
  32. #define WIN32_NO_STATUS
  33. #include "windef.h"
  34. #include "winternl.h"
  35. #include "object.h"
  36. #include "file.h"
  37. #include "handle.h"
  38. #include "request.h"
  39. static const WCHAR completion_name[] = {'I','o','C','o','m','p','l','e','t','i','o','n'};
  40. struct type_descr completion_type =
  41. {
  42. { completion_name, sizeof(completion_name) }, /* name */
  43. IO_COMPLETION_ALL_ACCESS, /* valid_access */
  44. { /* mapping */
  45. STANDARD_RIGHTS_READ | IO_COMPLETION_QUERY_STATE,
  46. STANDARD_RIGHTS_WRITE | IO_COMPLETION_MODIFY_STATE,
  47. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  48. IO_COMPLETION_ALL_ACCESS
  49. },
  50. };
  51. struct completion
  52. {
  53. struct object obj;
  54. struct list queue;
  55. unsigned int depth;
  56. };
  57. static void completion_dump( struct object*, int );
  58. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
  59. static void completion_destroy( struct object * );
  60. static const struct object_ops completion_ops =
  61. {
  62. sizeof(struct completion), /* size */
  63. &completion_type, /* type */
  64. completion_dump, /* dump */
  65. add_queue, /* add_queue */
  66. remove_queue, /* remove_queue */
  67. completion_signaled, /* signaled */
  68. no_satisfied, /* satisfied */
  69. no_signal, /* signal */
  70. no_get_fd, /* get_fd */
  71. default_map_access, /* map_access */
  72. default_get_sd, /* get_sd */
  73. default_set_sd, /* set_sd */
  74. default_get_full_name, /* get_full_name */
  75. no_lookup_name, /* lookup_name */
  76. directory_link_name, /* link_name */
  77. default_unlink_name, /* unlink_name */
  78. no_open_file, /* open_file */
  79. no_kernel_obj_list, /* get_kernel_obj_list */
  80. no_close_handle, /* close_handle */
  81. completion_destroy /* destroy */
  82. };
  83. struct comp_msg
  84. {
  85. struct list queue_entry;
  86. apc_param_t ckey;
  87. apc_param_t cvalue;
  88. apc_param_t information;
  89. unsigned int status;
  90. };
  91. static void completion_destroy( struct object *obj)
  92. {
  93. struct completion *completion = (struct completion *) obj;
  94. struct comp_msg *tmp, *next;
  95. LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
  96. {
  97. free( tmp );
  98. }
  99. }
  100. static void completion_dump( struct object *obj, int verbose )
  101. {
  102. struct completion *completion = (struct completion *) obj;
  103. assert( obj->ops == &completion_ops );
  104. fprintf( stderr, "Completion depth=%u\n", completion->depth );
  105. }
  106. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
  107. {
  108. struct completion *completion = (struct completion *)obj;
  109. return !list_empty( &completion->queue );
  110. }
  111. static struct completion *create_completion( struct object *root, const struct unicode_str *name,
  112. unsigned int attr, unsigned int concurrent,
  113. const struct security_descriptor *sd )
  114. {
  115. struct completion *completion;
  116. if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
  117. {
  118. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  119. {
  120. list_init( &completion->queue );
  121. completion->depth = 0;
  122. }
  123. }
  124. return completion;
  125. }
  126. struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
  127. {
  128. return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
  129. }
  130. void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
  131. unsigned int status, apc_param_t information )
  132. {
  133. struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
  134. if (!msg)
  135. return;
  136. msg->ckey = ckey;
  137. msg->cvalue = cvalue;
  138. msg->status = status;
  139. msg->information = information;
  140. list_add_tail( &completion->queue, &msg->queue_entry );
  141. completion->depth++;
  142. wake_up( &completion->obj, 1 );
  143. }
  144. /* create a completion */
  145. DECL_HANDLER(create_completion)
  146. {
  147. struct completion *completion;
  148. struct unicode_str name;
  149. struct object *root;
  150. const struct security_descriptor *sd;
  151. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  152. if (!objattr) return;
  153. if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
  154. {
  155. reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
  156. release_object( completion );
  157. }
  158. if (root) release_object( root );
  159. }
  160. /* open a completion */
  161. DECL_HANDLER(open_completion)
  162. {
  163. struct unicode_str name = get_req_unicode_str();
  164. reply->handle = open_object( current->process, req->rootdir, req->access,
  165. &completion_ops, &name, req->attributes );
  166. }
  167. /* add completion to completion port */
  168. DECL_HANDLER(add_completion)
  169. {
  170. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  171. if (!completion) return;
  172. add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
  173. release_object( completion );
  174. }
  175. /* get completion from completion port */
  176. DECL_HANDLER(remove_completion)
  177. {
  178. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  179. struct list *entry;
  180. struct comp_msg *msg;
  181. if (!completion) return;
  182. entry = list_head( &completion->queue );
  183. if (!entry)
  184. set_error( STATUS_PENDING );
  185. else
  186. {
  187. list_remove( entry );
  188. completion->depth--;
  189. msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
  190. reply->ckey = msg->ckey;
  191. reply->cvalue = msg->cvalue;
  192. reply->status = msg->status;
  193. reply->information = msg->information;
  194. free( msg );
  195. }
  196. release_object( completion );
  197. }
  198. /* get queue depth for completion port */
  199. DECL_HANDLER(query_completion)
  200. {
  201. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
  202. if (!completion) return;
  203. reply->depth = completion->depth;
  204. release_object( completion );
  205. }