2 * Copyright (c) 2014, Linaro Limited
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 #ifndef KERNEL_MUTEX_H
28 #define KERNEL_MUTEX_H
30 #include <types_ext.h>
31 #include <sys/queue.h>
32 #include <kernel/wait_queue.h>
40 * Positive owner ids signifies actual threads, negative ids has special
41 * meanings according to the defines below. Note that only the first of the
42 * defines is allowed in struct mutex::owener_id.
44 #define MUTEX_OWNER_ID_NONE -1
45 #define MUTEX_OWNER_ID_CONDVAR_SLEEP -2
46 #define MUTEX_OWNER_ID_MUTEX_UNLOCK -3
49 enum mutex_value value;
50 unsigned spin_lock; /* used when operating on this struct */
53 TAILQ_ENTRY(mutex) link;
55 #define MUTEX_INITIALIZER \
56 { .value = MUTEX_VALUE_UNLOCKED, .owner_id = MUTEX_OWNER_ID_NONE, \
57 .wq = WAIT_QUEUE_INITIALIZER, }
59 TAILQ_HEAD(mutex_head, mutex);
61 void mutex_init(struct mutex *m);
62 void mutex_destroy(struct mutex *m);
64 #ifdef CFG_MUTEX_DEBUG
65 void mutex_unlock_debug(struct mutex *m, const char *fname, int lineno);
66 #define mutex_unlock(m) mutex_unlock_debug((m), __FILE__, __LINE__)
68 void mutex_lock_debug(struct mutex *m, const char *fname, int lineno);
69 #define mutex_lock(m) mutex_lock_debug((m), __FILE__, __LINE__)
71 bool mutex_trylock_debug(struct mutex *m, const char *fname, int lineno);
72 #define mutex_trylock(m) mutex_trylock_debug((m), __FILE__, __LINE__)
75 void mutex_unlock(struct mutex *m);
76 void mutex_lock(struct mutex *m);
77 bool mutex_trylock(struct mutex *m);
85 #define CONDVAR_INITIALIZER { .m = NULL }
87 void condvar_init(struct condvar *cv);
88 void condvar_destroy(struct condvar *cv);
90 #ifdef CFG_MUTEX_DEBUG
91 void condvar_signal_debug(struct condvar *cv, const char *fname, int lineno);
92 #define condvar_signal(cv) condvar_signal_debug((cv), __FILE__, __LINE__)
94 void condvar_broadcast_debug(struct condvar *cv, const char *fname, int lineno);
95 #define condvar_broadcast(cv) condvar_broadcast_debug((cv), __FILE__, __LINE__)
97 void condvar_wait_debug(struct condvar *cv, struct mutex *m,
98 const char *fname, int lineno);
99 #define condvar_wait(cv, m) condvar_wait_debug((cv), (m), __FILE__, __LINE__)
101 void condvar_signal(struct condvar *cv);
102 void condvar_broadcast(struct condvar *cv);
103 void condvar_wait(struct condvar *cv, struct mutex *m);
106 #endif /*KERNEL_MUTEX_H*/