- Send whole OBJECT_ATTRIBUTES.Attributes to the server not just an
[wine] / server / semaphore.c
1 /*
2  * Server-side semaphore management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "windef.h"
29 #include "winternl.h"
30
31 #include "handle.h"
32 #include "thread.h"
33 #include "request.h"
34
35 struct semaphore
36 {
37     struct object  obj;    /* object header */
38     unsigned int   count;  /* current count */
39     unsigned int   max;    /* maximum possible count */
40 };
41
42 static void semaphore_dump( struct object *obj, int verbose );
43 static int semaphore_signaled( struct object *obj, struct thread *thread );
44 static int semaphore_satisfied( struct object *obj, struct thread *thread );
45 static int semaphore_signal( struct object *obj, unsigned int access );
46
47 static const struct object_ops semaphore_ops =
48 {
49     sizeof(struct semaphore),      /* size */
50     semaphore_dump,                /* dump */
51     add_queue,                     /* add_queue */
52     remove_queue,                  /* remove_queue */
53     semaphore_signaled,            /* signaled */
54     semaphore_satisfied,           /* satisfied */
55     semaphore_signal,              /* signal */
56     no_get_fd,                     /* get_fd */
57     no_close_handle,               /* close_handle */
58     no_destroy                     /* destroy */
59 };
60
61
62 static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
63                                            unsigned int initial, unsigned int max )
64 {
65     struct semaphore *sem;
66
67     if (!max || (initial > max))
68     {
69         set_error( STATUS_INVALID_PARAMETER );
70         return NULL;
71     }
72     if ((sem = create_named_object( sync_namespace, &semaphore_ops, name, len )))
73     {
74         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
75         {
76             /* initialize it if it didn't already exist */
77             sem->count = initial;
78             sem->max   = max;
79         }
80     }
81     return sem;
82 }
83
84 static int release_semaphore( struct semaphore *sem, unsigned int count,
85                               unsigned int *prev )
86 {
87     if (prev) *prev = sem->count;
88     if (sem->count + count < sem->count || sem->count + count > sem->max)
89     {
90         set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
91         return 0;
92     }
93     else if (sem->count)
94     {
95         /* there cannot be any thread to wake up if the count is != 0 */
96         sem->count += count;
97     }
98     else
99     {
100         sem->count = count;
101         wake_up( &sem->obj, count );
102     }
103     return 1;
104 }
105
106 static void semaphore_dump( struct object *obj, int verbose )
107 {
108     struct semaphore *sem = (struct semaphore *)obj;
109     assert( obj->ops == &semaphore_ops );
110     fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
111     dump_object_name( &sem->obj );
112     fputc( '\n', stderr );
113 }
114
115 static int semaphore_signaled( struct object *obj, struct thread *thread )
116 {
117     struct semaphore *sem = (struct semaphore *)obj;
118     assert( obj->ops == &semaphore_ops );
119     return (sem->count > 0);
120 }
121
122 static int semaphore_satisfied( struct object *obj, struct thread *thread )
123 {
124     struct semaphore *sem = (struct semaphore *)obj;
125     assert( obj->ops == &semaphore_ops );
126     assert( sem->count );
127     sem->count--;
128     return 0;  /* not abandoned */
129 }
130
131 static int semaphore_signal( struct object *obj, unsigned int access )
132 {
133     struct semaphore *sem = (struct semaphore *)obj;
134     assert( obj->ops == &semaphore_ops );
135
136     if (!(access & SEMAPHORE_MODIFY_STATE))
137     {
138         set_error( STATUS_ACCESS_DENIED );
139         return 0;
140     }
141     return release_semaphore( sem, 1, NULL );
142 }
143
144 /* create a semaphore */
145 DECL_HANDLER(create_semaphore)
146 {
147     struct semaphore *sem;
148
149     reply->handle = 0;
150     if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
151                                  req->initial, req->max )))
152     {
153         reply->handle = alloc_handle( current->process, sem, req->access,
154                                       req->attributes & OBJ_INHERIT );
155         release_object( sem );
156     }
157 }
158
159 /* open a handle to a semaphore */
160 DECL_HANDLER(open_semaphore)
161 {
162     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
163                                  &semaphore_ops, req->access, req->attributes );
164 }
165
166 /* release a semaphore */
167 DECL_HANDLER(release_semaphore)
168 {
169     struct semaphore *sem;
170
171     if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
172                                                    SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
173     {
174         release_semaphore( sem, req->count, &reply->prev_count );
175         release_object( sem );
176     }
177 }