apio
b54a7f3a80
All checks were successful
continuous-integration/drone/push Build is passing
O_RDONLY, O_WRONLY, O_RDWR, O_TRUNC, O_CREAT and O_EXCL are fully implemented. O_APPEND is partially implemented. Other flags are not here yet.
62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
#include "thread/Thread.h"
|
|
#include <bits/open-flags.h>
|
|
#include <luna/Alloc.h>
|
|
#include <luna/Atomic.h>
|
|
|
|
static Atomic<u64> g_next_id;
|
|
|
|
LinkedList<Thread> g_threads;
|
|
|
|
void Thread::init()
|
|
{
|
|
g_next_id = 1;
|
|
}
|
|
|
|
Result<Thread*> new_thread()
|
|
{
|
|
Thread* const thread = TRY(make<Thread>());
|
|
|
|
thread->id = g_next_id++;
|
|
|
|
return thread;
|
|
}
|
|
|
|
Result<int> Thread::allocate_fd(int min)
|
|
{
|
|
if (min < 0 || min >= FD_MAX) return err(EINVAL);
|
|
for (int i = min; i < FD_MAX; i++)
|
|
{
|
|
// FIXME: Possible race condition if multiple threads share a FileDescriptorTable? Let's not worry about it for
|
|
// now, we're still a long way away from reaching that point.
|
|
if (!fd_table[i].has_value()) { return i; }
|
|
}
|
|
|
|
return err(EMFILE);
|
|
}
|
|
|
|
Result<FileDescriptor*> Thread::resolve_fd(int fd)
|
|
{
|
|
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
|
|
|
|
Option<FileDescriptor>& maybe_descriptor = fd_table[fd];
|
|
|
|
if (!maybe_descriptor.has_value()) return err(EBADF);
|
|
|
|
return maybe_descriptor.value_ptr();
|
|
}
|
|
|
|
bool FileDescriptor::should_append()
|
|
{
|
|
return flags & O_APPEND;
|
|
}
|
|
|
|
bool FileDescriptor::is_readable()
|
|
{
|
|
return flags & O_RDONLY;
|
|
}
|
|
|
|
bool FileDescriptor::is_writable()
|
|
{
|
|
return flags & O_WRONLY;
|
|
}
|