2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include "server/thread.h"
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? */
25 static void dump_mutex( 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 destroy_mutex( struct object
*obj
);
30 static const struct object_ops mutex_ops
=
39 struct object
*create_mutex( const char *name
, int owned
)
43 if (!(mutex
= (struct mutex
*)create_named_object( name
, &mutex_ops
, sizeof(*mutex
) )))
45 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
47 /* initialize it if it didn't already exist */
51 mutex
->next
= mutex
->prev
= NULL
;
52 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
57 int open_mutex( unsigned int access
, int inherit
, const char *name
)
59 return open_object( name
, &mutex_ops
, access
, inherit
);
62 /* release a mutex once the recursion count is 0 */
63 static void do_release( struct mutex
*mutex
, struct thread
*thread
)
65 assert( !mutex
->count
);
66 /* remove the mutex from the thread list of owned mutexes */
67 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
68 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
69 else thread
->mutex
= mutex
->next
;
71 mutex
->next
= mutex
->prev
= NULL
;
72 wake_up( &mutex
->obj
, 0 );
75 int release_mutex( int handle
)
79 if (!(mutex
= (struct mutex
*)get_handle_obj( current
->process
, handle
,
80 MUTEX_MODIFY_STATE
, &mutex_ops
)))
82 if (!mutex
->count
|| (mutex
->owner
!= current
))
84 SET_ERROR( ERROR_NOT_OWNER
);
87 if (!--mutex
->count
) do_release( mutex
, current
);
88 release_object( mutex
);
92 void abandon_mutexes( struct thread
*thread
)
96 struct mutex
*mutex
= thread
->mutex
;
97 assert( mutex
->owner
== thread
);
100 do_release( mutex
, thread
);
104 static void dump_mutex( struct object
*obj
, int verbose
)
106 struct mutex
*mutex
= (struct mutex
*)obj
;
107 assert( obj
->ops
== &mutex_ops
);
108 printf( "Mutex count=%u owner=%p\n", mutex
->count
, mutex
->owner
);
111 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
113 struct mutex
*mutex
= (struct mutex
*)obj
;
114 assert( obj
->ops
== &mutex_ops
);
115 return (!mutex
->count
|| (mutex
->owner
== thread
));
118 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
120 struct mutex
*mutex
= (struct mutex
*)obj
;
121 assert( obj
->ops
== &mutex_ops
);
122 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
124 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
126 assert( !mutex
->owner
);
127 mutex
->owner
= thread
;
129 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
130 thread
->mutex
= mutex
;
132 if (!mutex
->abandoned
) return 0;
133 mutex
->abandoned
= 0;
137 static void destroy_mutex( struct object
*obj
)
139 struct mutex
*mutex
= (struct mutex
*)obj
;
140 assert( obj
->ops
== &mutex_ops
);