ntdll/tests: Account for some timer resolution rounding.
[wine.git] / server / completion.c
blobeb0d256ad0957a2d66e09f728b50ed59c7f0f0a2
1 /*
2 * Server-side IO completion ports implementation
4 * Copyright (C) 2007 Andrey Turkin
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.
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.
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
22 /* FIXMEs:
23 * - built-in wait queues used which means:
24 * + threads are awaken FIFO and not LIFO as native does
25 * + "max concurrent active threads" parameter not used
26 * + completion handle is waitable, while native isn't
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
33 #include <stdio.h>
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winternl.h"
40 #include "object.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "request.h"
46 static const WCHAR completion_name[] = {'I','o','C','o','m','p','l','e','t','i','o','n'};
48 struct type_descr completion_type =
50 { completion_name, sizeof(completion_name) }, /* name */
51 IO_COMPLETION_ALL_ACCESS, /* valid_access */
52 { /* mapping */
53 STANDARD_RIGHTS_READ | IO_COMPLETION_QUERY_STATE,
54 STANDARD_RIGHTS_WRITE | IO_COMPLETION_MODIFY_STATE,
55 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
56 IO_COMPLETION_ALL_ACCESS
60 struct completion
62 struct object obj;
63 struct list queue;
64 unsigned int depth;
67 static void completion_dump( struct object*, int );
68 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
69 static void completion_destroy( struct object * );
71 static const struct object_ops completion_ops =
73 sizeof(struct completion), /* size */
74 &completion_type, /* type */
75 completion_dump, /* dump */
76 add_queue, /* add_queue */
77 remove_queue, /* remove_queue */
78 completion_signaled, /* signaled */
79 no_satisfied, /* satisfied */
80 no_signal, /* signal */
81 no_get_fd, /* get_fd */
82 default_map_access, /* map_access */
83 default_get_sd, /* get_sd */
84 default_set_sd, /* set_sd */
85 default_get_full_name, /* get_full_name */
86 no_lookup_name, /* lookup_name */
87 directory_link_name, /* link_name */
88 default_unlink_name, /* unlink_name */
89 no_open_file, /* open_file */
90 no_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 completion_destroy /* destroy */
95 struct comp_msg
97 struct list queue_entry;
98 apc_param_t ckey;
99 apc_param_t cvalue;
100 apc_param_t information;
101 unsigned int status;
104 static void completion_destroy( struct object *obj)
106 struct completion *completion = (struct completion *) obj;
107 struct comp_msg *tmp, *next;
109 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
111 free( tmp );
115 static void completion_dump( struct object *obj, int verbose )
117 struct completion *completion = (struct completion *) obj;
119 assert( obj->ops == &completion_ops );
120 fprintf( stderr, "Completion depth=%u\n", completion->depth );
123 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
125 struct completion *completion = (struct completion *)obj;
127 return !list_empty( &completion->queue );
130 static struct completion *create_completion( struct object *root, const struct unicode_str *name,
131 unsigned int attr, unsigned int concurrent,
132 const struct security_descriptor *sd )
134 struct completion *completion;
136 if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
138 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
140 list_init( &completion->queue );
141 completion->depth = 0;
145 return completion;
148 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
150 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
153 void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
154 unsigned int status, apc_param_t information )
156 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
158 if (!msg)
159 return;
161 msg->ckey = ckey;
162 msg->cvalue = cvalue;
163 msg->status = status;
164 msg->information = information;
166 list_add_tail( &completion->queue, &msg->queue_entry );
167 completion->depth++;
168 wake_up( &completion->obj, 1 );
171 /* create a completion */
172 DECL_HANDLER(create_completion)
174 struct completion *completion;
175 struct unicode_str name;
176 struct object *root;
177 const struct security_descriptor *sd;
178 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
180 if (!objattr) return;
182 if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
184 reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
185 release_object( completion );
188 if (root) release_object( root );
191 /* open a completion */
192 DECL_HANDLER(open_completion)
194 struct unicode_str name = get_req_unicode_str();
196 reply->handle = open_object( current->process, req->rootdir, req->access,
197 &completion_ops, &name, req->attributes );
201 /* add completion to completion port */
202 DECL_HANDLER(add_completion)
204 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
206 if (!completion) return;
208 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
210 release_object( completion );
213 /* get completion from completion port */
214 DECL_HANDLER(remove_completion)
216 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
217 struct list *entry;
218 struct comp_msg *msg;
220 if (!completion) return;
222 entry = list_head( &completion->queue );
223 if (!entry)
224 set_error( STATUS_PENDING );
225 else
227 list_remove( entry );
228 completion->depth--;
229 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
230 reply->ckey = msg->ckey;
231 reply->cvalue = msg->cvalue;
232 reply->status = msg->status;
233 reply->information = msg->information;
234 free( msg );
237 release_object( completion );
240 /* get queue depth for completion port */
241 DECL_HANDLER(query_completion)
243 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
245 if (!completion) return;
247 reply->depth = completion->depth;
249 release_object( completion );