push c2f455f1c8ebe6b078e404988318bd3426429fe5
[wine/hacks.git] / server / completion.c
blob1282bb47d93b0f7446dd2cec0ed06f4370c9b5e5
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 "handle.h"
43 #include "request.h"
46 struct completion
48 struct object obj;
49 struct list queue;
50 unsigned int depth;
53 static void completion_dump( struct object*, int );
54 static void completion_destroy( struct object * );
55 static int completion_signaled( struct object *obj, struct thread *thread );
57 static const struct object_ops completion_ops =
59 sizeof(struct completion), /* size */
60 completion_dump, /* dump */
61 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 completion_signaled, /* signaled */
64 no_satisfied, /* satisfied */
65 no_signal, /* signal */
66 no_get_fd, /* get_fd */
67 no_map_access, /* map_access */
68 no_lookup_name, /* lookup_name */
69 no_open_file, /* open_file */
70 no_close_handle, /* close_handle */
71 completion_destroy /* destroy */
74 struct comp_msg
76 struct list queue_entry;
77 unsigned long ckey;
78 unsigned long cvalue;
79 unsigned long information;
80 unsigned int status;
83 static void completion_destroy( struct object *obj)
85 struct completion *completion = (struct completion *) obj;
86 struct comp_msg *tmp, *next;
88 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
90 free( tmp );
94 static void completion_dump( struct object *obj, int verbose )
96 struct completion *completion = (struct completion *) obj;
98 assert( obj->ops == &completion_ops );
99 fprintf( stderr, "Completion " );
100 dump_object_name( &completion->obj );
101 fprintf( stderr, " (%u packets pending)\n", completion->depth );
104 static int completion_signaled( struct object *obj, struct thread *thread )
106 struct completion *completion = (struct completion *)obj;
108 return !list_empty( &completion->queue );
111 static struct completion *create_completion( struct directory *root, const struct unicode_str *name, unsigned int attr, unsigned int concurrent )
113 struct completion *completion;
115 if ((completion = create_named_object_dir( root, name, attr, &completion_ops )))
117 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
119 list_init( &completion->queue );
120 completion->depth = 0;
124 return completion;
127 static struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
129 return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
132 static void add_completion( struct completion *completion, unsigned long ckey, unsigned long cvalue, unsigned int status, unsigned long information )
134 struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
136 if (!msg)
137 return;
139 msg->ckey = ckey;
140 msg->cvalue = cvalue;
141 msg->status = status;
142 msg->information = information;
144 list_add_tail( &completion->queue, &msg->queue_entry );
145 completion->depth++;
146 wake_up( &completion->obj, 1 );
149 /* create a completion */
150 DECL_HANDLER(create_completion)
152 struct completion *completion;
153 struct unicode_str name;
154 struct directory *root = NULL;
156 reply->handle = 0;
158 get_req_unicode_str( &name );
159 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
160 return;
162 if ( (completion = create_completion( root, &name, req->attributes, req->concurrent )) != NULL )
164 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
165 release_object( completion );
168 if (root) release_object( root );
171 /* open a completion */
172 DECL_HANDLER(open_completion)
174 struct completion *completion;
175 struct unicode_str name;
176 struct directory *root = NULL;
178 reply->handle = 0;
180 get_req_unicode_str( &name );
181 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
182 return;
184 if ( (completion = open_object_dir( root, &name, req->attributes, &completion_ops )) != NULL )
186 reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
187 release_object( completion );
190 if (root) release_object( root );
194 /* add completion to completion port */
195 DECL_HANDLER(add_completion)
197 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
199 if (!completion) return;
201 add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
203 release_object( completion );
206 /* get completion from completion port */
207 DECL_HANDLER(remove_completion)
209 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
210 struct list *entry;
211 struct comp_msg *msg;
213 if (!completion) return;
215 entry = list_head( &completion->queue );
216 if (!entry)
217 set_error( STATUS_PENDING );
218 else
220 list_remove( entry );
221 completion->depth--;
222 msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
223 reply->ckey = msg->ckey;
224 reply->cvalue = msg->cvalue;
225 reply->status = msg->status;
226 reply->information = msg->information;
227 free( msg );
230 release_object( completion );
233 /* get queue depth for completion port */
234 DECL_HANDLER(query_completion)
236 struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
238 if (!completion) return;
240 reply->depth = completion->depth;
242 release_object( completion );