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

Last change on this file was 9f2f5ee, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Rewrite kernel mutex implementation a little

Removes MUTEX_ACTIVE, the use of which has been removed in favor of
irq_spinlock_t, and fixes some issues with the old implementation.

  • A race in mtx→owner access is unavoidable, so make it explicitly atomic.
  • The THREAD==NULL case happens when there are no other threads yet, so we factor it out as a special case. Also ensures recursive mutex works before threads are initialized, just as normal mutex does.
  • More and better asserts.
  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[9f2f5ee]3 * Copyright (c) 2025 Jiří Zárevúcky
[f761f1eb]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
[e88eb48]30/** @addtogroup kernel_sync
[b45c443]31 * @{
32 */
33
[cf26ba9]34/**
[b45c443]35 * @file
[2e4e706]36 * @brief Mutexes.
[cf26ba9]37 */
[2e4e706]38
[63e27ef]39#include <assert.h>
[897fd8f1]40#include <errno.h>
[9f2f5ee]41#include <proc/thread.h>
42#include <stdatomic.h>
[f761f1eb]43#include <synch/mutex.h>
44#include <synch/semaphore.h>
45
[08a19ba]46/** Initialize mutex.
[63975c6]47 *
[15d9fe6]48 * @param mtx Mutex.
49 * @param type Type of the mutex.
[63975c6]50 */
[08a19ba]51void mutex_initialize(mutex_t *mtx, mutex_type_t type)
[f761f1eb]52{
[597fa24]53 *mtx = MUTEX_INITIALIZER(*mtx, type);
[f761f1eb]54}
55
[9f2f5ee]56/** A race in mtx->owner access is unavoidable, so we have to make
57 * access to it formally atomic. These are convenience functions to
58 * read/write the variable without memory barriers, since we don't need
59 * them and C11 atomics default to the strongest possible memory ordering
60 * by default, which is utterly ridiculous.
61 */
62static inline thread_t *_get_owner(mutex_t *mtx)
63{
64 return atomic_load_explicit(&mtx->owner, memory_order_relaxed);
65}
66
67/** Counterpart to _get_owner(). */
68static inline void _set_owner(mutex_t *mtx, thread_t *owner)
69{
70 atomic_store_explicit(&mtx->owner, owner, memory_order_relaxed);
71}
72
[d7da4284]73/** Find out whether the mutex is currently locked.
74 *
[15d9fe6]75 * @param mtx Mutex.
76 *
77 * @return True if the mutex is locked, false otherwise.
[d7da4284]78 */
79bool mutex_locked(mutex_t *mtx)
80{
[9f2f5ee]81 if (!THREAD)
82 return mtx->nesting > 0;
[76e17d7c]83
[9f2f5ee]84 return _get_owner(mtx) == THREAD;
[76e17d7c]85}
[fce7b43]86
[08a19ba]87/** Acquire mutex.
[63975c6]88 *
[76e17d7c]89 * This operation is uninterruptible and cannot fail.
90 */
91void mutex_lock(mutex_t *mtx)
92{
[9f2f5ee]93 if (!THREAD) {
94 assert(mtx->type == MUTEX_RECURSIVE || mtx->nesting == 0);
[76e17d7c]95 mtx->nesting++;
96 return;
97 }
98
[9f2f5ee]99 if (_get_owner(mtx) == THREAD) {
100 /* This will also detect nested locks on a non-recursive mutex. */
101 assert(mtx->type == MUTEX_RECURSIVE);
102 assert(mtx->nesting > 0);
103 mtx->nesting++;
[76e17d7c]104 return;
105 }
106
107 semaphore_down(&mtx->sem);
[9f2f5ee]108
109 _set_owner(mtx, THREAD);
110 assert(mtx->nesting == 0);
[76e17d7c]111 mtx->nesting = 1;
112}
113
114/** Acquire mutex with timeout.
[63975c6]115 *
[15d9fe6]116 * @param mtx Mutex.
117 * @param usec Timeout in microseconds.
[2e4e706]118 *
[76e17d7c]119 * @return EOK if lock was successfully acquired, something else otherwise.
[63975c6]120 */
[76e17d7c]121errno_t mutex_lock_timeout(mutex_t *mtx, uint32_t usec)
[f761f1eb]122{
[9f2f5ee]123 if (!THREAD) {
124 assert(mtx->type == MUTEX_RECURSIVE || mtx->nesting == 0);
125 mtx->nesting++;
126 return EOK;
[76e17d7c]127 }
[15d9fe6]128
[9f2f5ee]129 if (_get_owner(mtx) == THREAD) {
130 assert(mtx->type == MUTEX_RECURSIVE);
131 assert(mtx->nesting > 0);
[76e17d7c]132 mtx->nesting++;
133 return EOK;
[08a19ba]134 }
135
[76e17d7c]136 errno_t rc = semaphore_down_timeout(&mtx->sem, usec);
[9f2f5ee]137 if (rc != EOK)
138 return rc;
139
140 _set_owner(mtx, THREAD);
141 assert(mtx->nesting == 0);
142 mtx->nesting = 1;
143 return EOK;
[f761f1eb]144}
145
[76e17d7c]146/** Attempt to acquire mutex without blocking.
147 *
148 * @return EOK if lock was successfully acquired, something else otherwise.
149 */
[5110d0a]150errno_t mutex_trylock(mutex_t *mtx)
151{
[76e17d7c]152 return mutex_lock_timeout(mtx, 0);
[5110d0a]153}
154
[08a19ba]155/** Release mutex.
[63975c6]156 *
[15d9fe6]157 * @param mtx Mutex.
[63975c6]158 */
[f761f1eb]159void mutex_unlock(mutex_t *mtx)
160{
[9f2f5ee]161 if (--mtx->nesting > 0) {
162 assert(mtx->type == MUTEX_RECURSIVE);
163 return;
[15d9fe6]164 }
[9f2f5ee]165
166 assert(mtx->nesting == 0);
167
168 if (!THREAD)
169 return;
170
171 assert(_get_owner(mtx) == THREAD);
172 _set_owner(mtx, NULL);
173
[f761f1eb]174 semaphore_up(&mtx->sem);
175}
[116d1ef4]176
[cc73a8a1]177/** @}
[b45c443]178 */
Note: See TracBrowser for help on using the repository browser.