Recovery of release 990110 after disk crash.
[wine.git] / server / mutex.c
blob62fe216dd998c3249832beb681e3e6e2bd3954a4
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "winerror.h"
12 #include "winnt.h"
13 #include "server/thread.h"
15 struct mutex
17 struct object obj; /* object header */
18 struct thread *owner; /* mutex owner */
19 unsigned int count; /* recursion count */
20 int abandoned; /* has it been abandoned? */
21 struct mutex *next;
22 struct mutex *prev;
25 static void mutex_dump( struct object *obj, int verbose );
26 static int mutex_signaled( struct object *obj, struct thread *thread );
27 static int mutex_satisfied( struct object *obj, struct thread *thread );
28 static void mutex_destroy( struct object *obj );
30 static const struct object_ops mutex_ops =
32 mutex_dump,
33 add_queue,
34 remove_queue,
35 mutex_signaled,
36 mutex_satisfied,
37 no_read_fd,
38 no_write_fd,
39 no_flush,
40 no_get_file_info,
41 mutex_destroy
45 struct object *create_mutex( const char *name, int owned )
47 struct mutex *mutex;
49 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
50 return NULL;
51 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
53 /* initialize it if it didn't already exist */
54 mutex->count = 0;
55 mutex->owner = NULL;
56 mutex->abandoned = 0;
57 mutex->next = mutex->prev = NULL;
58 if (owned) mutex_satisfied( &mutex->obj, current );
60 return &mutex->obj;
63 int open_mutex( unsigned int access, int inherit, const char *name )
65 return open_object( name, &mutex_ops, access, inherit );
68 /* release a mutex once the recursion count is 0 */
69 static void do_release( struct mutex *mutex, struct thread *thread )
71 assert( !mutex->count );
72 /* remove the mutex from the thread list of owned mutexes */
73 if (mutex->next) mutex->next->prev = mutex->prev;
74 if (mutex->prev) mutex->prev->next = mutex->next;
75 else thread->mutex = mutex->next;
76 mutex->owner = NULL;
77 mutex->next = mutex->prev = NULL;
78 wake_up( &mutex->obj, 0 );
81 int release_mutex( int handle )
83 struct mutex *mutex;
85 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
86 MUTEX_MODIFY_STATE, &mutex_ops )))
87 return 0;
88 if (!mutex->count || (mutex->owner != current))
90 SET_ERROR( ERROR_NOT_OWNER );
91 return 0;
93 if (!--mutex->count) do_release( mutex, current );
94 release_object( mutex );
95 return 1;
98 void abandon_mutexes( struct thread *thread )
100 while (thread->mutex)
102 struct mutex *mutex = thread->mutex;
103 assert( mutex->owner == thread );
104 mutex->count = 0;
105 mutex->abandoned = 1;
106 do_release( mutex, thread );
110 static void mutex_dump( struct object *obj, int verbose )
112 struct mutex *mutex = (struct mutex *)obj;
113 assert( obj->ops == &mutex_ops );
114 printf( "Mutex count=%u owner=%p name='%s'\n",
115 mutex->count, mutex->owner, get_object_name( &mutex->obj) );
118 static int mutex_signaled( struct object *obj, struct thread *thread )
120 struct mutex *mutex = (struct mutex *)obj;
121 assert( obj->ops == &mutex_ops );
122 return (!mutex->count || (mutex->owner == thread));
125 static int mutex_satisfied( struct object *obj, struct thread *thread )
127 struct mutex *mutex = (struct mutex *)obj;
128 assert( obj->ops == &mutex_ops );
129 assert( !mutex->count || (mutex->owner == thread) );
131 if (!mutex->count++) /* FIXME: avoid wrap-around */
133 assert( !mutex->owner );
134 mutex->owner = thread;
135 mutex->prev = NULL;
136 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
137 thread->mutex = mutex;
139 if (!mutex->abandoned) return 0;
140 mutex->abandoned = 0;
141 return 1;
144 static void mutex_destroy( struct object *obj )
146 struct mutex *mutex = (struct mutex *)obj;
147 assert( obj->ops == &mutex_ops );
148 free( mutex );