server: Implement server-side completion queues and operations on them.
[wine/wine-gecko.git] / server / completion.c
blobf024b44a076f6c5a5e4dec83278d4cc793fb9466
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 <stdio.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "wine/unicode.h"
40 #include "object.h"
41 #include "handle.h"
42 #include "request.h"
45 struct completion
47 struct object obj;
48 struct list queue;
49 unsigned int depth;
52 static void completion_dump( struct object*, int );
53 static void completion_destroy( struct object * );
54 static int completion_signaled( struct object *obj, struct thread *thread );
56 static const struct object_ops completion_ops =
58 sizeof(struct completion), /* size */
59 completion_dump, /* dump */
60 add_queue, /* add_queue */
61 remove_queue, /* remove_queue */
62 completion_signaled, /* signaled */
63 no_satisfied, /* satisfied */
64 no_signal, /* signal */
65 no_get_fd, /* get_fd */
66 no_map_access, /* map_access */
67 no_lookup_name, /* lookup_name */
68 no_open_file, /* open_file */
69 no_close_handle, /* close_handle */
70 completion_destroy /* destroy */
73 struct comp_msg
75 struct list queue_entry;
76 unsigned long ckey;
77 unsigned long cvalue;
78 unsigned long information;
79 unsigned int status;
82 static void completion_destroy( struct object *obj)
84 struct completion *completion = (struct completion *) obj;
85 struct comp_msg *tmp, *next;
87 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
89 free( tmp );
93 static void completion_dump( struct object *obj, int verbose )
95 struct completion *completion = (struct completion *) obj;
97 assert( obj->ops == &completion_ops );
98 fprintf( stderr, "Completion " );
99 dump_object_name( &completion->obj );
100 fprintf( stderr, " (%u packets pending)\n", completion->depth );
103 static int completion_signaled( struct object *obj, struct thread *thread )
105 struct completion *completion = (struct completion *)obj;
107 return !list_empty( &completion->queue );
110 static struct completion *create_completion( struct directory *root, const struct unicode_str *name, unsigned int attr, unsigned int concurrent )
112 struct completion *completion;
114 if ((completion = create_named_object_dir( root, name, attr, &completion_ops )))
116 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
118 list_init( &completion->queue );
119 completion->depth = 0;
123 return completion;
126 static struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
128 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
131 static void add_completion( struct completion *completion, unsigned long ckey, unsigned long cvalue, unsigned int status, unsigned long information )
133 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
135 if (!msg)
136 return;
138 msg->ckey = ckey;
139 msg->cvalue = cvalue;
140 msg->status = status;
141 msg->information = information;
143 list_add_tail( &completion->queue, &msg->queue_entry );
144 completion->depth++;
145 wake_up( &completion->obj, 1 );
148 /* create a completion */
149 DECL_HANDLER(create_completion)
151 struct completion *completion;
152 struct unicode_str name;
153 struct directory *root = NULL;
155 reply->handle = 0;
157 get_req_unicode_str( &name );
158 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
159 return;
161 if ( (completion = create_completion( root, &name, req->attributes, req->concurrent )) != NULL )
163 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
164 release_object( completion );
167 if (root) release_object( root );
170 /* open a completion */
171 DECL_HANDLER(open_completion)
173 struct completion *completion;
174 struct unicode_str name;
175 struct directory *root = NULL;
177 reply->handle = 0;
179 get_req_unicode_str( &name );
180 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
181 return;
183 if ( (completion = open_object_dir( root, &name, req->attributes, &completion_ops )) != NULL )
185 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
186 release_object( completion );
189 if (root) release_object( root );
193 /* add completion to completion port */
194 DECL_HANDLER(add_completion)
196 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
198 if (!completion) return;
200 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
202 release_object( completion );
205 /* get completion from completion port */
206 DECL_HANDLER(remove_completion)
208 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
209 struct list *entry;
210 struct comp_msg *msg;
212 if (!completion) return;
214 entry = list_head( &completion->queue );
215 if (!entry)
216 set_error( STATUS_PENDING );
217 else
219 list_remove( entry );
220 completion->depth--;
221 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
222 reply->ckey = msg->ckey;
223 reply->cvalue = msg->cvalue;
224 reply->status = msg->status;
225 reply->information = msg->information;
226 free( msg );
229 release_object( completion );
232 /* get queue depth for completion port */
233 DECL_HANDLER(query_completion)
235 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
237 if (!completion) return;
239 reply->depth = completion->depth;
241 release_object( completion );