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
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
30 #include "wine/port.h"
36 #define WIN32_NO_STATUS
40 #include "wine/unicode.h"
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 */
77 struct list queue_entry;
80 unsigned long information;
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 )
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;
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 ) );
141 msg->cvalue = cvalue;
142 msg->status = status;
143 msg->information = information;
145 list_add_tail( &completion->queue, &msg->queue_entry );
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;
159 get_req_unicode_str( &name );
160 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
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;
181 get_req_unicode_str( &name );
182 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
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 );
212 struct comp_msg *msg;
214 if (!completion) return;
216 entry = list_head( &completion->queue );
218 set_error( STATUS_PENDING );
221 list_remove( entry );
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;
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 );