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
=
41 struct object
*create_mutex( const char *name
, int owned
)
45 if (!(mutex
= (struct mutex
*)create_named_object( name
, &mutex_ops
, sizeof(*mutex
) )))
47 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
49 /* initialize it if it didn't already exist */
53 mutex
->next
= mutex
->prev
= NULL
;
54 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
59 int open_mutex( unsigned int access
, int inherit
, const char *name
)
61 return open_object( name
, &mutex_ops
, access
, inherit
);
64 /* release a mutex once the recursion count is 0 */
65 static void do_release( struct mutex
*mutex
, struct thread
*thread
)
67 assert( !mutex
->count
);
68 /* remove the mutex from the thread list of owned mutexes */
69 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
70 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
71 else thread
->mutex
= mutex
->next
;
73 mutex
->next
= mutex
->prev
= NULL
;
74 wake_up( &mutex
->obj
, 0 );
77 int release_mutex( int handle
)
81 if (!(mutex
= (struct mutex
*)get_handle_obj( current
->process
, handle
,
82 MUTEX_MODIFY_STATE
, &mutex_ops
)))
84 if (!mutex
->count
|| (mutex
->owner
!= current
))
86 SET_ERROR( ERROR_NOT_OWNER
);
89 if (!--mutex
->count
) do_release( mutex
, current
);
90 release_object( mutex
);
94 void abandon_mutexes( struct thread
*thread
)
98 struct mutex
*mutex
= thread
->mutex
;
99 assert( mutex
->owner
== thread
);
101 mutex
->abandoned
= 1;
102 do_release( mutex
, thread
);
106 static void mutex_dump( struct object
*obj
, int verbose
)
108 struct mutex
*mutex
= (struct mutex
*)obj
;
109 assert( obj
->ops
== &mutex_ops
);
110 printf( "Mutex count=%u owner=%p\n", mutex
->count
, mutex
->owner
);
113 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
115 struct mutex
*mutex
= (struct mutex
*)obj
;
116 assert( obj
->ops
== &mutex_ops
);
117 return (!mutex
->count
|| (mutex
->owner
== thread
));
120 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
122 struct mutex
*mutex
= (struct mutex
*)obj
;
123 assert( obj
->ops
== &mutex_ops
);
124 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
126 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
128 assert( !mutex
->owner
);
129 mutex
->owner
= thread
;
131 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
132 thread
->mutex
= mutex
;
134 if (!mutex
->abandoned
) return 0;
135 mutex
->abandoned
= 0;
139 static void mutex_destroy( struct object
*obj
)
141 struct mutex
*mutex
= (struct mutex
*)obj
;
142 assert( obj
->ops
== &mutex_ops
);