forked from msm8916-mainline/lk2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.c
208 lines (174 loc) · 5.32 KB
/
mutex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2008-2009 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file
* @brief Mutex functions
*
* @defgroup mutex Mutex
* @{
*/
#include <debug.h>
#include <err.h>
#include <kernel/mutex.h>
#include <kernel/thread.h>
#if DEBUGLEVEL > 1
#define MUTEX_CHECK 1
#endif
/**
* @brief Initialize a mutex_t
*/
void mutex_init(mutex_t *m)
{
#if MUTEX_CHECK
// ASSERT(m->magic != MUTEX_MAGIC);
#endif
m->magic = MUTEX_MAGIC;
m->count = 0;
m->holder = 0;
wait_queue_init(&m->wait);
}
/**
* @brief Destroy a mutex_t
*
* This function frees any resources that were allocated
* in mutex_init(). The mutex_t object itself is not freed.
*/
void mutex_destroy(mutex_t *m)
{
enter_critical_section();
#if MUTEX_CHECK
ASSERT(m->magic == MUTEX_MAGIC);
#endif
// if (m->holder != 0 && current_thread != m->holder)
// panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
// current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
m->magic = 0;
m->count = 0;
wait_queue_destroy(&m->wait, true);
exit_critical_section();
}
/**
* @brief Acquire a mutex; wait if needed.
*
* This function waits for a mutex to become available. It
* may wait forever if the mutex never becomes free.
*
* @return NO_ERROR on success, other values on error
*/
status_t mutex_acquire(mutex_t *m)
{
status_t ret = NO_ERROR;
if (current_thread == m->holder)
panic("mutex_acquire: thread %p (%s) tried to acquire mutex %p it already owns.\n",
current_thread, current_thread->name, m);
enter_critical_section();
#if MUTEX_CHECK
ASSERT(m->magic == MUTEX_MAGIC);
#endif
// dprintf("mutex_acquire: m %p, count %d, curr %p\n", m, m->count, current_thread);
m->count++;
if (unlikely(m->count > 1)) {
/*
* block on the wait queue. If it returns an error, it was likely destroyed
* out from underneath us, so make sure we dont scribble thread ownership
* on the mutex.
*/
ret = wait_queue_block(&m->wait, INFINITE_TIME);
if (ret < 0)
goto err;
}
m->holder = current_thread;
err:
exit_critical_section();
return ret;
}
/**
* @brief Mutex wait with timeout
*
* This function waits up to \a timeout ms for the mutex to become available.
* Timeout may be zero, in which case this function returns immediately if
* the mutex is not free.
*
* @return NO_ERROR on success, ERR_TIMED_OUT on timeout,
* other values on error
*/
status_t mutex_acquire_timeout(mutex_t *m, time_t timeout)
{
status_t ret = NO_ERROR;
if (current_thread == m->holder)
panic("mutex_acquire_timeout: thread %p (%s) tried to acquire mutex %p it already owns.\n",
current_thread, current_thread->name, m);
if (timeout == INFINITE_TIME)
return mutex_acquire(m);
enter_critical_section();
#if MUTEX_CHECK
ASSERT(m->magic == MUTEX_MAGIC);
#endif
// dprintf("mutex_acquire_timeout: m %p, count %d, curr %p, timeout %d\n", m, m->count, current_thread, timeout);
m->count++;
if (unlikely(m->count > 1)) {
ret = wait_queue_block(&m->wait, timeout);
if (ret < NO_ERROR) {
/* if the acquisition timed out, back out the acquire and exit */
if (ret == ERR_TIMED_OUT) {
/*
* XXX race: the mutex may have been destroyed after the timeout,
* but before we got scheduled again which makes messing with the
* count variable dangerous.
*/
m->count--;
goto err;
}
/* if there was a general error, it may have been destroyed out from
* underneath us, so just exit (which is really an invalid state anyway)
*/
}
}
m->holder = current_thread;
err:
exit_critical_section();
return ret;
}
/**
* @brief Release mutex
*/
status_t mutex_release(mutex_t *m)
{
if (current_thread != m->holder)
panic("mutex_release: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
enter_critical_section();
#if MUTEX_CHECK
ASSERT(m->magic == MUTEX_MAGIC);
#endif
// dprintf("mutex_release: m %p, count %d, holder %p, curr %p\n", m, m->count, m->holder, current_thread);
m->holder = 0;
m->count--;
if (unlikely(m->count >= 1)) {
/* release a thread */
// dprintf("releasing thread\n");
wait_queue_wake_one(&m->wait, true, NO_ERROR);
}
exit_critical_section();
return NO_ERROR;
}