aboutsummaryrefslogtreecommitdiffstats
path: root/init
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
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')
-rw-r--r--init/init.c3
-rw-r--r--init/log.c34
2 files changed, 23 insertions, 14 deletions
diff --git a/init/init.c b/init/init.c
index e2ece8a..806792f 100644
--- a/init/init.c
+++ b/init/init.c
@@ -1,6 +1,7 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/param.h>
#include <sys/resource.h>
#include <sys/wait.h>
@@ -71,7 +72,7 @@ int main(void)
struct rlimit *stack_rlimit = malloc(sizeof(struct rlimit));
getrlimit(RLIMIT_STACK, stack_rlimit);
if(stack_rlimit->rlim_cur != RLIM_INFINITY) {
- stack_size = stack_rlimit->rlim_cur;
+ stack_size = MIN(stack_rlimit->rlim_cur, stack_size);
}
free(stack_rlimit);
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],