taskmgr: Make FillSolidRect2() and Draw3dRect2?() static.
[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 void completion_destroy( struct object * );
57 static int  completion_signaled( struct object *obj, struct thread *thread );
58
59 static const struct object_ops completion_ops =
60 {
61     sizeof(struct completion), /* size */
62     completion_dump,           /* dump */
63     completion_get_type,       /* get_type */
64     add_queue,                 /* add_queue */
65     remove_queue,              /* remove_queue */
66     completion_signaled,       /* signaled */
67     no_satisfied,              /* satisfied */
68     no_signal,                 /* signal */
69     no_get_fd,                 /* get_fd */
70     no_map_access,             /* map_access */
71     default_get_sd,            /* get_sd */
72     default_set_sd,            /* set_sd */
73     no_lookup_name,            /* lookup_name */
74     no_open_file,              /* open_file */
75     no_close_handle,           /* close_handle */
76     completion_destroy         /* destroy */
77 };
78
79 struct comp_msg
80 {
81     struct   list queue_entry;
82     apc_param_t   ckey;
83     apc_param_t   cvalue;
84     unsigned int  information;
85     unsigned int  status;
86 };
87
88 static void completion_destroy( struct object *obj)
89 {
90     struct completion *completion = (struct completion *) obj;
91     struct comp_msg *tmp, *next;
92
93     LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
94     {
95         free( tmp );
96     }
97 }
98
99 static void completion_dump( struct object *obj, int verbose )
100 {
101     struct completion *completion = (struct completion *) obj;
102
103     assert( obj->ops == &completion_ops );
104     fprintf( stderr, "Completion " );
105     dump_object_name( &completion->obj );
106     fprintf( stderr, " (%u packets pending)\n", completion->depth );
107 }
108
109 static struct object_type *completion_get_type( struct object *obj )
110 {
111     static const WCHAR name[] = {'C','o','m','p','l','e','t','i','o','n'};
112     static const struct unicode_str str = { name, sizeof(name) };
113     return get_object_type( &str );
114 }
115
116 static int completion_signaled( struct object *obj, struct thread *thread )
117 {
118     struct completion *completion = (struct completion *)obj;
119
120     return !list_empty( &completion->queue );
121 }
122
123 static struct completion *create_completion( struct directory *root, const struct unicode_str *name, unsigned int attr, unsigned int concurrent )
124 {
125     struct completion *completion;
126
127     if ((completion = create_named_object_dir( root, name, attr, &completion_ops )))
128     {
129         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
130         {
131             list_init( &completion->queue );
132             completion->depth = 0;
133         }
134     }
135
136     return completion;
137 }
138
139 struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
140 {
141     return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
142 }
143
144 void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
145                      unsigned int status, unsigned int information )
146 {
147     struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
148
149     if (!msg)
150         return;
151
152     msg->ckey = ckey;
153     msg->cvalue = cvalue;
154     msg->status = status;
155     msg->information = information;
156
157     list_add_tail( &completion->queue, &msg->queue_entry );
158     completion->depth++;
159     wake_up( &completion->obj, 1 );
160 }
161
162 /* create a completion */
163 DECL_HANDLER(create_completion)
164 {
165     struct completion *completion;
166     struct unicode_str name;
167     struct directory *root = NULL;
168
169     reply->handle = 0;
170
171     get_req_unicode_str( &name );
172     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
173         return;
174
175     if ( (completion = create_completion( root, &name, req->attributes, req->concurrent )) != NULL )
176     {
177         reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
178         release_object( completion );
179     }
180
181     if (root) release_object( root );
182 }
183
184 /* open a completion */
185 DECL_HANDLER(open_completion)
186 {
187     struct completion *completion;
188     struct unicode_str name;
189     struct directory *root = NULL;
190
191     reply->handle = 0;
192
193     get_req_unicode_str( &name );
194     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
195         return;
196
197     if ( (completion = open_object_dir( root, &name, req->attributes, &completion_ops )) != NULL )
198     {
199         reply->handle = alloc_handle( current->process, completion, req->access, req->attributes );
200         release_object( completion );
201     }
202
203     if (root) release_object( root );
204 }
205
206
207 /* add completion to completion port */
208 DECL_HANDLER(add_completion)
209 {
210     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
211
212     if (!completion) return;
213
214     add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
215
216     release_object( completion );
217 }
218
219 /* get completion from completion port */
220 DECL_HANDLER(remove_completion)
221 {
222     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
223     struct list *entry;
224     struct comp_msg *msg;
225
226     if (!completion) return;
227
228     entry = list_head( &completion->queue );
229     if (!entry)
230         set_error( STATUS_PENDING );
231     else
232     {
233         list_remove( entry );
234         completion->depth--;
235         msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
236         reply->ckey = msg->ckey;
237         reply->cvalue = msg->cvalue;
238         reply->status = msg->status;
239         reply->information = msg->information;
240         free( msg );
241     }
242
243     release_object( completion );
244 }
245
246 /* get queue depth for completion port */
247 DECL_HANDLER(query_completion)
248 {
249     struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
250
251     if (!completion) return;
252
253     reply->depth = completion->depth;
254
255     release_object( completion );
256 }