1 /*
2 * Copyright (c) 2008-2014 Travis Geiselbrecht
3 * Copyright (c) 2012-2012 Shantanu Gupta
4 *
5 * Use of this source code is governed by a MIT-style
6 * license that can be found in the LICENSE file or at
7 * https://opensource.org/licenses/MIT
8 */
9
10 /**
11 * @file
12 * @brief Mutex functions
13 *
14 * @defgroup mutex Mutex
15 * @{
16 */
17
18 #include <kernel/mutex.h>
19
20 #include <assert.h>
21 #include <kernel/thread.h>
22 #include <lk/debug.h>
23 #include <lk/err.h>
24 #include <lk/init.h>
25
26 static bool mutex_threading_ready;
27
28 /* mutex_threading_ready is currently only used from a DEBUG_ASSERT */
29 #if LK_DEBUGLEVEL > 1
30
mutex_threading_ready_init_func(uint level)31 static void mutex_threading_ready_init_func(uint level) {
32 mutex_threading_ready = true;
33 }
34
35 LK_INIT_HOOK(mutex_threading_ready, mutex_threading_ready_init_func, LK_INIT_LEVEL_THREADING);
36 #endif
37
38 /**
39 * @brief Initialize a mutex_t
40 */
mutex_init(mutex_t * m)41 void mutex_init(mutex_t *m) {
42 *m = (mutex_t)MUTEX_INITIAL_VALUE(*m);
43 }
44
45 /**
46 * @brief Destroy a mutex_t
47 *
48 * This function frees any resources that were allocated
49 * in mutex_init(). The mutex_t object itself is not freed.
50 */
mutex_destroy(mutex_t * m)51 void mutex_destroy(mutex_t *m) {
52 DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
53
54 #if LK_DEBUGLEVEL > 0
55 if (unlikely(m->holder != 0 && get_current_thread() != m->holder))
56 panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
57 get_current_thread(), get_current_thread()->name, m, m->holder, m->holder->name);
58 #endif
59
60 THREAD_LOCK(state);
61 m->magic = 0;
62 m->count = 0;
63 wait_queue_destroy(&m->wait, true);
64 THREAD_UNLOCK(state);
65 }
66
67 /**
68 * @brief Mutex wait with timeout
69 *
70 * This function waits up to \a timeout ms for the mutex to become available.
71 * Timeout may be zero, in which case this function returns immediately if
72 * the mutex is not free.
73 *
74 * @return NO_ERROR on success, ERR_TIMED_OUT on timeout,
75 * other values on error
76 */
mutex_acquire_timeout(mutex_t * m,lk_time_t timeout)77 status_t mutex_acquire_timeout(mutex_t *m, lk_time_t timeout) {
78 DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
79
80 #if LK_DEBUGLEVEL > 0
81 if (unlikely(get_current_thread() == m->holder))
82 panic("mutex_acquire_timeout: thread %p (%s) tried to acquire mutex %p it already owns.\n",
83 get_current_thread(), get_current_thread()->name, m);
84 #endif
85 DEBUG_ASSERT(!mutex_threading_ready || !timeout || !arch_ints_disabled());
86
87 THREAD_LOCK(state);
88
89 status_t ret = NO_ERROR;
90 if (unlikely(++m->count > 1)) {
91 ret = wait_queue_block(&m->wait, timeout);
92 if (unlikely(ret < NO_ERROR)) {
93 /* if the acquisition timed out, back out the acquire and exit */
94 if (likely(ret == ERR_TIMED_OUT)) {
95 /*
96 * race: the mutex may have been destroyed after the timeout,
97 * but before we got scheduled again which makes messing with the
98 * count variable dangerous.
99 */
100 m->count--;
101 }
102 /* if there was a general error, it may have been destroyed out from
103 * underneath us, so just exit (which is really an invalid state anyway)
104 */
105 goto err;
106 }
107 }
108
109 m->holder = get_current_thread();
110
111 err:
112 THREAD_UNLOCK(state);
113 return ret;
114 }
115
116 /**
117 * @brief Release mutex
118 */
mutex_release(mutex_t * m)119 status_t mutex_release(mutex_t *m) {
120 DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
121
122 #if LK_DEBUGLEVEL > 0
123 if (unlikely(get_current_thread() != m->holder)) {
124 panic("mutex_release: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
125 get_current_thread(), get_current_thread()->name, m, m->holder, m->holder ? m->holder->name : "none");
126 }
127 #endif
128
129 THREAD_LOCK(state);
130
131 m->holder = 0;
132
133 if (unlikely(--m->count >= 1)) {
134 /* release a thread */
135 wait_queue_wake_one(&m->wait, true, NO_ERROR);
136 }
137
138 THREAD_UNLOCK(state);
139 return NO_ERROR;
140 }
141
142