1 /*
2  * Copyright (c) 2008-2014 Travis Geiselbrecht
3  * Copyright (c) 2012-2012 Shantanu Gupta
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files
7  * (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * @file
27  * @brief  Mutex functions
28  *
29  * @defgroup mutex Mutex
30  * @{
31  */
32 
33 #include <kernel/mutex.h>
34 #include <debug.h>
35 #include <assert.h>
36 #include <err.h>
37 #include <kernel/thread.h>
38 
39 /**
40  * @brief  Initialize a mutex_t
41  */
mutex_init(mutex_t * m)42 void mutex_init(mutex_t *m)
43 {
44     *m = (mutex_t)MUTEX_INITIAL_VALUE(*m);
45 }
46 
47 /**
48  * @brief  Destroy a mutex_t
49  *
50  * This function frees any resources that were allocated
51  * in mutex_init().  The mutex_t object itself is not freed.
52  */
mutex_destroy(mutex_t * m)53 void mutex_destroy(mutex_t *m)
54 {
55     DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
56 
57 #if LK_DEBUGLEVEL > 0
58     if (unlikely(m->holder != 0 && get_current_thread() != m->holder))
59         panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
60               get_current_thread(), get_current_thread()->name, m, m->holder, m->holder->name);
61 #endif
62 
63     THREAD_LOCK(state);
64     m->magic = 0;
65     m->count = 0;
66     wait_queue_destroy(&m->wait, true);
67     THREAD_UNLOCK(state);
68 }
69 
70 /**
71  * @brief  Mutex wait with timeout
72  *
73  * This function waits up to \a timeout ms for the mutex to become available.
74  * Timeout may be zero, in which case this function returns immediately if
75  * the mutex is not free.
76  *
77  * @return  NO_ERROR on success, ERR_TIMED_OUT on timeout,
78  * other values on error
79  */
mutex_acquire_timeout(mutex_t * m,lk_time_t timeout)80 status_t mutex_acquire_timeout(mutex_t *m, lk_time_t timeout)
81 {
82     DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
83 
84 #if LK_DEBUGLEVEL > 0
85     if (unlikely(get_current_thread() == m->holder))
86         panic("mutex_acquire_timeout: thread %p (%s) tried to acquire mutex %p it already owns.\n",
87               get_current_thread(), get_current_thread()->name, m);
88 #endif
89 
90     THREAD_LOCK(state);
91 
92     status_t ret = NO_ERROR;
93     if (unlikely(++m->count > 1)) {
94         ret = wait_queue_block(&m->wait, timeout);
95         if (unlikely(ret < NO_ERROR)) {
96             /* if the acquisition timed out, back out the acquire and exit */
97             if (likely(ret == ERR_TIMED_OUT)) {
98                 /*
99                  * race: the mutex may have been destroyed after the timeout,
100                  * but before we got scheduled again which makes messing with the
101                  * count variable dangerous.
102                  */
103                 m->count--;
104             }
105             /* if there was a general error, it may have been destroyed out from
106              * underneath us, so just exit (which is really an invalid state anyway)
107              */
108             goto err;
109         }
110     }
111 
112     m->holder = get_current_thread();
113 
114 err:
115     THREAD_UNLOCK(state);
116     return ret;
117 }
118 
119 /**
120  * @brief  Release mutex
121  */
mutex_release(mutex_t * m)122 status_t mutex_release(mutex_t *m)
123 {
124     DEBUG_ASSERT(m->magic == MUTEX_MAGIC);
125 
126 #if LK_DEBUGLEVEL > 0
127     if (unlikely(get_current_thread() != m->holder)) {
128         panic("mutex_release: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
129               get_current_thread(), get_current_thread()->name, m, m->holder, m->holder ? m->holder->name : "none");
130     }
131 #endif
132 
133     THREAD_LOCK(state);
134 
135     m->holder = 0;
136 
137     if (unlikely(--m->count >= 1)) {
138         /* release a thread */
139         wait_queue_wake_one(&m->wait, true, NO_ERROR);
140     }
141 
142     THREAD_UNLOCK(state);
143     return NO_ERROR;
144 }
145 
extern_is_mutex_held(mutex_t * m)146 bool extern_is_mutex_held(mutex_t *m) {
147     return is_mutex_held(m);
148 }
149