Compare commits

..

4 Commits

Author SHA1 Message Date
5b72144fac
Add a handy consume() method to LinkedList
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-19 12:35:08 +01:00
92a7004c2f
Move the reaping logic to Scheduler 2022-12-19 12:24:15 +01:00
31ee901e01
TarStream: Add a variant of read_contents() returning an OwnedStringView 2022-12-19 12:21:17 +01:00
9eb829f3a2
CString: Add strcmp() 2022-12-19 12:20:56 +01:00
8 changed files with 68 additions and 17 deletions

View File

@ -1,7 +1,6 @@
#include "Log.h" #include "Log.h"
#include "arch/CPU.h" #include "arch/CPU.h"
#include "arch/MMU.h" #include "arch/MMU.h"
#include "arch/Serial.h"
#include "arch/Timer.h" #include "arch/Timer.h"
#include "boot/Init.h" #include "boot/Init.h"
#include "boot/bootboot.h" #include "boot/bootboot.h"
@ -10,6 +9,7 @@
#include "memory/KernelVM.h" #include "memory/KernelVM.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <luna/CString.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/TarStream.h> #include <luna/TarStream.h>
#include <luna/Units.h> #include <luna/Units.h>
@ -46,22 +46,7 @@ void reap_thread()
auto dying_threads = Scheduler::check_for_dying_threads(); auto dying_threads = Scheduler::check_for_dying_threads();
CPU::enable_interrupts(); CPU::enable_interrupts();
if (dying_threads.count()) dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); });
{
Thread* thread_to_reap = dying_threads.expect_first();
Thread* next_thread = thread_to_reap;
while (thread_to_reap)
{
next_thread = dying_threads.next(thread_to_reap).value_or(nullptr);
kinfoln("reap: reaping thread with id %zu", thread_to_reap->id);
auto stack = thread_to_reap->stack;
kinfoln("deleting thread stack @ %#lx, has %zu bytes of stack", stack.bottom(), stack.bytes());
MemoryManager::unmap_owned_and_free_vm(stack.bottom(), stack.bytes() / ARCH_PAGE_SIZE).release_value();
delete thread_to_reap;
thread_to_reap = next_thread;
}
}
kernel_sleep(250); kernel_sleep(250);
} }
@ -87,8 +72,17 @@ Result<void> init()
while (TRY(stream.read_next_entry().try_set_value_with_specific_error(entry, 0))) while (TRY(stream.read_next_entry().try_set_value_with_specific_error(entry, 0)))
{ {
if (entry.type == TarStream::EntryType::RegularFile) if (entry.type == TarStream::EntryType::RegularFile)
{
kinfoln("Found file %s in initial ramdisk, of size %s", entry.name, kinfoln("Found file %s in initial ramdisk, of size %s", entry.name,
to_dynamic_unit(entry.size).release_value().chars()); to_dynamic_unit(entry.size).release_value().chars());
if (!strcmp(entry.name, "sys/config"))
{
auto contents = TRY(stream.read_contents_as_string(entry, 0, entry.size));
kinfoln("%s", contents.chars());
}
}
} }
Thread::init(); Thread::init();

View File

@ -95,6 +95,16 @@ namespace Scheduler
return new_kernel_thread_impl(thread); return new_kernel_thread_impl(thread);
} }
void reap_thread(Thread* thread)
{
kinfoln("reap: reaping thread with id %zu", thread->id);
auto stack = thread->stack;
kinfoln("deleting thread stack @ %#lx, has %zu bytes of stack", stack.bottom(), stack.bytes());
// FIXME: Propagate errors I guess?
MemoryManager::unmap_owned_and_free_vm(stack.bottom(), stack.bytes() / ARCH_PAGE_SIZE).release_value();
delete thread;
}
Thread* pick_task() Thread* pick_task()
{ {
Thread* old = g_current; Thread* old = g_current;

View File

@ -14,6 +14,8 @@ namespace Scheduler
Thread* pick_task(); Thread* pick_task();
void reap_thread(Thread* thread);
void switch_task(Registers* regs); void switch_task(Registers* regs);
void invoke(Registers* regs); void invoke(Registers* regs);

View File

@ -8,6 +8,7 @@ extern "C"
int memcmp(const void* a, const void* b, usize n); int memcmp(const void* a, const void* b, usize n);
void* memmove(void* dest, const void* src, usize n); void* memmove(void* dest, const void* src, usize n);
usize strlen(const char* str); usize strlen(const char* str);
int strcmp(const char* a, const char* b);
usize wcslen(const wchar_t* str); usize wcslen(const wchar_t* str);

View File

@ -155,26 +155,45 @@ template <typename T> class DoublyLinkedList
return nonnull_or_error((T*)extract_node(item)->get_last()); return nonnull_or_error((T*)extract_node(item)->get_last());
} }
// Iterates over the elements of the DoublyLinkedList from start to end, calling callback for every element.
template <typename Callback> void for_each(Callback callback) template <typename Callback> void for_each(Callback callback)
{ {
for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); } for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); }
} }
// Iterates over the elements of the DoublyLinkedList from end to start, calling callback for every element.
template <typename Callback> void for_each_reversed(Callback callback) template <typename Callback> void for_each_reversed(Callback callback)
{ {
for (Node* node = m_end_node; node; node = node->get_last()) { callback((T*)node); } for (Node* node = m_end_node; node; node = node->get_last()) { callback((T*)node); }
} }
// Iterates over the elements of the DoublyLinkedList from the element after 'start' to end, calling callback for
// every element.
template <typename Callback> void for_each_after(T* start, Callback callback) template <typename Callback> void for_each_after(T* start, Callback callback)
{ {
for (Node* node = extract_node(start)->m_next_node; node; node = node->get_next()) { callback((T*)node); } for (Node* node = extract_node(start)->m_next_node; node; node = node->get_next()) { callback((T*)node); }
} }
// Iterates over the elements of the DoublyLinkedList from the element before 'end' to start, calling callback for
// every element.
template <typename Callback> void for_each_before(T* end, Callback callback) template <typename Callback> void for_each_before(T* end, Callback callback)
{ {
for (Node* node = extract_node(end)->m_last_node; node; node = node->get_last()) { callback((T*)node); } for (Node* node = extract_node(end)->m_last_node; node; node = node->get_last()) { callback((T*)node); }
} }
// Iterates over the elements of the DoublyLinkedList from start to end, removing each element before passing it to
// the callback.
template <typename Callback> void consume(Callback callback)
{
for (Node* node = m_start_node; node;)
{
T* current = (T*)node;
node = node->get_next();
remove(current);
callback(current);
}
}
usize count() usize count()
{ {
return m_count; return m_count;

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <luna/OwnedStringView.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/Types.h> #include <luna/Types.h>
@ -33,6 +34,8 @@ class TarStream
usize read_contents(const Entry& entry, char* buf, usize offset, usize length); usize read_contents(const Entry& entry, char* buf, usize offset, usize length);
Result<OwnedStringView> read_contents_as_string(const Entry& entry, usize offset, usize max);
private: private:
struct [[gnu::packed]] TarHeader struct [[gnu::packed]] TarHeader
{ {

View File

@ -46,6 +46,16 @@ extern "C"
return (usize)(i - str); return (usize)(i - str);
} }
int strcmp(const char* a, const char* b)
{
while (*a && (*a == *b))
{
a++;
b++;
}
return *(const u8*)a - *(const u8*)b;
}
usize wcslen(const wchar_t* str) usize wcslen(const wchar_t* str)
{ {
const wchar_t* i = str; const wchar_t* i = str;

View File

@ -1,5 +1,6 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS #define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Alloc.h>
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/NumberParsing.h> #include <luna/NumberParsing.h>
#include <luna/TarStream.h> #include <luna/TarStream.h>
@ -95,3 +96,14 @@ usize TarStream::read_contents(const Entry& entry, char* buf, usize offset, usiz
return length; return length;
} }
Result<OwnedStringView> TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max)
{
char* buf = TRY(make_array<char>(max + 1));
usize nread = read_contents(entry, buf, offset, max);
buf[nread] = 0;
return OwnedStringView{buf};
}