You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB
C

/* See the end of this file for copyright and license terms. */
#pragma once
#include <gay/cdefs.h>
#include <gay/irq.h>
#include <gay/sched.h>
#include <gay/types.h>
/**
* @brief Print an error message and halt the system immediately.
*
* @param fmt printf style format string
*/
void panic(const char *fmt, ...) __noreturn __printflike(1, 2);
#ifdef DEBUG
/**
* @brief Assert that statement `x` is true.
* If it is not and debugging is enabled, the kernel panics.
*/
#define KASSERT(x) do { \
if ( __predict_false(!(x)) ) { \
panic("Assertion \"%s\" failed!\n" \
" in function %s()\n" \
" in file %s, line %u\n", \
#x, __func__, __FILENAME__, __LINE__); \
} \
} while (0)
#else
#define KASSERT(x) do { } while (0)
#endif
/**
* @brief Enter a critical section, meaning preemption gets disabled.
* Critical sections should be kept as short as possible for performance.
* Cannot be called from irq context because irqs always run atomically.
*
* @see `critical_leave()`
*/
static inline void critical_enter(void)
{
KASSERT(!in_irq());
struct task *tsk = current;
tsk->critnest++;
}
/**
* @brief Leave a critical section.
* Cannot be called from irq context.
*
* @see `critical_enter()`
*/
static inline void critical_leave(void)
{
KASSERT(!in_irq());
struct task *tsk = current;
tsk->critnest--;
KASSERT(tsk->critnest >= 0);
}
static inline bool in_critical(void)
{
if (in_irq())
return true;
return current->critnest > 0;
}
/*
* This file is part of GayBSD.
* Copyright (c) 2021 fef <owo@fef.moe>.
*
* GayBSD is nonviolent software: you may only use, redistribute, and/or
* modify it under the terms of the Cooperative Nonviolent Public License
* (CNPL) as found in the LICENSE file in the source code root directory
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
* of the license, or (at your option) any later version.
*
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPL for details.
*/