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
);
31 static void mutex_destroy( struct object
*obj
);
33 static const struct object_ops mutex_ops
=
35 sizeof(struct mutex
), /* size */
36 mutex_dump
, /* dump */
37 add_queue
, /* add_queue */
38 remove_queue
, /* remove_queue */
39 mutex_signaled
, /* signaled */
40 mutex_satisfied
, /* satisfied */
41 NULL
, /* get_poll_events */
42 NULL
, /* poll_event */
43 no_read_fd
, /* get_read_fd */
44 no_write_fd
, /* get_write_fd */
46 no_get_file_info
, /* get_file_info */
47 mutex_destroy
/* destroy */
51 static struct mutex
*create_mutex( const WCHAR
*name
, size_t len
, int owned
)
55 if ((mutex
= create_named_object( &mutex_ops
, name
, len
)))
57 if (get_error() != ERROR_ALREADY_EXISTS
)
59 /* initialize it if it didn't already exist */
63 mutex
->next
= mutex
->prev
= NULL
;
64 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
70 /* release a mutex once the recursion count is 0 */
71 static void do_release( struct mutex
*mutex
)
73 assert( !mutex
->count
);
74 /* remove the mutex from the thread list of owned mutexes */
75 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
76 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
77 else mutex
->owner
->mutex
= mutex
->next
;
79 mutex
->next
= mutex
->prev
= NULL
;
80 wake_up( &mutex
->obj
, 0 );
83 void abandon_mutexes( struct thread
*thread
)
87 struct mutex
*mutex
= thread
->mutex
;
88 assert( mutex
->owner
== thread
);
95 static void mutex_dump( struct object
*obj
, int verbose
)
97 struct mutex
*mutex
= (struct mutex
*)obj
;
98 assert( obj
->ops
== &mutex_ops
);
99 fprintf( stderr
, "Mutex count=%u owner=%p ", mutex
->count
, mutex
->owner
);
100 dump_object_name( &mutex
->obj
);
101 fputc( '\n', stderr
);
104 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
106 struct mutex
*mutex
= (struct mutex
*)obj
;
107 assert( obj
->ops
== &mutex_ops
);
108 return (!mutex
->count
|| (mutex
->owner
== thread
));
111 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
113 struct mutex
*mutex
= (struct mutex
*)obj
;
114 assert( obj
->ops
== &mutex_ops
);
115 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
117 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
119 assert( !mutex
->owner
);
120 mutex
->owner
= thread
;
122 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
123 thread
->mutex
= mutex
;
125 if (!mutex
->abandoned
) return 0;
126 mutex
->abandoned
= 0;
130 static void mutex_destroy( struct object
*obj
)
132 struct mutex
*mutex
= (struct mutex
*)obj
;
133 assert( obj
->ops
== &mutex_ops
);
135 if (!mutex
->count
) return;
141 DECL_HANDLER(create_mutex
)
143 size_t len
= get_req_strlenW( req
->name
);
147 if ((mutex
= create_mutex( req
->name
, len
, req
->owned
)))
149 req
->handle
= alloc_handle( current
->process
, mutex
, MUTEX_ALL_ACCESS
, req
->inherit
);
150 release_object( mutex
);
154 /* open a handle to a mutex */
155 DECL_HANDLER(open_mutex
)
157 size_t len
= get_req_strlenW( req
->name
);
158 req
->handle
= open_object( req
->name
, len
, &mutex_ops
, req
->access
, req
->inherit
);
161 /* release a mutex */
162 DECL_HANDLER(release_mutex
)
166 if ((mutex
= (struct mutex
*)get_handle_obj( current
->process
, req
->handle
,
167 MUTEX_MODIFY_STATE
, &mutex_ops
)))
169 if (!mutex
->count
|| (mutex
->owner
!= current
)) set_error( ERROR_NOT_OWNER
);
170 else if (!--mutex
->count
) do_release( mutex
);
171 release_object( mutex
);