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 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
=
45 struct object
*create_mutex( const char *name
, int owned
)
49 if (!(mutex
= (struct mutex
*)create_named_object( name
, &mutex_ops
, sizeof(*mutex
) )))
51 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
53 /* initialize it if it didn't already exist */
57 mutex
->next
= mutex
->prev
= NULL
;
58 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
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
;
77 mutex
->next
= mutex
->prev
= NULL
;
78 wake_up( &mutex
->obj
, 0 );
81 int release_mutex( int handle
)
85 if (!(mutex
= (struct mutex
*)get_handle_obj( current
->process
, handle
,
86 MUTEX_MODIFY_STATE
, &mutex_ops
)))
88 if (!mutex
->count
|| (mutex
->owner
!= current
))
90 SET_ERROR( ERROR_NOT_OWNER
);
93 if (!--mutex
->count
) do_release( mutex
, current
);
94 release_object( mutex
);
98 void abandon_mutexes( struct thread
*thread
)
100 while (thread
->mutex
)
102 struct mutex
*mutex
= thread
->mutex
;
103 assert( mutex
->owner
== thread
);
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
;
136 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
137 thread
->mutex
= mutex
;
139 if (!mutex
->abandoned
) return 0;
140 mutex
->abandoned
= 0;
144 static void mutex_destroy( struct object
*obj
)
146 struct mutex
*mutex
= (struct mutex
*)obj
;
147 assert( obj
->ops
== &mutex_ops
);