panic: move to separate file, add KASSERT macro
parent
24c6e9504d
commit
b6385aea0a
@ -0,0 +1,64 @@
|
||||
/* See the end of this file for copyright and license terms. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gay/cdefs.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
|
||||
|
||||
static inline void critical_enter(void)
|
||||
{
|
||||
struct task *tsk = current;
|
||||
tsk->critnest++;
|
||||
}
|
||||
|
||||
static inline void critical_leave(void)
|
||||
{
|
||||
struct task *tsk = current;
|
||||
tsk->critnest--;
|
||||
|
||||
KASSERT(tsk->critnest >= 0);
|
||||
}
|
||||
|
||||
static inline bool in_critical(void)
|
||||
{
|
||||
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.
|
||||
*/
|
Loading…
Reference in New Issue