Compare commits

..

No commits in common. "7b4cfd52cdb89016fddfa84e0cc0ecf4871fe8bd" and "77dcfab5ef701bf02ca0027ff803b954a461af1d" have entirely different histories.

5 changed files with 26 additions and 83 deletions

View File

@ -7,10 +7,7 @@ Result<u64> sys_exit(Registers*, SyscallArgs args)
Thread* current = Scheduler::current();
Scheduler::for_each_child(current, [](Thread* child) {
child->parent_id = 1;
return true;
});
Scheduler::for_each_child((pid_t)current->id, [](Thread* child) { child->parent_id = 1; });
current->status = status;
current->state = ThreadState::Exited;

View File

@ -27,11 +27,11 @@ Result<u64> sys_waitpid(Registers*, SyscallArgs args)
}
else if (pid == -1)
{
if (!Scheduler::has_children(current)) return err(ECHILD);
if (!Scheduler::has_children((pid_t)current->id)) return err(ECHILD);
Option<Thread*> child;
while (child = Scheduler::find_exited_child(current), !child.has_value())
while (child = Scheduler::find_exited_child((pid_t)current->id), !child.has_value())
{
if (options & WNOHANG) return err(EAGAIN);
kernel_sleep(10);

View File

@ -249,13 +249,12 @@ namespace Scheduler
g_current->ticks_left--;
for (auto* const thread : g_threads)
{
g_threads.for_each([](Thread* thread) {
if (thread->state == ThreadState::Sleeping)
{
if (--thread->sleep_ticks_left == 0) thread->state = ThreadState::Runnable;
}
}
});
if (!g_current->ticks_left) switch_task(regs);
}
@ -277,37 +276,30 @@ namespace Scheduler
Option<Thread*> find_by_pid(pid_t pid)
{
for (auto* const thread : g_threads)
{
if (thread->id == (u64)pid && thread->state != ThreadState::Dying) return thread;
}
Option<Thread*> result;
return {};
}
bool has_children(Thread* thread)
{
bool result { false };
for_each_child(thread, [&](Thread*) {
result = true;
return false;
g_threads.for_each([&](Thread* thread) {
if (thread->id == (u64)pid && thread->state != ThreadState::Dying) result = thread;
});
return result;
}
Option<Thread*> find_exited_child(Thread* thread)
bool has_children(pid_t pid)
{
bool result { false };
for_each_child(pid, [&](Thread*) { result = true; });
return result;
}
Option<Thread*> find_exited_child(pid_t pid)
{
Option<Thread*> result;
for_each_child(thread, [&](Thread* child) {
if (!result.has_value() && child->state == ThreadState::Exited)
{
result = child;
return false;
}
return true;
for_each_child(pid, [&](Thread* child) {
if (!result.has_value() && child->state == ThreadState::Exited) result = child;
});
return result;

View File

@ -29,21 +29,16 @@ namespace Scheduler
Option<Thread*> find_by_pid(pid_t pid);
template <typename Callback> void for_each_child(Thread* thread, Callback callback)
template <typename Callback> void for_each_child(pid_t pid, Callback callback)
{
for (Thread* current = thread; current; current = g_threads.next(current).value_or(nullptr))
{
if (current && current->parent_id == thread->id)
{
bool should_continue = callback(current);
if (!should_continue) return;
}
}
g_threads.for_each([&](Thread* thread) {
if (thread->parent_id == (u64)pid) callback(thread);
});
}
bool has_children(Thread* thread);
bool has_children(pid_t pid);
Option<Thread*> find_exited_child(Thread* thread);
Option<Thread*> find_exited_child(pid_t pid);
}
extern "C" void kernel_yield();

View File

@ -210,47 +210,6 @@ template <typename T> class LinkedList
return m_count;
}
struct LinkedListIterator
{
typedef T* PtrT;
private:
LinkedListIterator(PtrT ptr, LinkedList<T>& list) : m_ptr(ptr), m_list(list)
{
}
PtrT m_ptr;
LinkedList<T>& m_list;
public:
PtrT& operator*()
{
return m_ptr;
}
void operator++()
{
m_ptr = m_list.next(m_ptr).value_or(nullptr);
}
bool operator!=(LinkedListIterator& other)
{
return m_ptr != other.m_ptr || &m_list != &other.m_list;
}
friend class LinkedList<T>;
};
LinkedListIterator begin()
{
return { (T*)m_start_node, *this };
}
LinkedListIterator end()
{
return { nullptr, *this };
}
private:
Node* m_start_node = nullptr;
Node* m_end_node = nullptr;