push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / server / completion.c
blob13f59395cb8618b9dbbe1b650cea798b16123812
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 void completion_destroy( struct object * );
56 static int completion_signaled( struct object *obj, struct thread *thread );
58 static const struct object_ops completion_ops =
60 sizeof(struct completion), /* size */
61 completion_dump, /* dump */
62 add_queue, /* add_queue */
63 remove_queue, /* remove_queue */
64 completion_signaled, /* signaled */
65 no_satisfied, /* satisfied */
66 no_signal, /* signal */
67 no_get_fd, /* get_fd */
68 no_map_access, /* map_access */
69 no_lookup_name, /* lookup_name */
70 no_open_file, /* open_file */
71 no_close_handle, /* close_handle */
72 completion_destroy /* destroy */
75 struct comp_msg
77 struct list queue_entry;
78 unsigned long ckey;
79 unsigned long cvalue;
80 unsigned long information;
81 unsigned int status;
84 static void completion_destroy( struct object *obj)
86 struct completion *completion = (struct completion *) obj;
87 struct comp_msg *tmp, *next;
89 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
91 free( tmp );
95 static void completion_dump( struct object *obj, int verbose )
97 struct completion *completion = (struct completion *) obj;
99 assert( obj->ops == &completion_ops );
100 fprintf( stderr, "Completion " );
101 dump_object_name( &completion->obj );
102 fprintf( stderr, " (%u packets pending)\n", completion->depth );
105 static int completion_signaled( struct object *obj, struct thread *thread )
107 struct completion *completion = (struct completion *)obj;
109 return !list_empty( &completion->queue );
112 static struct completion *create_completion( struct directory *root, const struct unicode_str *name, unsigned int attr, unsigned int concurrent )
114 struct completion *completion;
116 if ((completion = create_named_object_dir( root, name, attr, &completion_ops )))
118 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
120 list_init( &completion->queue );
121 completion->depth = 0;
125 return completion;
128 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
130 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
133 static void add_completion( struct completion *completion, unsigned long ckey, unsigned long cvalue, unsigned int status, unsigned long information )
135 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
137 if (!msg)
138 return;
140 msg->ckey = ckey;
141 msg->cvalue = cvalue;
142 msg->status = status;
143 msg->information = information;
145 list_add_tail( &completion->queue, &msg->queue_entry );
146 completion->depth++;
147 wake_up( &completion->obj, 1 );
150 /* create a completion */
151 DECL_HANDLER(create_completion)
153 struct completion *completion;
154 struct unicode_str name;
155 struct directory *root = NULL;
157 reply->handle = 0;
159 get_req_unicode_str( &name );
160 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
161 return;
163 if ( (completion = create_completion( root, &name, req->attributes, req->concurrent )) != NULL )
165 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
166 release_object( completion );
169 if (root) release_object( root );
172 /* open a completion */
173 DECL_HANDLER(open_completion)
175 struct completion *completion;
176 struct unicode_str name;
177 struct directory *root = NULL;
179 reply->handle = 0;
181 get_req_unicode_str( &name );
182 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
183 return;
185 if ( (completion = open_object_dir( root, &name, req->attributes, &completion_ops )) != NULL )
187 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
188 release_object( completion );
191 if (root) release_object( root );
195 /* add completion to completion port */
196 DECL_HANDLER(add_completion)
198 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
200 if (!completion) return;
202 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
204 release_object( completion );
207 /* get completion from completion port */
208 DECL_HANDLER(remove_completion)
210 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
211 struct list *entry;
212 struct comp_msg *msg;
214 if (!completion) return;
216 entry = list_head( &completion->queue );
217 if (!entry)
218 set_error( STATUS_PENDING );
219 else
221 list_remove( entry );
222 completion->depth--;
223 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
224 reply->ckey = msg->ckey;
225 reply->cvalue = msg->cvalue;
226 reply->status = msg->status;
227 reply->information = msg->information;
228 free( msg );
231 release_object( completion );
234 /* get queue depth for completion port */
235 DECL_HANDLER(query_completion)
237 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
239 if (!completion) return;
241 reply->depth = completion->depth;
243 release_object( completion );