2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
20 struct object obj
; /* object header */
21 struct thread
*owner
; /* mutex owner */
22 unsigned int count
; /* recursion count */
23 int abandoned
; /* has it been abandoned? */
28 static void mutex_dump( struct object
*obj
, int verbose
);
29 static int mutex_signaled( struct object
*obj
, struct thread
*thread
);
30 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
);
31 static void mutex_destroy( struct object
*obj
);
33 static const struct object_ops mutex_ops
=
49 static struct mutex
*create_mutex( const WCHAR
*name
, size_t len
, int owned
)
53 if ((mutex
= create_named_object( &mutex_ops
, name
, len
)))
55 if (get_error() != ERROR_ALREADY_EXISTS
)
57 /* initialize it if it didn't already exist */
61 mutex
->next
= mutex
->prev
= NULL
;
62 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
68 /* release a mutex once the recursion count is 0 */
69 static void do_release( struct mutex
*mutex
)
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 mutex
->owner
->mutex
= mutex
->next
;
77 mutex
->next
= mutex
->prev
= NULL
;
78 wake_up( &mutex
->obj
, 0 );
81 void abandon_mutexes( struct thread
*thread
)
85 struct mutex
*mutex
= thread
->mutex
;
86 assert( mutex
->owner
== thread
);
93 static void mutex_dump( struct object
*obj
, int verbose
)
95 struct mutex
*mutex
= (struct mutex
*)obj
;
96 assert( obj
->ops
== &mutex_ops
);
97 fprintf( stderr
, "Mutex count=%u owner=%p ", mutex
->count
, mutex
->owner
);
98 dump_object_name( &mutex
->obj
);
99 fputc( '\n', stderr
);
102 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
104 struct mutex
*mutex
= (struct mutex
*)obj
;
105 assert( obj
->ops
== &mutex_ops
);
106 return (!mutex
->count
|| (mutex
->owner
== thread
));
109 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
111 struct mutex
*mutex
= (struct mutex
*)obj
;
112 assert( obj
->ops
== &mutex_ops
);
113 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
115 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
117 assert( !mutex
->owner
);
118 mutex
->owner
= thread
;
120 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
121 thread
->mutex
= mutex
;
123 if (!mutex
->abandoned
) return 0;
124 mutex
->abandoned
= 0;
128 static void mutex_destroy( struct object
*obj
)
130 struct mutex
*mutex
= (struct mutex
*)obj
;
131 assert( obj
->ops
== &mutex_ops
);
133 if (!mutex
->count
) return;
139 DECL_HANDLER(create_mutex
)
141 size_t len
= get_req_strlenW( req
->name
);
145 if ((mutex
= create_mutex( req
->name
, len
, req
->owned
)))
147 req
->handle
= alloc_handle( current
->process
, mutex
, MUTEX_ALL_ACCESS
, req
->inherit
);
148 release_object( mutex
);
152 /* open a handle to a mutex */
153 DECL_HANDLER(open_mutex
)
155 size_t len
= get_req_strlenW( req
->name
);
156 req
->handle
= open_object( req
->name
, len
, &mutex_ops
, req
->access
, req
->inherit
);
159 /* release a mutex */
160 DECL_HANDLER(release_mutex
)
164 if ((mutex
= (struct mutex
*)get_handle_obj( current
->process
, req
->handle
,
165 MUTEX_MODIFY_STATE
, &mutex_ops
)))
167 if (!mutex
->count
|| (mutex
->owner
!= current
)) set_error( ERROR_NOT_OWNER
);
168 else if (!--mutex
->count
) do_release( mutex
);
169 release_object( mutex
);