source: mainline/kernel/generic/include/synch/condvar.h

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

Extend kernel condvar_t to work with spinlocks

MUTEX_ACTIVE is a poor copy of the spinlock, made solely to allow
using condvars with it. Solve that issue the other way around,
by making condvar_wait() generic, so that we can get rid of that
hack.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2025 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/** @file
34 */
35
36#ifndef KERN_CONDVAR_H_
37#define KERN_CONDVAR_H_
38
39#include <stdint.h>
40#include <synch/waitq.h>
41#include <synch/mutex.h>
42#include <synch/spinlock.h>
43#include <abi/synch.h>
44
45typedef struct {
46 waitq_t wq;
47} condvar_t;
48
49#define CONDVAR_INITIALIZER(name) (condvar_t) { \
50 .wq = WAITQ_INITIALIZER((name).wq), \
51}
52
53#define CONDVAR_INITIALIZE(name) \
54 condvar_t name = CONDVAR_INITIALIZER(name)
55
56extern void condvar_initialize(condvar_t *cv);
57extern void condvar_signal(condvar_t *cv);
58extern void condvar_broadcast(condvar_t *cv);
59
60extern errno_t __condvar_wait_mutex(condvar_t *cv, mutex_t *mtx);
61extern errno_t __condvar_wait_spinlock(condvar_t *cv, spinlock_t *mtx);
62extern errno_t __condvar_wait_irq_spinlock(condvar_t *cv, irq_spinlock_t *mtx);
63extern errno_t __condvar_wait_timeout_mutex(condvar_t *cv, mutex_t *mtx, uint32_t usec);
64extern errno_t __condvar_wait_timeout_spinlock(condvar_t *cv, spinlock_t *mtx, uint32_t usec);
65extern errno_t __condvar_wait_timeout_irq_spinlock(condvar_t *cv, irq_spinlock_t *mtx, uint32_t usec);
66
67#define condvar_wait(cv, mtx) (_Generic((mtx), \
68 mutex_t *: __condvar_wait_mutex, \
69 spinlock_t *: __condvar_wait_spinlock, \
70 irq_spinlock_t *: __condvar_wait_irq_spinlock \
71)(cv, mtx))
72
73#define condvar_wait_timeout(cv, mtx, usec) (_Generic((mtx), \
74 mutex_t *: __condvar_wait_timeout_mutex, \
75 spinlock_t *: __condvar_wait_timeout_spinlock, \
76 irq_spinlock_t *: __condvar_wait_timeout_irq_spinlock \
77)(cv, mtx))
78
79#endif
80
81/** @}
82 */
Note: See TracBrowser for help on using the repository browser.