Kernel: Prepare Socket for becoming a File.

Make the Socket functions take a FileDescriptor& rather than a socket role
throughout the code. Also change threads to block on a FileDescriptor,
rather than either an fd index or a Socket.
This commit is contained in:
Andreas Kling 2019-05-03 20:15:54 +02:00
parent 9f633a1871
commit 03da7046bd
Notes: sideshowbarker 2024-07-19 14:18:17 +09:00
14 changed files with 118 additions and 115 deletions

View file

@ -1,7 +1,7 @@
#include <Kernel/Thread.h>
#include <Kernel/Scheduler.h>
#include <Kernel/Process.h>
#include <Kernel/Net/Socket.h>
#include <Kernel/FileSystem/FileDescriptor.h>
#include <Kernel/VM/MemoryManager.h>
#include <LibC/signal_numbers.h>
@ -91,6 +91,7 @@ Thread::~Thread()
void Thread::unblock()
{
m_blocked_descriptor = nullptr;
if (current == this) {
m_state = Thread::Running;
return;
@ -120,6 +121,12 @@ void Thread::block(Thread::State new_state)
process().big_lock().lock();
}
void Thread::block(Thread::State new_state, FileDescriptor& descriptor)
{
m_blocked_descriptor = &descriptor;
block(new_state);
}
void Thread::sleep(dword ticks)
{
ASSERT(state() == Thread::Running);
@ -157,9 +164,10 @@ const char* to_string(Thread::State state)
void Thread::finalize()
{
dbgprintf("Finalizing Thread %u in %s(%u)\n", tid(), m_process.name().characters(), pid());
m_blocked_socket = nullptr;
set_state(Thread::State::Dead);
m_blocked_descriptor = nullptr;
if (this == &m_process.main_thread())
m_process.finalize();
}
@ -496,14 +504,14 @@ Thread* Thread::clone(Process& process)
return clone;
}
KResult Thread::wait_for_connect(Socket& socket)
KResult Thread::wait_for_connect(FileDescriptor& descriptor)
{
ASSERT(descriptor.is_socket());
auto& socket = *descriptor.socket();
if (socket.is_connected())
return KSuccess;
m_blocked_socket = socket;
block(Thread::State::BlockedConnect);
block(Thread::State::BlockedConnect, descriptor);
Scheduler::yield();
m_blocked_socket = nullptr;
if (!socket.is_connected())
return KResult(-ECONNREFUSED);
return KSuccess;
@ -533,8 +541,3 @@ bool Thread::is_thread(void* ptr)
}
return false;
}
void Thread::set_blocked_socket(Socket* socket)
{
m_blocked_socket = socket;
}