source: mainline/kernel/generic/src/synch/mutex.c@ a188131

Last change on this file since a188131 was 597fa24, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 months ago

Enable static initialization of kernel synchronization primitives

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2023 Jiří Zárevúcky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup kernel_sync
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Mutexes.
37 */
38
39#include <assert.h>
40#include <errno.h>
41#include <synch/mutex.h>
42#include <synch/semaphore.h>
43#include <arch.h>
44#include <stacktrace.h>
45#include <cpu.h>
46#include <proc/thread.h>
47
48/** Initialize mutex.
49 *
50 * @param mtx Mutex.
51 * @param type Type of the mutex.
52 */
53void mutex_initialize(mutex_t *mtx, mutex_type_t type)
54{
55 *mtx = MUTEX_INITIALIZER(*mtx, type);
56}
57
58/** Find out whether the mutex is currently locked.
59 *
60 * @param mtx Mutex.
61 *
62 * @return True if the mutex is locked, false otherwise.
63 */
64bool mutex_locked(mutex_t *mtx)
65{
66 errno_t rc = semaphore_trydown(&mtx->sem);
67 if (rc == EOK) {
68 semaphore_up(&mtx->sem);
69 }
70 return rc != EOK;
71}
72
73static void mutex_lock_active(mutex_t *mtx)
74{
75 assert((mtx->type == MUTEX_ACTIVE) || !THREAD);
76
77 const unsigned deadlock_treshold = 100000000;
78 unsigned int cnt = 0;
79 bool deadlock_reported = false;
80
81 while (semaphore_trydown(&mtx->sem) != EOK) {
82 if (cnt++ > deadlock_treshold) {
83 printf("cpu%u: looping on active mutex %p\n", CPU->id, mtx);
84 stack_trace();
85 cnt = 0;
86 deadlock_reported = true;
87 }
88 }
89
90 if (deadlock_reported)
91 printf("cpu%u: not deadlocked\n", CPU->id);
92}
93
94/** Acquire mutex.
95 *
96 * This operation is uninterruptible and cannot fail.
97 */
98void mutex_lock(mutex_t *mtx)
99{
100 if (mtx->type == MUTEX_RECURSIVE && mtx->owner == THREAD) {
101 assert(THREAD);
102 mtx->nesting++;
103 return;
104 }
105
106 if (mtx->type == MUTEX_ACTIVE || !THREAD) {
107 mutex_lock_active(mtx);
108 return;
109 }
110
111 semaphore_down(&mtx->sem);
112 mtx->owner = THREAD;
113 mtx->nesting = 1;
114}
115
116/** Acquire mutex with timeout.
117 *
118 * @param mtx Mutex.
119 * @param usec Timeout in microseconds.
120 *
121 * @return EOK if lock was successfully acquired, something else otherwise.
122 */
123errno_t mutex_lock_timeout(mutex_t *mtx, uint32_t usec)
124{
125 if (usec != 0) {
126 assert(mtx->type != MUTEX_ACTIVE);
127 assert(THREAD);
128 }
129
130 if (mtx->type == MUTEX_RECURSIVE && mtx->owner == THREAD) {
131 assert(THREAD);
132 mtx->nesting++;
133 return EOK;
134 }
135
136 errno_t rc = semaphore_down_timeout(&mtx->sem, usec);
137 if (rc == EOK) {
138 mtx->owner = THREAD;
139 mtx->nesting = 1;
140 }
141 return rc;
142}
143
144/** Attempt to acquire mutex without blocking.
145 *
146 * @return EOK if lock was successfully acquired, something else otherwise.
147 */
148errno_t mutex_trylock(mutex_t *mtx)
149{
150 return mutex_lock_timeout(mtx, 0);
151}
152
153/** Release mutex.
154 *
155 * @param mtx Mutex.
156 */
157void mutex_unlock(mutex_t *mtx)
158{
159 if (mtx->type == MUTEX_RECURSIVE) {
160 assert(mtx->owner == THREAD);
161 if (--mtx->nesting > 0)
162 return;
163 mtx->owner = NULL;
164 }
165 semaphore_up(&mtx->sem);
166}
167
168/** @}
169 */
Note: See TracBrowser for help on using the repository browser.