completion.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. struct completion
  40. {
  41. struct object obj;
  42. struct list queue;
  43. unsigned int depth;
  44. };
  45. static void completion_dump( struct object*, int );
  46. static struct object_type *completion_get_type( struct object *obj );
  47. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
  48. static unsigned int completion_map_access( struct object *obj, unsigned int access );
  49. static void completion_destroy( struct object * );
  50. static const struct object_ops completion_ops =
  51. {
  52. sizeof(struct completion), /* size */
  53. completion_dump, /* dump */
  54. completion_get_type, /* get_type */
  55. add_queue, /* add_queue */
  56. remove_queue, /* remove_queue */
  57. completion_signaled, /* signaled */
  58. NULL, /* get_esync_fd */
  59. no_satisfied, /* satisfied */
  60. no_signal, /* signal */
  61. no_get_fd, /* get_fd */
  62. completion_map_access, /* map_access */
  63. default_get_sd, /* get_sd */
  64. default_set_sd, /* set_sd */
  65. no_lookup_name, /* lookup_name */
  66. directory_link_name, /* link_name */
  67. default_unlink_name, /* unlink_name */
  68. no_open_file, /* open_file */
  69. no_kernel_obj_list, /* get_kernel_obj_list */
  70. no_alloc_handle, /* alloc_handle */
  71. no_close_handle, /* close_handle */
  72. completion_destroy /* destroy */
  73. };
  74. struct comp_msg
  75. {
  76. struct list queue_entry;
  77. apc_param_t ckey;
  78. apc_param_t cvalue;
  79. apc_param_t information;
  80. unsigned int status;
  81. };
  82. static void completion_destroy( struct object *obj)
  83. {
  84. struct completion *completion = (struct completion *) obj;
  85. struct comp_msg *tmp, *next;
  86. LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
  87. {
  88. free( tmp );
  89. }
  90. }
  91. static void completion_dump( struct object *obj, int verbose )
  92. {
  93. struct completion *completion = (struct completion *) obj;
  94. assert( obj->ops == &completion_ops );
  95. fprintf( stderr, "Completion depth=%u\n", completion->depth );
  96. }
  97. static struct object_type *completion_get_type( struct object *obj )
  98. {
  99. static const struct unicode_str str = { type_IoCompletion, sizeof(type_IoCompletion) };
  100. return get_object_type( &str );
  101. }
  102. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
  103. {
  104. struct completion *completion = (struct completion *)obj;
  105. return !list_empty( &completion->queue );
  106. }
  107. static unsigned int completion_map_access( struct object *obj, unsigned int access )
  108. {
  109. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | IO_COMPLETION_QUERY_STATE;
  110. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
  111. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  112. if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | IO_COMPLETION_ALL_ACCESS;
  113. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  114. }
  115. static struct completion *create_completion( struct object *root, const struct unicode_str *name,
  116. unsigned int attr, unsigned int concurrent,
  117. const struct security_descriptor *sd )
  118. {
  119. struct completion *completion;
  120. if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
  121. {
  122. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  123. {
  124. list_init( &completion->queue );
  125. completion->depth = 0;
  126. }
  127. }
  128. return completion;
  129. }
  130. struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
  131. {
  132. return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
  133. }
  134. void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
  135. unsigned int status, apc_param_t information )
  136. {
  137. struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
  138. if (!msg)
  139. return;
  140. msg->ckey = ckey;
  141. msg->cvalue = cvalue;
  142. msg->status = status;
  143. msg->information = information;
  144. list_add_tail( &completion->queue, &msg->queue_entry );
  145. completion->depth++;
  146. wake_up( &completion->obj, 1 );
  147. }
  148. /* create a completion */
  149. DECL_HANDLER(create_completion)
  150. {
  151. struct completion *completion;
  152. struct unicode_str name;
  153. struct object *root;
  154. const struct security_descriptor *sd;
  155. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  156. if (!objattr) return;
  157. if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
  158. {
  159. reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
  160. release_object( completion );
  161. }
  162. if (root) release_object( root );
  163. }
  164. /* open a completion */
  165. DECL_HANDLER(open_completion)
  166. {
  167. struct unicode_str name = get_req_unicode_str();
  168. reply->handle = open_object( current->process, req->rootdir, req->access,
  169. &completion_ops, &name, req->attributes );
  170. }
  171. /* add completion to completion port */
  172. DECL_HANDLER(add_completion)
  173. {
  174. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  175. if (!completion) return;
  176. add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
  177. release_object( completion );
  178. }
  179. /* get completion from completion port */
  180. DECL_HANDLER(remove_completion)
  181. {
  182. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  183. struct list *entry;
  184. struct comp_msg *msg;
  185. if (!completion) return;
  186. entry = list_head( &completion->queue );
  187. if (!entry)
  188. set_error( STATUS_PENDING );
  189. else
  190. {
  191. list_remove( entry );
  192. completion->depth--;
  193. msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
  194. reply->ckey = msg->ckey;
  195. reply->cvalue = msg->cvalue;
  196. reply->status = msg->status;
  197. reply->information = msg->information;
  198. free( msg );
  199. }
  200. release_object( completion );
  201. }
  202. /* get queue depth for completion port */
  203. DECL_HANDLER(query_completion)
  204. {
  205. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
  206. if (!completion) return;
  207. reply->depth = completion->depth;
  208. release_object( completion );
  209. }