include: Added missing Header_* defines.
[wine.git] / server / completion.c
blobbe5ffe585c33aea65d97b02020c1da1a12eee377
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 "wine/unicode.h"
41 #include "object.h"
42 #include "file.h"
43 #include "handle.h"
44 #include "request.h"
47 struct completion
49 struct object obj;
50 struct list queue;
51 unsigned int depth;
54 static void completion_dump( struct object*, int );
55 static struct object_type *completion_get_type( struct object *obj );
56 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
57 static unsigned int completion_map_access( struct object *obj, unsigned int access );
58 static void completion_destroy( struct object * );
60 static const struct object_ops completion_ops =
62 sizeof(struct completion), /* size */
63 completion_dump, /* dump */
64 completion_get_type, /* get_type */
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 completion_map_access, /* map_access */
72 default_get_sd, /* get_sd */
73 default_set_sd, /* set_sd */
74 no_lookup_name, /* lookup_name */
75 directory_link_name, /* link_name */
76 default_unlink_name, /* unlink_name */
77 no_open_file, /* open_file */
78 no_kernel_obj_list, /* get_kernel_obj_list */
79 no_close_handle, /* close_handle */
80 completion_destroy /* destroy */
83 struct comp_msg
85 struct list queue_entry;
86 apc_param_t ckey;
87 apc_param_t cvalue;
88 apc_param_t information;
89 unsigned int status;
92 static void completion_destroy( struct object *obj)
94 struct completion *completion = (struct completion *) obj;
95 struct comp_msg *tmp, *next;
97 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
99 free( tmp );
103 static void completion_dump( struct object *obj, int verbose )
105 struct completion *completion = (struct completion *) obj;
107 assert( obj->ops == &completion_ops );
108 fprintf( stderr, "Completion depth=%u\n", completion->depth );
111 static struct object_type *completion_get_type( struct object *obj )
113 static const WCHAR name[] = {'I','o','C','o','m','p','l','e','t','i','o','n'};
114 static const struct unicode_str str = { name, sizeof(name) };
115 return get_object_type( &str );
118 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
120 struct completion *completion = (struct completion *)obj;
122 return !list_empty( &completion->queue );
125 static unsigned int completion_map_access( struct object *obj, unsigned int access )
127 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | IO_COMPLETION_QUERY_STATE;
128 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
129 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
130 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | IO_COMPLETION_ALL_ACCESS;
131 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
134 static struct completion *create_completion( struct object *root, const struct unicode_str *name,
135 unsigned int attr, unsigned int concurrent,
136 const struct security_descriptor *sd )
138 struct completion *completion;
140 if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
142 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
144 list_init( &completion->queue );
145 completion->depth = 0;
149 return completion;
152 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
154 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
157 void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
158 unsigned int status, apc_param_t information )
160 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
162 if (!msg)
163 return;
165 msg->ckey = ckey;
166 msg->cvalue = cvalue;
167 msg->status = status;
168 msg->information = information;
170 list_add_tail( &completion->queue, &msg->queue_entry );
171 completion->depth++;
172 wake_up( &completion->obj, 1 );
175 /* create a completion */
176 DECL_HANDLER(create_completion)
178 struct completion *completion;
179 struct unicode_str name;
180 struct object *root;
181 const struct security_descriptor *sd;
182 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
184 if (!objattr) return;
186 if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
188 reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
189 release_object( completion );
192 if (root) release_object( root );
195 /* open a completion */
196 DECL_HANDLER(open_completion)
198 struct unicode_str name = get_req_unicode_str();
200 reply->handle = open_object( current->process, req->rootdir, req->access,
201 &completion_ops, &name, req->attributes );
205 /* add completion to completion port */
206 DECL_HANDLER(add_completion)
208 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
210 if (!completion) return;
212 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
214 release_object( completion );
217 /* get completion from completion port */
218 DECL_HANDLER(remove_completion)
220 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
221 struct list *entry;
222 struct comp_msg *msg;
224 if (!completion) return;
226 entry = list_head( &completion->queue );
227 if (!entry)
228 set_error( STATUS_PENDING );
229 else
231 list_remove( entry );
232 completion->depth--;
233 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
234 reply->ckey = msg->ckey;
235 reply->cvalue = msg->cvalue;
236 reply->status = msg->status;
237 reply->information = msg->information;
238 free( msg );
241 release_object( completion );
244 /* get queue depth for completion port */
245 DECL_HANDLER(query_completion)
247 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
249 if (!completion) return;
251 reply->depth = completion->depth;
253 release_object( completion );