Add name_lookup function in object_ops.
[wine] / server / mutex.c
1 /*
2  * Server-side mutex 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 #include <stdarg.h>
28
29 #include "windef.h"
30 #include "winternl.h"
31
32 #include "handle.h"
33 #include "thread.h"
34 #include "request.h"
35
36 struct mutex
37 {
38     struct object  obj;             /* object header */
39     struct thread *owner;           /* mutex owner */
40     unsigned int   count;           /* recursion count */
41     int            abandoned;       /* has it been abandoned? */
42     struct list    entry;           /* entry in owner thread mutex list */
43 };
44
45 static void mutex_dump( struct object *obj, int verbose );
46 static int mutex_signaled( struct object *obj, struct thread *thread );
47 static int mutex_satisfied( struct object *obj, struct thread *thread );
48 static void mutex_destroy( struct object *obj );
49 static int mutex_signal( struct object *obj, unsigned int access );
50
51 static const struct object_ops mutex_ops =
52 {
53     sizeof(struct mutex),      /* size */
54     mutex_dump,                /* dump */
55     add_queue,                 /* add_queue */
56     remove_queue,              /* remove_queue */
57     mutex_signaled,            /* signaled */
58     mutex_satisfied,           /* satisfied */
59     mutex_signal,              /* signal */
60     no_get_fd,                 /* get_fd */
61     no_lookup_name,            /* lookup_name */
62     no_close_handle,           /* close_handle */
63     mutex_destroy              /* destroy */
64 };
65
66
67 static struct mutex *create_mutex( const struct unicode_str *name, unsigned int attr, int owned )
68 {
69     struct mutex *mutex;
70
71     if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, attr )))
72     {
73         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
74         {
75             /* initialize it if it didn't already exist */
76             mutex->count = 0;
77             mutex->owner = NULL;
78             mutex->abandoned = 0;
79             if (owned) mutex_satisfied( &mutex->obj, current );
80         }
81     }
82     return mutex;
83 }
84
85 /* release a mutex once the recursion count is 0 */
86 static void do_release( struct mutex *mutex )
87 {
88     assert( !mutex->count );
89     /* remove the mutex from the thread list of owned mutexes */
90     list_remove( &mutex->entry );
91     mutex->owner = NULL;
92     wake_up( &mutex->obj, 0 );
93 }
94
95 void abandon_mutexes( struct thread *thread )
96 {
97     struct list *ptr;
98
99     while ((ptr = list_head( &thread->mutex_list )) != NULL)
100     {
101         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
102         assert( mutex->owner == thread );
103         mutex->count = 0;
104         mutex->abandoned = 1;
105         do_release( mutex );
106     }
107 }
108
109 static void mutex_dump( struct object *obj, int verbose )
110 {
111     struct mutex *mutex = (struct mutex *)obj;
112     assert( obj->ops == &mutex_ops );
113     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
114     dump_object_name( &mutex->obj );
115     fputc( '\n', stderr );
116 }
117
118 static int mutex_signaled( struct object *obj, struct thread *thread )
119 {
120     struct mutex *mutex = (struct mutex *)obj;
121     assert( obj->ops == &mutex_ops );
122     return (!mutex->count || (mutex->owner == thread));
123 }
124
125 static int mutex_satisfied( struct object *obj, struct thread *thread )
126 {
127     struct mutex *mutex = (struct mutex *)obj;
128     assert( obj->ops == &mutex_ops );
129     assert( !mutex->count || (mutex->owner == thread) );
130
131     if (!mutex->count++)  /* FIXME: avoid wrap-around */
132     {
133         assert( !mutex->owner );
134         mutex->owner = thread;
135         list_add_head( &thread->mutex_list, &mutex->entry );
136     }
137     if (!mutex->abandoned) return 0;
138     mutex->abandoned = 0;
139     return 1;
140 }
141
142 static int mutex_signal( struct object *obj, unsigned int access )
143 {
144     struct mutex *mutex = (struct mutex *)obj;
145     assert( obj->ops == &mutex_ops );
146
147     if (!(access & SYNCHRONIZE))  /* FIXME: MUTEX_MODIFY_STATE? */
148     {
149         set_error( STATUS_ACCESS_DENIED );
150         return 0;
151     }
152     if (!mutex->count || (mutex->owner != current))
153     {
154         set_error( STATUS_MUTANT_NOT_OWNED );
155         return 0;
156     }
157     if (!--mutex->count) do_release( mutex );
158     return 1;
159 }
160
161 static void mutex_destroy( struct object *obj )
162 {
163     struct mutex *mutex = (struct mutex *)obj;
164     assert( obj->ops == &mutex_ops );
165
166     if (!mutex->count) return;
167     mutex->count = 0;
168     do_release( mutex );
169 }
170
171 /* create a mutex */
172 DECL_HANDLER(create_mutex)
173 {
174     struct mutex *mutex;
175     struct unicode_str name;
176
177     reply->handle = 0;
178     get_req_unicode_str( &name );
179     if ((mutex = create_mutex( &name, req->attributes, req->owned )))
180     {
181         reply->handle = alloc_handle( current->process, mutex, req->access,
182                                       req->attributes & OBJ_INHERIT );
183         release_object( mutex );
184     }
185 }
186
187 /* open a handle to a mutex */
188 DECL_HANDLER(open_mutex)
189 {
190     struct unicode_str name;
191
192     get_req_unicode_str( &name );
193     reply->handle = open_object( sync_namespace, &name, &mutex_ops, req->access, req->attributes );
194 }
195
196 /* release a mutex */
197 DECL_HANDLER(release_mutex)
198 {
199     struct mutex *mutex;
200
201     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
202                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
203     {
204         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
205         else
206         {
207             reply->prev_count = mutex->count;
208             if (!--mutex->count) do_release( mutex );
209         }
210         release_object( mutex );
211     }
212 }