aboutsummaryrefslogtreecommitdiffstats
path: root/init/log.c
diff options
context:
space:
mode:
authorturret <turret@duck.com>2023-12-18 18:40:19 -0600
committerturret <turret@duck.com>2023-12-18 18:40:19 -0600
commit7648b5a7b22ae3b3e3a3fa39fd83bce06554385d (patch)
treed553afe5122333663c32645d660a54fdfa872716 /init/log.c
parent54b308af97b91ceabb0a482f325d3a8595b01aa3 (diff)
downloaddiscord-bot-skeleton-7648b5a7b22ae3b3e3a3fa39fd83bce06554385d.tar.gz
discord-bot-skeleton-7648b5a7b22ae3b3e3a3fa39fd83bce06554385d.tar.bz2
discord-bot-skeleton-7648b5a7b22ae3b3e3a3fa39fd83bce06554385d.zip
init: minor optimisations
move spin locks to dedicated function so we can minimise repetition of code. now uses more C and less assembly. init.c uses 8192 * 512 as a maximum even if the stack rlimit is larger
Diffstat (limited to 'init/log.c')
-rw-r--r--init/log.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/init/log.c b/init/log.c
index 75840a2..2df6d82 100644
--- a/init/log.c
+++ b/init/log.c
@@ -32,7 +32,23 @@ static const char *mode_to_string[] = {
[PANICMODE_DIE] = "catastrophic failure",
};
-int console_lock = 0;
+static int console_lock = 0;
+
+static void obtain_console_lock()
+{
+ register int rax asm("rax");
+retry:
+ while(console_lock)
+ ;
+
+ asm("mov %0, 1\n\t"
+ "xchg %0, %1" : "=r" (rax) : "m" (console_lock));
+
+ if(rax)
+ goto retry;
+
+ return;
+}
static int vaprint(const char *fmt, va_list ap)
{
@@ -80,13 +96,9 @@ static int vaprint(const char *fmt, va_list ap)
at the same time. I considered simply writing all at once, but
ended up just not caring enough to the point where spinlocks
prevail. */
- if(dolocks) {
- __asm__(".spin_lock:");
- __asm__("mov rax, 1");
- __asm__("xchg rax, [console_lock]");
- __asm__("test rax, rax");
- __asm__("jnz .spin_lock");
- }
+ if(dolocks)
+ obtain_console_lock();
+
/* we want to support stuff without colons, but frankly I havent
tested this at time of writing. will find out later */
@@ -137,11 +149,7 @@ void _panic(const char *fileorigin,
char **backtrace_symbolnames =
backtrace_symbols(backtrace_addresses, backtrace_count);
- __asm__("_panic.spin_lock:");
- __asm__("mov rax, 1");
- __asm__("xchg rax, [console_lock]");
- __asm__("test rax, rax");
- __asm__("jnz .spin_lock");
+ obtain_console_lock();
print(NOLOCK("5") "------------[ cut here ]------------");
print(LOG_SOH "\7""0" "%s at %s:%d", mode_to_string[(int)mode],