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
=
47 static struct object
*create_mutex( const char *name
, int owned
)
51 if (!(mutex
= (struct mutex
*)create_named_object( name
, &mutex_ops
, sizeof(*mutex
) )))
53 if (GET_ERROR() != ERROR_ALREADY_EXISTS
)
55 /* initialize it if it didn't already exist */
59 mutex
->next
= mutex
->prev
= NULL
;
60 if (owned
) mutex_satisfied( &mutex
->obj
, current
);
65 /* release a mutex once the recursion count is 0 */
66 static void do_release( struct mutex
*mutex
, struct thread
*thread
)
68 assert( !mutex
->count
);
69 /* remove the mutex from the thread list of owned mutexes */
70 if (mutex
->next
) mutex
->next
->prev
= mutex
->prev
;
71 if (mutex
->prev
) mutex
->prev
->next
= mutex
->next
;
72 else thread
->mutex
= mutex
->next
;
74 mutex
->next
= mutex
->prev
= NULL
;
75 wake_up( &mutex
->obj
, 0 );
78 static int release_mutex( int handle
)
82 if (!(mutex
= (struct mutex
*)get_handle_obj( current
->process
, handle
,
83 MUTEX_MODIFY_STATE
, &mutex_ops
)))
85 if (!mutex
->count
|| (mutex
->owner
!= current
))
87 SET_ERROR( ERROR_NOT_OWNER
);
90 if (!--mutex
->count
) do_release( mutex
, current
);
91 release_object( mutex
);
95 void abandon_mutexes( struct thread
*thread
)
99 struct mutex
*mutex
= thread
->mutex
;
100 assert( mutex
->owner
== thread
);
102 mutex
->abandoned
= 1;
103 do_release( mutex
, thread
);
107 static void mutex_dump( struct object
*obj
, int verbose
)
109 struct mutex
*mutex
= (struct mutex
*)obj
;
110 assert( obj
->ops
== &mutex_ops
);
111 printf( "Mutex count=%u owner=%p name='%s'\n",
112 mutex
->count
, mutex
->owner
, get_object_name( &mutex
->obj
) );
115 static int mutex_signaled( struct object
*obj
, struct thread
*thread
)
117 struct mutex
*mutex
= (struct mutex
*)obj
;
118 assert( obj
->ops
== &mutex_ops
);
119 return (!mutex
->count
|| (mutex
->owner
== thread
));
122 static int mutex_satisfied( struct object
*obj
, struct thread
*thread
)
124 struct mutex
*mutex
= (struct mutex
*)obj
;
125 assert( obj
->ops
== &mutex_ops
);
126 assert( !mutex
->count
|| (mutex
->owner
== thread
) );
128 if (!mutex
->count
++) /* FIXME: avoid wrap-around */
130 assert( !mutex
->owner
);
131 mutex
->owner
= thread
;
133 if ((mutex
->next
= thread
->mutex
)) mutex
->next
->prev
= mutex
;
134 thread
->mutex
= mutex
;
136 if (!mutex
->abandoned
) return 0;
137 mutex
->abandoned
= 0;
141 static void mutex_destroy( struct object
*obj
)
143 struct mutex
*mutex
= (struct mutex
*)obj
;
144 assert( obj
->ops
== &mutex_ops
);
149 DECL_HANDLER(create_mutex
)
151 struct create_mutex_reply reply
= { -1 };
153 char *name
= (char *)data
;
154 if (!len
) name
= NULL
;
155 else CHECK_STRING( "create_mutex", name
, len
);
157 obj
= create_mutex( name
, req
->owned
);
160 reply
.handle
= alloc_handle( current
->process
, obj
, MUTEX_ALL_ACCESS
, req
->inherit
);
161 release_object( obj
);
163 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
166 /* open a handle to a mutex */
167 DECL_HANDLER(open_mutex
)
169 struct open_mutex_reply reply
;
170 char *name
= (char *)data
;
171 if (!len
) name
= NULL
;
172 else CHECK_STRING( "open_mutex", name
, len
);
174 reply
.handle
= open_object( name
, &mutex_ops
, req
->access
, req
->inherit
);
175 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
178 /* release a mutex */
179 DECL_HANDLER(release_mutex
)
181 if (release_mutex( req
->handle
)) CLEAR_ERROR();
182 send_reply( current
, -1, 0 );