2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
19 struct object obj
; /* object header */
20 struct thread
*owner
; /* mutex owner */
21 unsigned int count
; /* recursion count */
22 int abandoned
; /* has it been abandoned? */
27 static void mutex_dump( struct object
*obj
, int verbose
);
28 static int mutex_signaled( struct object
*obj
, struct thread
*thread
);
29 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
);
30 static void mutex_destroy( struct object
*obj
);
32 static const struct object_ops mutex_ops
=
34 sizeof(struct mutex
), /* size */
35 mutex_dump
, /* dump */
36 add_queue
, /* add_queue */
37 remove_queue
, /* remove_queue */
38 mutex_signaled
, /* signaled */
39 mutex_satisfied
, /* satisfied */
40 NULL
, /* get_poll_events */
41 NULL
, /* poll_event */
42 no_read_fd
, /* get_read_fd */
43 no_write_fd
, /* get_write_fd */
45 no_get_file_info
, /* get_file_info */
46 mutex_destroy
/* destroy */
50 static struct mutex
*create_mutex( const WCHAR
*name
, size_t len
, int owned
)
54 if ((mutex
= create_named_object( &mutex_ops
, name
, len
)))
56 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
58 /* initialize it if it didn't already exist */
62 mutex
->next
= mutex
->prev
= NULL
;
63 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
69 /* release a mutex once the recursion count is 0 */
70 static void do_release( struct mutex
*mutex
)
72 assert( !mutex
->count
);
73 /* remove the mutex from the thread list of owned mutexes */
74 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
75 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
76 else mutex
->owner
->mutex
= mutex
->next
;
78 mutex
->next
= mutex
->prev
= NULL
;
79 wake_up( &mutex
->obj
, 0 );
82 void abandon_mutexes( struct thread
*thread
)
86 struct mutex
*mutex
= thread
->mutex
;
87 assert( mutex
->owner
== thread
);
94 static void mutex_dump( struct object
*obj
, int verbose
)
96 struct mutex
*mutex
= (struct mutex
*)obj
;
97 assert( obj
->ops
== &mutex_ops
);
98 fprintf( stderr
, "Mutex count=%u owner=%p ", mutex
->count
, mutex
->owner
);
99 dump_object_name( &mutex
->obj
);
100 fputc( '\n', stderr
);
103 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
105 struct mutex
*mutex
= (struct mutex
*)obj
;
106 assert( obj
->ops
== &mutex_ops
);
107 return (!mutex
->count
|| (mutex
->owner
== thread
));
110 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
112 struct mutex
*mutex
= (struct mutex
*)obj
;
113 assert( obj
->ops
== &mutex_ops
);
114 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
116 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
118 assert( !mutex
->owner
);
119 mutex
->owner
= thread
;
121 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
122 thread
->mutex
= mutex
;
124 if (!mutex
->abandoned
) return 0;
125 mutex
->abandoned
= 0;
129 static void mutex_destroy( struct object
*obj
)
131 struct mutex
*mutex
= (struct mutex
*)obj
;
132 assert( obj
->ops
== &mutex_ops
);
134 if (!mutex
->count
) return;
140 DECL_HANDLER(create_mutex
)
142 size_t len
= get_req_strlenW( req
, req
->name
);
146 if ((mutex
= create_mutex( req
->name
, len
, req
->owned
)))
148 req
->handle
= alloc_handle( current
->process
, mutex
, MUTEX_ALL_ACCESS
, req
->inherit
);
149 release_object( mutex
);
153 /* open a handle to a mutex */
154 DECL_HANDLER(open_mutex
)
156 size_t len
= get_req_strlenW( req
, req
->name
);
157 req
->handle
= open_object( req
->name
, len
, &mutex_ops
, req
->access
, req
->inherit
);
160 /* release a mutex */
161 DECL_HANDLER(release_mutex
)
165 if ((mutex
= (struct mutex
*)get_handle_obj( current
->process
, req
->handle
,
166 MUTEX_MODIFY_STATE
, &mutex_ops
)))
168 if (!mutex
->count
|| (mutex
->owner
!= current
)) set_error( STATUS_MUTANT_NOT_OWNED
);
169 else if (!--mutex
->count
) do_release( mutex
);
170 release_object( mutex
);