1 /* Simple mutex locks for pthrlib.
2 * by Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: pthr_mutex.h,v 1.4 2002/12/01 14:29:28 rich Exp $
26 #include "pthr_pseudothread.h"
27 #include "pthr_wait_queue.h"
30 typedef struct mutex *mutex;
32 /* Function: new_mutex - mutual exclusion (mutex) locks
33 * Function: mutex_enter
34 * Function: mutex_leave
35 * Function: mutex_try_enter
36 * Function: mutex_nr_sleepers
38 * Mutex locks are simple: at most one pseudothread may enter the
39 * critical area protected by the lock at once. If instead you
40 * wish multiple reader / single writer semantics, then please
41 * see @ref{new_rwlock(3)}.
43 * Mutex locks are automatically released if they are being held when
46 * Note that there are possible deadlocks when using locks. To
47 * avoid deadlocks, always ensure every thread acquires locks
50 * @code{new_mutex} creates a new mutex object.
52 * @code{mutex_enter} and @code{mutex_leave} enter and leave
53 * the critical section. Only one thread can run at a time
54 * inside the critical section.
56 * @code{mutex_try_enter} is identical to @code{mutex_enter}
57 * except that it does not block if the lock is held by another
58 * thread. The function returns true if the lock was successfully
59 * acquired, or false if another thread is currently holding it.
61 * @code{mutex_nr_sleepers} returns the number of threads which
62 * are queued up waiting to enter the critical section.
64 * Bugs: A common mistake is to accidentally call @code{mutex_leave}
65 * when you are not holding the lock. This generally causes the
68 extern mutex new_mutex (pool);
69 extern void mutex_enter (mutex);
70 extern void mutex_leave (mutex);
71 extern int mutex_try_enter (mutex);
72 extern int mutex_nr_sleepers (mutex);
74 #endif /* PTHR_MUTEX_H */