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
);
32 static const struct object_ops mutex_ops
=
48 static struct mutex
*create_mutex( const char *name
, size_t len
, int owned
)
52 if ((mutex
= create_named_object( &mutex_ops
, name
, len
)))
54 if (get_error() != ERROR_ALREADY_EXISTS
)
56 /* initialize it if it didn't already exist */
60 mutex
->next
= mutex
->prev
= NULL
;
61 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
67 /* release a mutex once the recursion count is 0 */
68 static void do_release( struct mutex
*mutex
, struct thread
*thread
)
70 assert( !mutex
->count
);
71 /* remove the mutex from the thread list of owned mutexes */
72 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
73 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
74 else thread
->mutex
= mutex
->next
;
76 mutex
->next
= mutex
->prev
= NULL
;
77 wake_up( &mutex
->obj
, 0 );
80 void abandon_mutexes( struct thread
*thread
)
84 struct mutex
*mutex
= thread
->mutex
;
85 assert( mutex
->owner
== thread
);
88 do_release( mutex
, thread
);
92 static void mutex_dump( struct object
*obj
, int verbose
)
94 struct mutex
*mutex
= (struct mutex
*)obj
;
95 assert( obj
->ops
== &mutex_ops
);
96 printf( "Mutex count=%u owner=%p name='%s'\n",
97 mutex
->count
, mutex
->owner
, get_object_name( &mutex
->obj
) );
100 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
102 struct mutex
*mutex
= (struct mutex
*)obj
;
103 assert( obj
->ops
== &mutex_ops
);
104 return (!mutex
->count
|| (mutex
->owner
== thread
));
107 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
109 struct mutex
*mutex
= (struct mutex
*)obj
;
110 assert( obj
->ops
== &mutex_ops
);
111 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
113 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
115 assert( !mutex
->owner
);
116 mutex
->owner
= thread
;
118 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
119 thread
->mutex
= mutex
;
121 if (!mutex
->abandoned
) return 0;
122 mutex
->abandoned
= 0;
127 DECL_HANDLER(create_mutex
)
129 size_t len
= get_req_strlen( req
->name
);
133 if ((mutex
= create_mutex( req
->name
, len
, req
->owned
)))
135 req
->handle
= alloc_handle( current
->process
, mutex
, MUTEX_ALL_ACCESS
, req
->inherit
);
136 release_object( mutex
);
140 /* open a handle to a mutex */
141 DECL_HANDLER(open_mutex
)
143 size_t len
= get_req_strlen( req
->name
);
144 req
->handle
= open_object( req
->name
, len
, &mutex_ops
, req
->access
, req
->inherit
);
147 /* release a mutex */
148 DECL_HANDLER(release_mutex
)
152 if ((mutex
= (struct mutex
*)get_handle_obj( current
->process
, req
->handle
,
153 MUTEX_MODIFY_STATE
, &mutex_ops
)))
155 if (!mutex
->count
|| (mutex
->owner
!= current
)) set_error( ERROR_NOT_OWNER
);
156 else if (!--mutex
->count
) do_release( mutex
, current
);
157 release_object( mutex
);