Add to git.
[pthrlib.git] / src / pthr_mutex.h
1 /* Simple mutex locks for pthrlib.
2  * by Richard W.M. Jones <rich@annexia.org>
3  *
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.
8  *
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.
13  *
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.
17  *
18  * $Id: pthr_mutex.h,v 1.4 2002/12/01 14:29:28 rich Exp $
19  */
20
21 #ifndef PTHR_MUTEX_H
22 #define PTHR_MUTEX_H
23
24 #include <pool.h>
25
26 #include "pthr_pseudothread.h"
27 #include "pthr_wait_queue.h"
28
29 struct mutex;
30 typedef struct mutex *mutex;
31
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
37  *
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)}.
42  *
43  * Mutex locks are automatically released if they are being held when
44  * the thread exits.
45  *
46  * Note that there are possible deadlocks when using locks. To
47  * avoid deadlocks, always ensure every thread acquires locks
48  * in the same order.
49  *
50  * @code{new_mutex} creates a new mutex object.
51  *
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.
55  *
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.
60  *
61  * @code{mutex_nr_sleepers} returns the number of threads which
62  * are queued up waiting to enter the critical section.
63  *
64  * Bugs: A common mistake is to accidentally call @code{mutex_leave}
65  * when you are not holding the lock. This generally causes the
66  * library to crash.
67  */
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);
73
74 #endif /* PTHR_MUTEX_H */