Release 1.5.29.
[wine] / server / completion.c
1 /*
2  * Server-side IO completion ports implementation
3  *
4  * Copyright (C) 2007 Andrey Turkin
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdarg.h>
33 #include <stdio.h>
34
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winternl.h"
39
40 #include "wine/unicode.h"
41 #include "object.h"
42 #include "file.h"
43 #include "handle.h"
44 #include "request.h"
45
46
47 struct completion
48 {
49     struct object  obj;
50     struct list    queue;
51     unsigned int   depth;
52 };
53
54 static void completion_dump( struct object*, int );
55 static struct object_type *completion_get_type( struct object *obj );
56 static int completion_signaled( struct object *obj, struct thread *thread );
57 static unsigned int completion_map_access( struct object *obj, unsigned int access );
58 static void completion_destroy( struct object * );
59
60 static const struct object_ops completion_ops =
61 {
62     sizeof(struct completion), /* size */
63     completion_dump,           /* dump */
64     completion_get_type,       /* get_type */
65     add_queue,                 /* add_queue */
66     remove_queue,              /* remove_queue */
67     completion_signaled,       /* signaled */
68     no_satisfied,              /* satisfied */
69     no_signal,                 /* signal */
70     no_get_fd,                 /* get_fd */
71     completion_map_access,     /* map_access */
72     default_get_sd,            /* get_sd */
73     default_set_sd,            /* set_sd */
74     no_lookup_name,            /* lookup_name */
75     no_open_file,              /* open_file */
76     no_close_handle,           /* close_handle */
77     completion_destroy         /* destroy */
78 };
79
80 struct comp_msg
81 {
82     struct   list queue_entry;
83     apc_param_t   ckey;
84     apc_param_t   cvalue;
85     unsigned int  information;
86     unsigned int  status;
87 };
88
89 static void completion_destroy( struct object *obj)
90 {
91     struct completion *completion = (struct completion *) obj;
92     struct comp_msg *tmp, *next;
93
94     LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
95     {
96         free( tmp );
97     }
98 }
99
100 static void completion_dump( struct object *obj, int verbose )
101 {
102     struct completion *completion = (struct completion *) obj;
103
104     assert( obj->ops == &completion_ops );
105     fprintf( stderr, "Completion " );
106     dump_object_name( &completion->obj );
107     fprintf( stderr, " (%u packets pending)\n", completion->depth );
108 }
109
110 static struct object_type *completion_get_type( struct object *obj )
111 {
112     static const WCHAR name[] = {'C','o','m','p','l','e','t','i','o','n'};
113     static const struct unicode_str str = { name, sizeof(name) };
114     return get_object_type( &str );
115 }
116
117 static int completion_signaled( struct object *obj, struct thread *thread )
118 {
119     struct completion *completion = (struct completion *)obj;
120
121     return !list_empty( &completion->queue );
122 }
123
124 static unsigned int completion_map_access( struct object *obj, unsigned int access )
125 {
126     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | IO_COMPLETION_QUERY_STATE;
127     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
128     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
129     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL | IO_COMPLETION_ALL_ACCESS;
130     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
131 }
132
133 static struct completion *create_completion( struct directory *root, const struct unicode_str *name, unsigned int attr, unsigned int concurrent )
134 {
135     struct completion *completion;
136
137     if ((completion = create_named_object_dir( root, name, attr, &completion_ops )))
138     {
139         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
140         {
141             list_init( &completion->queue );
142             completion->depth = 0;
143         }
144     }
145
146     return completion;
147 }
148
149 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
150 {
151     return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
152 }
153
154 void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
155                      unsigned int status, unsigned int information )
156 {
157     struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
158
159     if (!msg)
160         return;
161
162     msg->ckey = ckey;
163     msg->cvalue = cvalue;
164     msg->status = status;
165     msg->information = information;
166
167     list_add_tail( &completion->queue, &msg->queue_entry );
168     completion->depth++;
169     wake_up( &completion->obj, 1 );
170 }
171
172 /* create a completion */
173 DECL_HANDLER(create_completion)
174 {
175     struct completion *completion;
176     struct unicode_str name;
177     struct directory *root = NULL;
178
179     reply->handle = 0;
180
181     get_req_unicode_str( &name );
182     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
183         return;
184
185     if ( (completion = create_completion( root, &name, req->attributes, req->concurrent )) != NULL )
186     {
187         reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
188         release_object( completion );
189     }
190
191     if (root) release_object( root );
192 }
193
194 /* open a completion */
195 DECL_HANDLER(open_completion)
196 {
197     struct completion *completion;
198     struct unicode_str name;
199     struct directory *root = NULL;
200
201     reply->handle = 0;
202
203     get_req_unicode_str( &name );
204     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
205         return;
206
207     if ( (completion = open_object_dir( root, &name, req->attributes, &completion_ops )) != NULL )
208     {
209         reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
210         release_object( completion );
211     }
212
213     if (root) release_object( root );
214 }
215
216
217 /* add completion to completion port */
218 DECL_HANDLER(add_completion)
219 {
220     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
221
222     if (!completion) return;
223
224     add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
225
226     release_object( completion );
227 }
228
229 /* get completion from completion port */
230 DECL_HANDLER(remove_completion)
231 {
232     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
233     struct list *entry;
234     struct comp_msg *msg;
235
236     if (!completion) return;
237
238     entry = list_head( &completion->queue );
239     if (!entry)
240         set_error( STATUS_PENDING );
241     else
242     {
243         list_remove( entry );
244         completion->depth--;
245         msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
246         reply->ckey = msg->ckey;
247         reply->cvalue = msg->cvalue;
248         reply->status = msg->status;
249         reply->information = msg->information;
250         free( msg );
251     }
252
253     release_object( completion );
254 }
255
256 /* get queue depth for completion port */
257 DECL_HANDLER(query_completion)
258 {
259     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
260
261     if (!completion) return;
262
263     reply->depth = completion->depth;
264
265     release_object( completion );
266 }