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
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[76e17d7c]3 * Copyright (c) 2023 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>
[f761f1eb]41#include <synch/mutex.h>
42#include <synch/semaphore.h>
[a3900cc]43#include <arch.h>
[311929ec]44#include <stacktrace.h>
[1066041]45#include <cpu.h>
46#include <proc/thread.h>
[f761f1eb]47
[08a19ba]48/** Initialize mutex.
[63975c6]49 *
[15d9fe6]50 * @param mtx Mutex.
51 * @param type Type of the mutex.
[63975c6]52 */
[08a19ba]53void mutex_initialize(mutex_t *mtx, mutex_type_t type)
[f761f1eb]54{
[597fa24]55 *mtx = MUTEX_INITIALIZER(*mtx, type);
[f761f1eb]56}
57
[d7da4284]58/** Find out whether the mutex is currently locked.
59 *
[15d9fe6]60 * @param mtx Mutex.
61 *
62 * @return True if the mutex is locked, false otherwise.
[d7da4284]63 */
64bool mutex_locked(mutex_t *mtx)
65{
[e994898]66 errno_t rc = semaphore_trydown(&mtx->sem);
67 if (rc == EOK) {
[4c78104]68 semaphore_up(&mtx->sem);
69 }
[e994898]70 return rc != EOK;
[d7da4284]71}
72
[76e17d7c]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}
[fce7b43]93
[08a19ba]94/** Acquire mutex.
[63975c6]95 *
[76e17d7c]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.
[63975c6]117 *
[15d9fe6]118 * @param mtx Mutex.
119 * @param usec Timeout in microseconds.
[2e4e706]120 *
[76e17d7c]121 * @return EOK if lock was successfully acquired, something else otherwise.
[63975c6]122 */
[76e17d7c]123errno_t mutex_lock_timeout(mutex_t *mtx, uint32_t usec)
[f761f1eb]124{
[76e17d7c]125 if (usec != 0) {
126 assert(mtx->type != MUTEX_ACTIVE);
[15d9fe6]127 assert(THREAD);
[76e17d7c]128 }
[15d9fe6]129
[76e17d7c]130 if (mtx->type == MUTEX_RECURSIVE && mtx->owner == THREAD) {
131 assert(THREAD);
132 mtx->nesting++;
133 return EOK;
[08a19ba]134 }
135
[76e17d7c]136 errno_t rc = semaphore_down_timeout(&mtx->sem, usec);
137 if (rc == EOK) {
138 mtx->owner = THREAD;
139 mtx->nesting = 1;
140 }
[08a19ba]141 return rc;
[f761f1eb]142}
143
[76e17d7c]144/** Attempt to acquire mutex without blocking.
145 *
146 * @return EOK if lock was successfully acquired, something else otherwise.
147 */
[5110d0a]148errno_t mutex_trylock(mutex_t *mtx)
149{
[76e17d7c]150 return mutex_lock_timeout(mtx, 0);
[5110d0a]151}
152
[08a19ba]153/** Release mutex.
[63975c6]154 *
[15d9fe6]155 * @param mtx Mutex.
[63975c6]156 */
[f761f1eb]157void mutex_unlock(mutex_t *mtx)
158{
[15d9fe6]159 if (mtx->type == MUTEX_RECURSIVE) {
160 assert(mtx->owner == THREAD);
161 if (--mtx->nesting > 0)
162 return;
163 mtx->owner = NULL;
164 }
[f761f1eb]165 semaphore_up(&mtx->sem);
166}
[116d1ef4]167
[cc73a8a1]168/** @}
[b45c443]169 */
Note: See TracBrowser for help on using the repository browser.