~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/server/mutex.c

Version: ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
 30 #define WIN32_NO_STATUS
 31 #include "windef.h"
 32 #include "winternl.h"
 33 
 34 #include "handle.h"
 35 #include "thread.h"
 36 #include "request.h"
 37 #include "security.h"
 38 
 39 struct mutex
 40 {
 41     struct object  obj;             /* object header */
 42     struct thread *owner;           /* mutex owner */
 43     unsigned int   count;           /* recursion count */
 44     int            abandoned;       /* has it been abandoned? */
 45     struct list    entry;           /* entry in owner thread mutex list */
 46 };
 47 
 48 static void mutex_dump( struct object *obj, int verbose );
 49 static struct object_type *mutex_get_type( struct object *obj );
 50 static int mutex_signaled( struct object *obj, struct thread *thread );
 51 static int mutex_satisfied( struct object *obj, struct thread *thread );
 52 static unsigned int mutex_map_access( struct object *obj, unsigned int access );
 53 static void mutex_destroy( struct object *obj );
 54 static int mutex_signal( struct object *obj, unsigned int access );
 55 
 56 static const struct object_ops mutex_ops =
 57 {
 58     sizeof(struct mutex),      /* size */
 59     mutex_dump,                /* dump */
 60     mutex_get_type,            /* get_type */
 61     add_queue,                 /* add_queue */
 62     remove_queue,              /* remove_queue */
 63     mutex_signaled,            /* signaled */
 64     mutex_satisfied,           /* satisfied */
 65     mutex_signal,              /* signal */
 66     no_get_fd,                 /* get_fd */
 67     mutex_map_access,          /* map_access */
 68     default_get_sd,            /* get_sd */
 69     default_set_sd,            /* set_sd */
 70     no_lookup_name,            /* lookup_name */
 71     no_open_file,              /* open_file */
 72     no_close_handle,           /* close_handle */
 73     mutex_destroy              /* destroy */
 74 };
 75 
 76 
 77 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
 78                                    unsigned int attr, int owned, const struct security_descriptor *sd )
 79 {
 80     struct mutex *mutex;
 81 
 82     if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
 83     {
 84         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
 85         {
 86             /* initialize it if it didn't already exist */
 87             mutex->count = 0;
 88             mutex->owner = NULL;
 89             mutex->abandoned = 0;
 90             if (owned) mutex_satisfied( &mutex->obj, current );
 91             if (sd) default_set_sd( &mutex->obj, sd, OWNER_SECURITY_INFORMATION|
 92                                                      GROUP_SECURITY_INFORMATION|
 93                                                      DACL_SECURITY_INFORMATION|
 94                                                      SACL_SECURITY_INFORMATION );
 95         }
 96     }
 97     return mutex;
 98 }
 99 
100 /* release a mutex once the recursion count is 0 */
101 static void do_release( struct mutex *mutex )
102 {
103     assert( !mutex->count );
104     /* remove the mutex from the thread list of owned mutexes */
105     list_remove( &mutex->entry );
106     mutex->owner = NULL;
107     wake_up( &mutex->obj, 0 );
108 }
109 
110 void abandon_mutexes( struct thread *thread )
111 {
112     struct list *ptr;
113 
114     while ((ptr = list_head( &thread->mutex_list )) != NULL)
115     {
116         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
117         assert( mutex->owner == thread );
118         mutex->count = 0;
119         mutex->abandoned = 1;
120         do_release( mutex );
121     }
122 }
123 
124 static void mutex_dump( struct object *obj, int verbose )
125 {
126     struct mutex *mutex = (struct mutex *)obj;
127     assert( obj->ops == &mutex_ops );
128     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
129     dump_object_name( &mutex->obj );
130     fputc( '\n', stderr );
131 }
132 
133 static struct object_type *mutex_get_type( struct object *obj )
134 {
135     static const WCHAR name[] = {'M','u','t','a','n','t'};
136     static const struct unicode_str str = { name, sizeof(name) };
137     return get_object_type( &str );
138 }
139 
140 static int mutex_signaled( struct object *obj, struct thread *thread )
141 {
142     struct mutex *mutex = (struct mutex *)obj;
143     assert( obj->ops == &mutex_ops );
144     return (!mutex->count || (mutex->owner == thread));
145 }
146 
147 static int mutex_satisfied( struct object *obj, struct thread *thread )
148 {
149     struct mutex *mutex = (struct mutex *)obj;
150     assert( obj->ops == &mutex_ops );
151     assert( !mutex->count || (mutex->owner == thread) );
152 
153     if (!mutex->count++)  /* FIXME: avoid wrap-around */
154     {
155         assert( !mutex->owner );
156         mutex->owner = thread;
157         list_add_head( &thread->mutex_list, &mutex->entry );
158     }
159     if (!mutex->abandoned) return 0;
160     mutex->abandoned = 0;
161     return 1;
162 }
163 
164 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
165 {
166     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
167     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE;
168     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
169     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
170     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
171 }
172 
173 static int mutex_signal( struct object *obj, unsigned int access )
174 {
175     struct mutex *mutex = (struct mutex *)obj;
176     assert( obj->ops == &mutex_ops );
177 
178     if (!(access & SYNCHRONIZE))  /* FIXME: MUTEX_MODIFY_STATE? */
179     {
180         set_error( STATUS_ACCESS_DENIED );
181         return 0;
182     }
183     if (!mutex->count || (mutex->owner != current))
184     {
185         set_error( STATUS_MUTANT_NOT_OWNED );
186         return 0;
187     }
188     if (!--mutex->count) do_release( mutex );
189     return 1;
190 }
191 
192 static void mutex_destroy( struct object *obj )
193 {
194     struct mutex *mutex = (struct mutex *)obj;
195     assert( obj->ops == &mutex_ops );
196 
197     if (!mutex->count) return;
198     mutex->count = 0;
199     do_release( mutex );
200 }
201 
202 /* create a mutex */
203 DECL_HANDLER(create_mutex)
204 {
205     struct mutex *mutex;
206     struct unicode_str name;
207     struct directory *root = NULL;
208     const struct object_attributes *objattr = get_req_data();
209     const struct security_descriptor *sd;
210 
211     reply->handle = 0;
212 
213     if (!objattr_is_valid( objattr, get_req_data_size() ))
214         return;
215 
216     sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
217     objattr_get_name( objattr, &name );
218 
219     if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
220         return;
221 
222     if ((mutex = create_mutex( root, &name, req->attributes, req->owned, sd )))
223     {
224         if (get_error() == STATUS_OBJECT_NAME_EXISTS)
225             reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
226         else
227             reply->handle = alloc_handle_no_access_check( current->process, mutex, req->access, req->attributes );
228         release_object( mutex );
229     }
230 
231     if (root) release_object( root );
232 }
233 
234 /* open a handle to a mutex */
235 DECL_HANDLER(open_mutex)
236 {
237     struct unicode_str name;
238     struct directory *root = NULL;
239     struct mutex *mutex;
240 
241     get_req_unicode_str( &name );
242     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
243         return;
244 
245     if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
246     {
247         reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
248         release_object( mutex );
249     }
250 
251     if (root) release_object( root );
252 }
253 
254 /* release a mutex */
255 DECL_HANDLER(release_mutex)
256 {
257     struct mutex *mutex;
258 
259     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
260                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
261     {
262         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
263         else
264         {
265             reply->prev_count = mutex->count;
266             if (!--mutex->count) do_release( mutex );
267         }
268         release_object( mutex );
269     }
270 }
271 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.