user.exe: Make sure that the thunk selector doesn't get freed.
[wine.git] / server / completion.c
blob6933195e72dc483f9c05ca38618143b46c5b4da4
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"
31 #include <stdarg.h>
32 #include <stdio.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "object.h"
40 #include "file.h"
41 #include "handle.h"
42 #include "request.h"
45 static const WCHAR completion_name[] = {'I','o','C','o','m','p','l','e','t','i','o','n'};
47 struct type_descr completion_type =
49 { completion_name, sizeof(completion_name) }, /* name */
50 IO_COMPLETION_ALL_ACCESS, /* valid_access */
51 { /* mapping */
52 STANDARD_RIGHTS_READ | IO_COMPLETION_QUERY_STATE,
53 STANDARD_RIGHTS_WRITE | IO_COMPLETION_MODIFY_STATE,
54 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
55 IO_COMPLETION_ALL_ACCESS
59 struct completion
61 struct object obj;
62 struct list queue;
63 unsigned int depth;
66 static void completion_dump( struct object*, int );
67 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
68 static void completion_destroy( struct object * );
70 static const struct object_ops completion_ops =
72 sizeof(struct completion), /* size */
73 &completion_type, /* type */
74 completion_dump, /* dump */
75 add_queue, /* add_queue */
76 remove_queue, /* remove_queue */
77 completion_signaled, /* signaled */
78 no_satisfied, /* satisfied */
79 no_signal, /* signal */
80 no_get_fd, /* get_fd */
81 default_map_access, /* map_access */
82 default_get_sd, /* get_sd */
83 default_set_sd, /* set_sd */
84 default_get_full_name, /* get_full_name */
85 no_lookup_name, /* lookup_name */
86 directory_link_name, /* link_name */
87 default_unlink_name, /* unlink_name */
88 no_open_file, /* open_file */
89 no_kernel_obj_list, /* get_kernel_obj_list */
90 no_close_handle, /* close_handle */
91 completion_destroy /* destroy */
94 struct comp_msg
96 struct list queue_entry;
97 apc_param_t ckey;
98 apc_param_t cvalue;
99 apc_param_t information;
100 unsigned int status;
103 static void completion_destroy( struct object *obj)
105 struct completion *completion = (struct completion *) obj;
106 struct comp_msg *tmp, *next;
108 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
110 free( tmp );
114 static void completion_dump( struct object *obj, int verbose )
116 struct completion *completion = (struct completion *) obj;
118 assert( obj->ops == &completion_ops );
119 fprintf( stderr, "Completion depth=%u\n", completion->depth );
122 static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
124 struct completion *completion = (struct completion *)obj;
126 return !list_empty( &completion->queue );
129 static struct completion *create_completion( struct object *root, const struct unicode_str *name,
130 unsigned int attr, unsigned int concurrent,
131 const struct security_descriptor *sd )
133 struct completion *completion;
135 if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
137 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
139 list_init( &completion->queue );
140 completion->depth = 0;
144 return completion;
147 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
149 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
152 void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
153 unsigned int status, apc_param_t information )
155 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
157 if (!msg)
158 return;
160 msg->ckey = ckey;
161 msg->cvalue = cvalue;
162 msg->status = status;
163 msg->information = information;
165 list_add_tail( &completion->queue, &msg->queue_entry );
166 completion->depth++;
167 wake_up( &completion->obj, 1 );
170 /* create a completion */
171 DECL_HANDLER(create_completion)
173 struct completion *completion;
174 struct unicode_str name;
175 struct object *root;
176 const struct security_descriptor *sd;
177 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
179 if (!objattr) return;
181 if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
183 reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
184 release_object( completion );
187 if (root) release_object( root );
190 /* open a completion */
191 DECL_HANDLER(open_completion)
193 struct unicode_str name = get_req_unicode_str();
195 reply->handle = open_object( current->process, req->rootdir, req->access,
196 &completion_ops, &name, req->attributes );
200 /* add completion to completion port */
201 DECL_HANDLER(add_completion)
203 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
205 if (!completion) return;
207 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
209 release_object( completion );
212 /* get completion from completion port */
213 DECL_HANDLER(remove_completion)
215 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
216 struct list *entry;
217 struct comp_msg *msg;
219 if (!completion) return;
221 entry = list_head( &completion->queue );
222 if (!entry)
223 set_error( STATUS_PENDING );
224 else
226 list_remove( entry );
227 completion->depth--;
228 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
229 reply->ckey = msg->ckey;
230 reply->cvalue = msg->cvalue;
231 reply->status = msg->status;
232 reply->information = msg->information;
233 free( msg );
236 release_object( completion );
239 /* get queue depth for completion port */
240 DECL_HANDLER(query_completion)
242 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
244 if (!completion) return;
246 reply->depth = completion->depth;
248 release_object( completion );