Compare commits

..

No commits in common. "5eefd98e1b70cf90b08e707407fdc95c0e7207ea" and "bf41b448d6a40fdaad095beb0a50c1ae4651c016" have entirely different histories.

41 changed files with 367 additions and 459 deletions

View File

@ -182,7 +182,7 @@ namespace Kernel
private: private:
BAN::WeakPtr<SharedFileData> m_shared_region; BAN::WeakPtr<SharedFileData> m_shared_region;
SpinLock m_epoll_lock; Mutex m_epoll_mutex;
BAN::LinkedList<class Epoll*> m_epolls; BAN::LinkedList<class Epoll*> m_epolls;
friend class Epoll; friend class Epoll;
friend class FileBackedRegion; friend class FileBackedRegion;

View File

@ -29,4 +29,30 @@ namespace Kernel
Lock& m_lock; Lock& m_lock;
}; };
template<typename Lock>
class LockFreeGuard
{
BAN_NON_COPYABLE(LockFreeGuard);
BAN_NON_MOVABLE(LockFreeGuard);
public:
LockFreeGuard(Lock& lock)
: m_lock(lock)
, m_depth(lock.lock_depth())
{
for (uint32_t i = 0; i < m_depth; i++)
m_lock.unlock();
}
~LockFreeGuard()
{
for (uint32_t i = 0; i < m_depth; i++)
m_lock.lock();
}
private:
Lock& m_lock;
const uint32_t m_depth;
};
} }

View File

@ -9,19 +9,7 @@
namespace Kernel namespace Kernel
{ {
class BaseMutex class Mutex
{
public:
virtual void lock() = 0;
virtual bool try_lock() = 0;
virtual void unlock() = 0;
virtual pid_t locker() const = 0;
virtual bool is_locked() const = 0;
virtual uint32_t lock_depth() const = 0;
};
class Mutex : public BaseMutex
{ {
BAN_NON_COPYABLE(Mutex); BAN_NON_COPYABLE(Mutex);
BAN_NON_MOVABLE(Mutex); BAN_NON_MOVABLE(Mutex);
@ -29,10 +17,9 @@ namespace Kernel
public: public:
Mutex() = default; Mutex() = default;
void lock() override void lock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(!tid || !Thread::current().has_spinlock());
if (tid == m_locker) if (tid == m_locker)
ASSERT(m_lock_depth > 0); ASSERT(m_lock_depth > 0);
else else
@ -50,10 +37,9 @@ namespace Kernel
m_lock_depth++; m_lock_depth++;
} }
bool try_lock() override bool try_lock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(!tid || !Thread::current().has_spinlock());
if (tid == m_locker) if (tid == m_locker)
ASSERT(m_lock_depth > 0); ASSERT(m_lock_depth > 0);
else else
@ -69,7 +55,7 @@ namespace Kernel
return true; return true;
} }
void unlock() override void unlock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(m_locker == tid); ASSERT(m_locker == tid);
@ -82,16 +68,16 @@ namespace Kernel
} }
} }
pid_t locker() const override { return m_locker; } pid_t locker() const { return m_locker; }
bool is_locked() const override { return m_locker != -1; } bool is_locked() const { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; } uint32_t lock_depth() const { return m_lock_depth; }
private: private:
BAN::Atomic<pid_t> m_locker { -1 }; BAN::Atomic<pid_t> m_locker { -1 };
uint32_t m_lock_depth { 0 }; uint32_t m_lock_depth { 0 };
}; };
class PriorityMutex : public BaseMutex class PriorityMutex
{ {
BAN_NON_COPYABLE(PriorityMutex); BAN_NON_COPYABLE(PriorityMutex);
BAN_NON_MOVABLE(PriorityMutex); BAN_NON_MOVABLE(PriorityMutex);
@ -99,7 +85,7 @@ namespace Kernel
public: public:
PriorityMutex() = default; PriorityMutex() = default;
void lock() override void lock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(!tid || !Thread::current().has_spinlock()); ASSERT(!tid || !Thread::current().has_spinlock());
@ -124,7 +110,7 @@ namespace Kernel
m_lock_depth++; m_lock_depth++;
} }
bool try_lock() override bool try_lock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(!tid || !Thread::current().has_spinlock()); ASSERT(!tid || !Thread::current().has_spinlock());
@ -147,7 +133,7 @@ namespace Kernel
return true; return true;
} }
void unlock() override void unlock()
{ {
const auto tid = Thread::current_tid(); const auto tid = Thread::current_tid();
ASSERT(m_locker == tid); ASSERT(m_locker == tid);
@ -163,9 +149,9 @@ namespace Kernel
} }
} }
pid_t locker() const override { return m_locker; } pid_t locker() const { return m_locker; }
bool is_locked() const override { return m_locker != -1; } bool is_locked() const { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; } uint32_t lock_depth() const { return m_lock_depth; }
private: private:
BAN::Atomic<pid_t> m_locker { -1 }; BAN::Atomic<pid_t> m_locker { -1 };

View File

@ -24,8 +24,6 @@ namespace Kernel
void unlock(InterruptState state); void unlock(InterruptState state);
uint32_t lock_depth() const { return current_processor_has_lock(); }
bool current_processor_has_lock() const bool current_processor_has_lock() const
{ {
return m_locker.load(BAN::MemoryOrder::memory_order_relaxed) == Processor::current_id().as_u32(); return m_locker.load(BAN::MemoryOrder::memory_order_relaxed) == Processor::current_id().as_u32();
@ -74,8 +72,6 @@ namespace Kernel
Processor::set_interrupt_state(state); Processor::set_interrupt_state(state);
} }
uint32_t lock_depth() const { return m_lock_depth; }
bool current_processor_has_lock() const bool current_processor_has_lock() const
{ {
return m_locker.load(BAN::MemoryOrder::memory_order_relaxed) == Processor::current_id().as_u32(); return m_locker.load(BAN::MemoryOrder::memory_order_relaxed) == Processor::current_id().as_u32();
@ -86,9 +82,6 @@ namespace Kernel
uint32_t m_lock_depth { 0 }; uint32_t m_lock_depth { 0 };
}; };
template<typename Lock>
class SpinLockGuardAsMutex;
template<typename Lock> template<typename Lock>
class SpinLockGuard class SpinLockGuard
{ {
@ -110,7 +103,6 @@ namespace Kernel
private: private:
Lock& m_lock; Lock& m_lock;
InterruptState m_state; InterruptState m_state;
friend class SpinLockGuardAsMutex<Lock>;
}; };
} }

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <BAN/UniqPtr.h> #include <BAN/UniqPtr.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Memory/PageTable.h> #include <kernel/Memory/PageTable.h>
#include <kernel/Memory/Types.h> #include <kernel/Memory/Types.h>
#include <kernel/ThreadBlocker.h> #include <kernel/ThreadBlocker.h>
@ -42,9 +41,9 @@ namespace Kernel
size_t virtual_page_count() const { return BAN::Math::div_round_up<size_t>(m_size, PAGE_SIZE); } size_t virtual_page_count() const { return BAN::Math::div_round_up<size_t>(m_size, PAGE_SIZE); }
size_t physical_page_count() const { return m_physical_page_count; } size_t physical_page_count() const { return m_physical_page_count; }
void pin(); void pin() { m_pinned_count++; }
void unpin(); void unpin() { if (--m_pinned_count == 0) m_pinned_blocker.unblock(); }
void wait_not_pinned(); void wait_not_pinned() { while (m_pinned_count) m_pinned_blocker.block_with_timeout_ms(100); }
virtual BAN::ErrorOr<void> msync(vaddr_t, size_t, int) = 0; virtual BAN::ErrorOr<void> msync(vaddr_t, size_t, int) = 0;
@ -69,7 +68,6 @@ namespace Kernel
vaddr_t m_vaddr { 0 }; vaddr_t m_vaddr { 0 };
size_t m_physical_page_count { 0 }; size_t m_physical_page_count { 0 };
Mutex m_pinned_mutex;
BAN::Atomic<size_t> m_pinned_count { 0 }; BAN::Atomic<size_t> m_pinned_count { 0 };
ThreadBlocker m_pinned_blocker; ThreadBlocker m_pinned_blocker;
}; };

View File

@ -331,6 +331,7 @@ namespace Kernel
bool m_is_userspace { false }; bool m_is_userspace { false };
SpinLock m_child_exit_lock;
BAN::Vector<ChildExitStatus> m_child_exit_statuses; BAN::Vector<ChildExitStatus> m_child_exit_statuses;
ThreadBlocker m_child_exit_blocker; ThreadBlocker m_child_exit_blocker;

View File

@ -11,7 +11,6 @@
namespace Kernel namespace Kernel
{ {
class BaseMutex;
class Thread; class Thread;
class ThreadBlocker; class ThreadBlocker;
@ -87,7 +86,7 @@ namespace Kernel
// if thread is already bound, this will never fail // if thread is already bound, this will never fail
BAN::ErrorOr<void> add_thread(Thread*); BAN::ErrorOr<void> add_thread(Thread*);
void block_current_thread(ThreadBlocker* thread_blocker, uint64_t wake_time_ns, BaseMutex* mutex); void block_current_thread(ThreadBlocker* thread_blocker, uint64_t wake_time_ns);
void unblock_thread(Thread*); void unblock_thread(Thread*);
Thread& current_thread(); Thread& current_thread();

View File

@ -58,27 +58,13 @@ namespace Kernel
bool add_signal(int signal); bool add_signal(int signal);
// blocks current thread and returns either on unblock, eintr, spuriously or after timeout // blocks current thread and returns either on unblock, eintr, spuriously or after timeout
// if mutex is not nullptr, it will be atomically freed before blocking and automatically locked on wake BAN::ErrorOr<void> sleep_or_eintr_ms(uint64_t ms) { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(ms, 1'000'000)); return sleep_or_eintr_ns(ms * 1'000'000); }
BAN::ErrorOr<void> sleep_or_eintr_ns(uint64_t ns); BAN::ErrorOr<void> sleep_or_eintr_ns(uint64_t ns);
BAN::ErrorOr<void> block_or_eintr_indefinite(ThreadBlocker& thread_blocker, BaseMutex* mutex); BAN::ErrorOr<void> block_or_eintr_indefinite(ThreadBlocker& thread_blocker);
BAN::ErrorOr<void> block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout, BaseMutex* mutex); BAN::ErrorOr<void> block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout) { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000)); return block_or_eintr_or_timeout_ns(thread_blocker, timeout_ms * 1'000'000, etimedout); }
BAN::ErrorOr<void> block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout, BaseMutex* mutex); BAN::ErrorOr<void> block_or_eintr_or_waketime_ms(ThreadBlocker& thread_blocker, uint64_t wake_time_ms, bool etimedout) { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(wake_time_ms, 1'000'000)); return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ms * 1'000'000, etimedout); }
BAN::ErrorOr<void> block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout);
BAN::ErrorOr<void> sleep_or_eintr_ms(uint64_t ms) BAN::ErrorOr<void> block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout);
{
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(ms, 1'000'000));
return sleep_or_eintr_ns(ms * 1'000'000);
}
BAN::ErrorOr<void> block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout, BaseMutex* mutex)
{
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000));
return block_or_eintr_or_timeout_ns(thread_blocker, timeout_ms * 1'000'000, etimedout, mutex);
}
BAN::ErrorOr<void> block_or_eintr_or_waketime_ms(ThreadBlocker& thread_blocker, uint64_t wake_time_ms, bool etimedout, BaseMutex* mutex)
{
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(wake_time_ms, 1'000'000));
return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ms * 1'000'000, etimedout, mutex);
}
pid_t tid() const { return m_tid; } pid_t tid() const { return m_tid; }

View File

@ -10,23 +10,13 @@ namespace Kernel
class ThreadBlocker class ThreadBlocker
{ {
public: public:
void block_indefinite(BaseMutex*); void block_indefinite();
void block_with_timeout_ns(uint64_t timeout_ns, BaseMutex*); void block_with_timeout_ms(uint64_t timeout_ms) { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000)); return block_with_timeout_ns(timeout_ms * 1'000'000); }
void block_with_wake_time_ns(uint64_t wake_time_ns, BaseMutex*); void block_with_wake_time_ms(uint64_t wake_time_ms) { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(wake_time_ms, 1'000'000)); return block_with_wake_time_ns(wake_time_ms * 1'000'000); }
void block_with_timeout_ns(uint64_t timeout_ns);
void block_with_wake_time_ns(uint64_t wake_time_ns);
void unblock(); void unblock();
void block_with_timeout_ms(uint64_t timeout_ms, BaseMutex* mutex)
{
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000));
return block_with_timeout_ns(timeout_ms * 1'000'000, mutex);
}
void block_with_wake_time_ms(uint64_t wake_time_ms, BaseMutex* mutex)
{
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(wake_time_ms, 1'000'000));
return block_with_wake_time_ns(wake_time_ms * 1'000'000, mutex);
}
private: private:
void add_thread_to_block_queue(SchedulerQueue::Node*); void add_thread_to_block_queue(SchedulerQueue::Node*);
void remove_blocked_thread(SchedulerQueue::Node*); void remove_blocked_thread(SchedulerQueue::Node*);

View File

@ -963,7 +963,7 @@ acpi_release_global_lock:
// FIXME: this can cause missing of event if it happens between // FIXME: this can cause missing of event if it happens between
// reading the status and blocking // reading the status and blocking
m_event_thread_blocker.block_with_timeout_ms(100, nullptr); m_event_thread_blocker.block_with_timeout_ms(100);
continue; continue;
handle_event: handle_event:

View File

@ -1,6 +1,5 @@
#include <kernel/Epoll.h> #include <kernel/Epoll.h>
#include <kernel/Lock/LockGuard.h> #include <kernel/Lock/LockGuard.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Timer/Timer.h> #include <kernel/Timer/Timer.h>
namespace Kernel namespace Kernel
@ -46,12 +45,10 @@ namespace Kernel
TRY(inode->add_epoll(this)); TRY(inode->add_epoll(this));
it->value.add_fd(fd, event); it->value.add_fd(fd, event);
SpinLockGuard _(m_ready_lock); auto processing_it = m_processing_events.find(inode);
auto ready_it = m_ready_events.find(inode); if (processing_it == m_processing_events.end())
if (ready_it == m_ready_events.end()) processing_it = MUST(m_processing_events.insert(inode, 0));
ready_it = MUST(m_ready_events.insert(inode, 0)); processing_it->value |= event.events;
ready_it->value |= event.events;
m_thread_blocker.unblock();
return {}; return {};
} }
@ -64,12 +61,10 @@ namespace Kernel
it->value.events[fd] = event; it->value.events[fd] = event;
SpinLockGuard _(m_ready_lock); auto processing_it = m_processing_events.find(inode);
auto ready_it = m_ready_events.find(inode); if (processing_it == m_processing_events.end())
if (ready_it == m_ready_events.end()) processing_it = MUST(m_processing_events.insert(inode, 0));
ready_it = MUST(m_ready_events.insert(inode, 0)); processing_it->value |= event.events;
ready_it->value |= event.events;
m_thread_blocker.unblock();
return {}; return {};
} }
@ -201,14 +196,8 @@ namespace Kernel
const uint64_t current_ns = SystemTimer::get().ns_since_boot(); const uint64_t current_ns = SystemTimer::get().ns_since_boot();
if (current_ns >= waketime_ns) if (current_ns >= waketime_ns)
break; break;
SpinLockGuard guard(m_ready_lock);
if (!m_ready_events.empty())
continue;
SpinLockGuardAsMutex smutex(guard);
const uint64_t timeout_ns = BAN::Math::min<uint64_t>(100'000'000, waketime_ns - current_ns); const uint64_t timeout_ns = BAN::Math::min<uint64_t>(100'000'000, waketime_ns - current_ns);
TRY(Thread::current().block_or_eintr_or_timeout_ns(m_thread_blocker, timeout_ns, false, &smutex)); TRY(Thread::current().block_or_eintr_or_timeout_ns(m_thread_blocker, timeout_ns, false));
} }
return event_count; return event_count;

View File

@ -46,54 +46,54 @@ namespace Kernel
void DevFileSystem::initialize_device_updater() void DevFileSystem::initialize_device_updater()
{ {
Process::create_kernel( Process::create_kernel(
[](void* _devfs) [](void*)
{ {
auto* devfs = static_cast<DevFileSystem*>(_devfs);
while (true) while (true)
{ {
{ {
LockGuard _(devfs->m_device_lock); LockGuard _(s_instance->m_device_lock);
for (auto& device : devfs->m_devices) for (auto& device : s_instance->m_devices)
device->update(); device->update();
} }
SystemTimer::get().sleep_ms(10); SystemTimer::get().sleep_ms(10);
} }
}, s_instance }, nullptr
); );
auto* sync_process = Process::create_kernel(); auto* sync_process = Process::create_kernel();
sync_process->add_thread(MUST(Thread::create_kernel( sync_process->add_thread(MUST(Thread::create_kernel(
[](void* _devfs) [](void*)
{ {
auto* devfs = static_cast<DevFileSystem*>(_devfs);
while (true) while (true)
{ {
LockGuard _(devfs->m_device_lock); LockGuard _(s_instance->m_device_lock);
while (!devfs->m_should_sync) while (!s_instance->m_should_sync)
devfs->m_sync_thread_blocker.block_indefinite(&devfs->m_device_lock); {
LockFreeGuard _(s_instance->m_device_lock);
s_instance->m_sync_thread_blocker.block_indefinite();
}
for (auto& device : devfs->m_devices) for (auto& device : s_instance->m_devices)
if (device->is_storage_device()) if (device->is_storage_device())
if (auto ret = static_cast<StorageDevice*>(device.ptr())->sync_disk_cache(); ret.is_error()) if (auto ret = static_cast<StorageDevice*>(device.ptr())->sync_disk_cache(); ret.is_error())
dwarnln("disk sync: {}", ret.error()); dwarnln("disk sync: {}", ret.error());
devfs->m_should_sync = false; s_instance->m_should_sync = false;
devfs->m_sync_done.unblock(); s_instance->m_sync_done.unblock();
} }
}, s_instance, sync_process }, nullptr, sync_process
))); )));
sync_process->add_thread(MUST(Kernel::Thread::create_kernel( sync_process->add_thread(MUST(Kernel::Thread::create_kernel(
[](void* _devfs) [](void*)
{ {
auto* devfs = static_cast<DevFileSystem*>(_devfs);
while (true) while (true)
{ {
SystemTimer::get().sleep_ms(10'000); SystemTimer::get().sleep_ms(10'000);
devfs->initiate_sync(false); s_instance->initiate_sync(false);
} }
}, s_instance, sync_process }, nullptr, sync_process
))); )));
sync_process->register_to_scheduler(); sync_process->register_to_scheduler();
@ -101,11 +101,13 @@ namespace Kernel
void DevFileSystem::initiate_sync(bool should_block) void DevFileSystem::initiate_sync(bool should_block)
{ {
LockGuard _(m_device_lock); {
m_should_sync = true; LockGuard _(m_device_lock);
m_sync_thread_blocker.unblock(); m_should_sync = true;
while (should_block && m_should_sync) m_sync_thread_blocker.unblock();
m_sync_done.block_indefinite(&m_device_lock); }
if (should_block)
m_sync_done.block_indefinite();
} }
void DevFileSystem::add_device(BAN::RefPtr<Device> device) void DevFileSystem::add_device(BAN::RefPtr<Device> device)

View File

@ -278,14 +278,14 @@ namespace Kernel
BAN::ErrorOr<void> Inode::add_epoll(class Epoll* epoll) BAN::ErrorOr<void> Inode::add_epoll(class Epoll* epoll)
{ {
SpinLockGuard _(m_epoll_lock); LockGuard _(m_epoll_mutex);
TRY(m_epolls.push_back(epoll)); TRY(m_epolls.push_back(epoll));
return {}; return {};
} }
void Inode::del_epoll(class Epoll* epoll) void Inode::del_epoll(class Epoll* epoll)
{ {
SpinLockGuard _(m_epoll_lock); LockGuard _(m_epoll_mutex);
for (auto it = m_epolls.begin(); it != m_epolls.end(); it++) for (auto it = m_epolls.begin(); it != m_epolls.end(); it++)
{ {
if (*it != epoll) if (*it != epoll)
@ -297,7 +297,7 @@ namespace Kernel
void Inode::epoll_notify(uint32_t event) void Inode::epoll_notify(uint32_t event)
{ {
SpinLockGuard _(m_epoll_lock); LockGuard _(m_epoll_mutex);
for (auto* epoll : m_epolls) for (auto* epoll : m_epolls)
epoll->notify(this, event); epoll->notify(this, event);
} }

View File

@ -44,8 +44,6 @@ namespace Kernel
void Pipe::on_close(int status_flags) void Pipe::on_close(int status_flags)
{ {
LockGuard _(m_mutex);
if (status_flags & O_WRONLY) if (status_flags & O_WRONLY)
{ {
auto old_writing_count = m_writing_count.fetch_sub(1); auto old_writing_count = m_writing_count.fetch_sub(1);
@ -73,7 +71,8 @@ namespace Kernel
{ {
if (m_writing_count == 0) if (m_writing_count == 0)
return 0; return 0;
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); LockFreeGuard lock_free(m_mutex);
TRY(Thread::current().block_or_eintr_or_timeout_ms(m_thread_blocker, 100, false));
} }
const size_t to_copy = BAN::Math::min<size_t>(buffer.size(), m_buffer_size); const size_t to_copy = BAN::Math::min<size_t>(buffer.size(), m_buffer_size);
@ -109,7 +108,8 @@ namespace Kernel
Thread::current().add_signal(SIGPIPE); Thread::current().add_signal(SIGPIPE);
return BAN::Error::from_errno(EPIPE); return BAN::Error::from_errno(EPIPE);
} }
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); LockFreeGuard lock_free(m_mutex);
TRY(Thread::current().block_or_eintr_or_timeout_ms(m_thread_blocker, 100, false));
} }
const size_t to_copy = BAN::Math::min(buffer.size(), m_buffer.size() - m_buffer_size); const size_t to_copy = BAN::Math::min(buffer.size(), m_buffer.size() - m_buffer_size);

View File

@ -1,5 +1,6 @@
#include <kernel/FS/ProcFS/FileSystem.h> #include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/ProcFS/Inode.h> #include <kernel/FS/ProcFS/Inode.h>
#include <kernel/Lock/LockGuard.h>
namespace Kernel namespace Kernel
{ {

View File

@ -1,7 +1,7 @@
#include <kernel/Device/DeviceNumbers.h> #include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Input/InputDevice.h> #include <kernel/Input/InputDevice.h>
#include <kernel/Lock/SpinLockAsMutex.h> #include <kernel/Lock/LockGuard.h>
#include <LibInput/KeyEvent.h> #include <LibInput/KeyEvent.h>
#include <LibInput/MouseEvent.h> #include <LibInput/MouseEvent.h>
@ -181,18 +181,23 @@ namespace Kernel
if (buffer.size() < m_event_size) if (buffer.size() < m_event_size)
return BAN::Error::from_errno(ENOBUFS); return BAN::Error::from_errno(ENOBUFS);
SpinLockGuard guard(m_event_lock); auto state = m_event_lock.lock();
while (m_event_count == 0) while (m_event_count == 0)
{ {
// FIXME: should m_mutex be unlocked? m_event_lock.unlock(state);
SpinLockGuardAsMutex smutex(guard); {
TRY(Thread::current().block_or_eintr_indefinite(m_event_thread_blocker, &smutex)); LockFreeGuard _(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_event_thread_blocker));
}
state = m_event_lock.lock();
} }
memcpy(buffer.data(), &m_event_buffer[m_event_tail * m_event_size], m_event_size); memcpy(buffer.data(), &m_event_buffer[m_event_tail * m_event_size], m_event_size);
m_event_tail = (m_event_tail + 1) % m_max_event_count; m_event_tail = (m_event_tail + 1) % m_max_event_count;
m_event_count--; m_event_count--;
m_event_lock.unlock(state);
return m_event_size; return m_event_size;
} }
@ -251,8 +256,8 @@ namespace Kernel
return bytes; return bytes;
} }
// FIXME: race condition as notify doesn't lock mutex LockFreeGuard _(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker));
} }
} }
@ -303,8 +308,8 @@ namespace Kernel
return bytes; return bytes;
} }
// FIXME: race condition as notify doesn't lock mutex LockFreeGuard _(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker));
} }
} }

View File

@ -1,3 +1,4 @@
#include <kernel/Lock/LockGuard.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Memory/MemoryBackedRegion.h> #include <kernel/Memory/MemoryBackedRegion.h>

View File

@ -1,4 +1,3 @@
#include <kernel/Lock/LockGuard.h>
#include <kernel/Memory/MemoryRegion.h> #include <kernel/Memory/MemoryRegion.h>
namespace Kernel namespace Kernel
@ -60,24 +59,4 @@ namespace Kernel
return ret; return ret;
} }
void MemoryRegion::pin()
{
LockGuard _(m_pinned_mutex);
m_pinned_count++;
}
void MemoryRegion::unpin()
{
LockGuard _(m_pinned_mutex);
if (--m_pinned_count == 0)
m_pinned_blocker.unblock();
}
void MemoryRegion::wait_not_pinned()
{
LockGuard _(m_pinned_mutex);
while (m_pinned_count)
m_pinned_blocker.block_with_timeout_ms(100, &m_pinned_mutex);
}
} }

View File

@ -1,3 +1,4 @@
#include <kernel/Lock/LockGuard.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Memory/VirtualRange.h> #include <kernel/Memory/VirtualRange.h>

View File

@ -1,4 +1,3 @@
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Networking/ARPTable.h> #include <kernel/Networking/ARPTable.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/Timer/Timer.h> #include <kernel/Timer/Timer.h>
@ -159,15 +158,16 @@ namespace Kernel
for (;;) for (;;)
{ {
PendingArpPacket pending = ({ PendingArpPacket pending = ({
SpinLockGuard guard(m_pending_lock); auto state = m_pending_lock.lock();
while (m_pending_packets.empty()) while (m_pending_packets.empty())
{ {
SpinLockGuardAsMutex smutex(guard); m_pending_lock.unlock(state);
m_pending_thread_blocker.block_indefinite(&smutex); m_pending_thread_blocker.block_indefinite();
state = m_pending_lock.lock();
} }
auto packet = m_pending_packets.front(); auto packet = m_pending_packets.front();
m_pending_packets.pop(); m_pending_packets.pop();
m_pending_lock.unlock(state);
packet; packet;
}); });

View File

@ -292,10 +292,8 @@ namespace Kernel
void E1000::handle_irq() void E1000::handle_irq()
{ {
const uint32_t icr = read32(REG_ICR); if (!(read32(REG_ICR) & (ICR_RxQ0 | ICR_RXT0)))
if (!(icr & (ICR_RxQ0 | ICR_RXT0)))
return; return;
write32(REG_ICR, icr);
SpinLockGuard _(m_lock); SpinLockGuard _(m_lock);
@ -317,6 +315,8 @@ namespace Kernel
descriptor.status = 0; descriptor.status = 0;
write32(REG_RDT0, rx_current); write32(REG_RDT0, rx_current);
} }
write32(REG_ICR, 0xFFFFFFFF);
} }
} }

View File

@ -1,6 +1,5 @@
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Memory/PageTable.h> #include <kernel/Memory/PageTable.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Networking/ICMP.h> #include <kernel/Networking/ICMP.h>
#include <kernel/Networking/IPv4Layer.h> #include <kernel/Networking/IPv4Layer.h>
#include <kernel/Networking/NetworkManager.h> #include <kernel/Networking/NetworkManager.h>
@ -332,15 +331,16 @@ namespace Kernel
for (;;) for (;;)
{ {
PendingIPv4Packet pending = ({ PendingIPv4Packet pending = ({
SpinLockGuard guard(m_pending_lock); auto state = m_pending_lock.lock();
while (m_pending_packets.empty()) while (m_pending_packets.empty())
{ {
SpinLockGuardAsMutex smutex(guard); m_pending_lock.unlock(state);
m_pending_thread_blocker.block_indefinite(&smutex); m_pending_thread_blocker.block_indefinite();
state = m_pending_lock.lock();
} }
auto packet = m_pending_packets.front(); auto packet = m_pending_packets.front();
m_pending_packets.pop(); m_pending_packets.pop();
m_pending_lock.unlock(state);
packet; packet;
}); });

View File

@ -1,4 +1,3 @@
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Networking/NetworkManager.h> #include <kernel/Networking/NetworkManager.h>
#include <kernel/Networking/RTL8169/Definitions.h> #include <kernel/Networking/RTL8169/Definitions.h>
#include <kernel/Networking/RTL8169/RTL8169.h> #include <kernel/Networking/RTL8169/RTL8169.h>
@ -206,18 +205,13 @@ namespace Kernel
return BAN::Error::from_errno(EADDRNOTAVAIL); return BAN::Error::from_errno(EADDRNOTAVAIL);
auto state = m_lock.lock(); auto state = m_lock.lock();
const uint32_t tx_current = m_tx_current; const uint32_t tx_current = m_tx_current;
m_tx_current = (m_tx_current + 1) % m_tx_descriptor_count; m_tx_current = (m_tx_current + 1) % m_tx_descriptor_count;
m_lock.unlock(state);
auto& descriptor = reinterpret_cast<volatile RTL8169Descriptor*>(m_tx_descriptor_region->vaddr())[tx_current]; auto& descriptor = reinterpret_cast<volatile RTL8169Descriptor*>(m_tx_descriptor_region->vaddr())[tx_current];
while (descriptor.command & RTL8169_DESC_CMD_OWN) while (descriptor.command & RTL8169_DESC_CMD_OWN)
{ m_thread_blocker.block_with_timeout_ms(100);
SpinLockAsMutex smutex(m_lock, state);
m_thread_blocker.block_indefinite(&smutex);
}
m_lock.unlock(state);
auto* tx_buffer = reinterpret_cast<uint8_t*>(m_tx_buffer_region->vaddr() + tx_current * buffer_size); auto* tx_buffer = reinterpret_cast<uint8_t*>(m_tx_buffer_region->vaddr() + tx_current * buffer_size);
@ -252,10 +246,7 @@ namespace Kernel
} }
if (interrupt_status & RTL8169_IR_TOK) if (interrupt_status & RTL8169_IR_TOK)
{
SpinLockGuard _(m_lock);
m_thread_blocker.unblock(); m_thread_blocker.unblock();
}
if (interrupt_status & RTL8169_IR_RER) if (interrupt_status & RTL8169_IR_RER)
dwarnln("Rx error"); dwarnln("Rx error");

View File

@ -73,7 +73,10 @@ namespace Kernel
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
while (m_pending_connections.empty()) while (m_pending_connections.empty())
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); {
LockFreeGuard _(m_mutex);
TRY(Thread::current().block_or_eintr_or_timeout_ms(m_thread_blocker, 100, false));
}
auto connection = m_pending_connections.front(); auto connection = m_pending_connections.front();
m_pending_connections.pop(); m_pending_connections.pop();
@ -108,7 +111,12 @@ namespace Kernel
const uint64_t wake_time_ms = SystemTimer::get().ms_since_boot() + 5000; const uint64_t wake_time_ms = SystemTimer::get().ms_since_boot() + 5000;
while (!return_inode->m_has_connected) while (!return_inode->m_has_connected)
TRY(Thread::current().block_or_eintr_or_waketime_ms(return_inode->m_thread_blocker, wake_time_ms, true, &m_mutex)); {
if (SystemTimer::get().ms_since_boot() >= wake_time_ms)
return BAN::Error::from_errno(ECONNABORTED);
LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_or_waketime_ms(return_inode->m_thread_blocker, wake_time_ms, true));
}
if (address) if (address)
{ {
@ -160,7 +168,12 @@ namespace Kernel
const uint64_t wake_time_ms = SystemTimer::get().ms_since_boot() + 5000; const uint64_t wake_time_ms = SystemTimer::get().ms_since_boot() + 5000;
while (!m_has_connected) while (!m_has_connected)
TRY(Thread::current().block_or_eintr_or_waketime_ms(m_thread_blocker, wake_time_ms, true, &m_mutex)); {
if (SystemTimer::get().ms_since_boot() >= wake_time_ms)
return BAN::Error::from_errno(ECONNREFUSED);
LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_or_waketime_ms(m_thread_blocker, wake_time_ms, true));
}
return {}; return {};
} }
@ -195,7 +208,8 @@ namespace Kernel
{ {
if (m_state != State::Established) if (m_state != State::Established)
return return_with_maybe_zero(); return return_with_maybe_zero();
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_or_timeout_ms(m_thread_blocker, 100, false));
} }
const uint32_t to_recv = BAN::Math::min<uint32_t>(buffer.size(), m_recv_window.data_size); const uint32_t to_recv = BAN::Math::min<uint32_t>(buffer.size(), m_recv_window.data_size);
@ -225,7 +239,8 @@ namespace Kernel
{ {
if (m_state != State::Established) if (m_state != State::Established)
return return_with_maybe_zero(); return return_with_maybe_zero();
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_or_timeout_ms(m_thread_blocker, 100, false));
} }
const size_t to_send = BAN::Math::min<size_t>(message.size(), m_send_window.buffer->size() - m_send_window.data_size); const size_t to_send = BAN::Math::min<size_t>(message.size(), m_send_window.buffer->size() - m_send_window.data_size);
@ -504,10 +519,8 @@ namespace Kernel
} }
auto socket = it->value; auto socket = it->value;
m_mutex.unlock(); LockFreeGuard _(m_mutex);
socket->receive_packet(buffer, sender, sender_len); socket->receive_packet(buffer, sender, sender_len);
m_mutex.lock();
return; return;
} }
break; break;
@ -647,114 +660,116 @@ namespace Kernel
BAN::RefPtr<TCPSocket> keep_alive { this }; BAN::RefPtr<TCPSocket> keep_alive { this };
this->unref(); this->unref();
LockGuard _(m_mutex);
while (m_process) while (m_process)
{ {
const uint64_t current_ms = SystemTimer::get().ms_since_boot(); const uint64_t current_ms = SystemTimer::get().ms_since_boot();
if (m_state == State::TimeWait && current_ms >= m_time_wait_start_ms + 30'000)
{ {
set_connection_as_closed(); LockGuard _(m_mutex);
continue;
}
// This is the last instance if (m_state == State::TimeWait && current_ms >= m_time_wait_start_ms + 30'000)
if (ref_count() == 1)
{
if (m_state == State::Listen)
{ {
set_connection_as_closed(); set_connection_as_closed();
continue; continue;
} }
if (m_state == State::Established)
// This is the last instance
if (ref_count() == 1)
{ {
m_next_flags = FIN | ACK; if (m_state == State::Listen)
m_next_state = State::FinWait1;
}
}
if (m_next_flags)
{
ASSERT(m_connection_info.has_value());
auto* target_address = reinterpret_cast<const sockaddr*>(&m_connection_info->address);
auto target_address_len = m_connection_info->address_len;
if (auto ret = m_network_layer.sendto(*this, {}, target_address, target_address_len); ret.is_error())
dwarnln("{}", ret.error());
const bool hungup_before = has_hungup_impl();
m_state = m_next_state;
if (m_state == State::Established)
m_has_connected = true;
if (!hungup_before && has_hungup_impl())
epoll_notify(EPOLLHUP);
continue;
}
if (m_send_window.data_size > 0 && m_send_window.current_ack - m_send_window.has_ghost_byte > m_send_window.start_seq)
{
uint32_t acknowledged_bytes = m_send_window.current_ack - m_send_window.start_seq - m_send_window.has_ghost_byte;
ASSERT(acknowledged_bytes <= m_send_window.data_size);
m_send_window.data_size -= acknowledged_bytes;
m_send_window.start_seq += acknowledged_bytes;
if (m_send_window.data_size > 0)
{
auto* send_buffer = reinterpret_cast<uint8_t*>(m_send_window.buffer->vaddr());
memmove(send_buffer, send_buffer + acknowledged_bytes, m_send_window.data_size);
}
m_send_window.sent_size -= acknowledged_bytes;
epoll_notify(EPOLLOUT);
dprintln_if(DEBUG_TCP, "Target acknowledged {} bytes", acknowledged_bytes);
continue;
}
const bool should_retransmit = m_send_window.data_size > 0 && current_ms >= m_send_window.last_send_ms + retransmit_timeout_ms;
if (m_send_window.data_size > m_send_window.sent_size || should_retransmit)
{
ASSERT(m_connection_info.has_value());
auto* target_address = reinterpret_cast<const sockaddr*>(&m_connection_info->address);
auto target_address_len = m_connection_info->address_len;
const uint32_t send_base = should_retransmit ? 0 : m_send_window.sent_size;
const uint32_t total_send = BAN::Math::min<uint32_t>(m_send_window.data_size - send_base, m_send_window.scaled_size());
m_send_window.current_seq = m_send_window.start_seq;
auto* send_buffer = reinterpret_cast<const uint8_t*>(m_send_window.buffer->vaddr() + send_base);
for (uint32_t i = 0; i < total_send;)
{
const uint32_t to_send = BAN::Math::min(total_send - i, m_send_window.mss);
auto message = BAN::ConstByteSpan(send_buffer + i, to_send);
m_next_flags = ACK;
if (auto ret = m_network_layer.sendto(*this, message, target_address, target_address_len); ret.is_error())
{ {
set_connection_as_closed();
continue;
}
if (m_state == State::Established)
{
m_next_flags = FIN | ACK;
m_next_state = State::FinWait1;
}
}
if (m_next_flags)
{
ASSERT(m_connection_info.has_value());
auto* target_address = reinterpret_cast<const sockaddr*>(&m_connection_info->address);
auto target_address_len = m_connection_info->address_len;
if (auto ret = m_network_layer.sendto(*this, {}, target_address, target_address_len); ret.is_error())
dwarnln("{}", ret.error()); dwarnln("{}", ret.error());
break; const bool hungup_before = has_hungup_impl();
m_state = m_next_state;
if (m_state == State::Established)
m_has_connected = true;
if (!hungup_before && has_hungup_impl())
epoll_notify(EPOLLHUP);
continue;
}
if (m_send_window.data_size > 0 && m_send_window.current_ack - m_send_window.has_ghost_byte > m_send_window.start_seq)
{
uint32_t acknowledged_bytes = m_send_window.current_ack - m_send_window.start_seq - m_send_window.has_ghost_byte;
ASSERT(acknowledged_bytes <= m_send_window.data_size);
m_send_window.data_size -= acknowledged_bytes;
m_send_window.start_seq += acknowledged_bytes;
if (m_send_window.data_size > 0)
{
auto* send_buffer = reinterpret_cast<uint8_t*>(m_send_window.buffer->vaddr());
memmove(send_buffer, send_buffer + acknowledged_bytes, m_send_window.data_size);
} }
dprintln_if(DEBUG_TCP, "Sent {} bytes", to_send); m_send_window.sent_size -= acknowledged_bytes;
m_send_window.sent_size += to_send; epoll_notify(EPOLLOUT);
m_send_window.current_seq += to_send;
i += to_send; dprintln_if(DEBUG_TCP, "Target acknowledged {} bytes", acknowledged_bytes);
continue;
} }
m_send_window.last_send_ms = current_ms; const bool should_retransmit = m_send_window.data_size > 0 && current_ms >= m_send_window.last_send_ms + retransmit_timeout_ms;
continue; if (m_send_window.data_size > m_send_window.sent_size || should_retransmit)
{
ASSERT(m_connection_info.has_value());
auto* target_address = reinterpret_cast<const sockaddr*>(&m_connection_info->address);
auto target_address_len = m_connection_info->address_len;
const uint32_t send_base = should_retransmit ? 0 : m_send_window.sent_size;
const uint32_t total_send = BAN::Math::min<uint32_t>(m_send_window.data_size - send_base, m_send_window.scaled_size());
m_send_window.current_seq = m_send_window.start_seq;
auto* send_buffer = reinterpret_cast<const uint8_t*>(m_send_window.buffer->vaddr() + send_base);
for (uint32_t i = 0; i < total_send;)
{
const uint32_t to_send = BAN::Math::min(total_send - i, m_send_window.mss);
auto message = BAN::ConstByteSpan(send_buffer + i, to_send);
m_next_flags = ACK;
if (auto ret = m_network_layer.sendto(*this, message, target_address, target_address_len); ret.is_error())
{
dwarnln("{}", ret.error());
break;
}
dprintln_if(DEBUG_TCP, "Sent {} bytes", to_send);
m_send_window.sent_size += to_send;
m_send_window.current_seq += to_send;
i += to_send;
}
m_send_window.last_send_ms = current_ms;
continue;
}
} }
m_thread_blocker.unblock(); m_thread_blocker.unblock();
m_thread_blocker.block_with_wake_time_ms(current_ms + retransmit_timeout_ms, &m_mutex); m_thread_blocker.block_with_wake_time_ms(current_ms + retransmit_timeout_ms);
} }
m_thread_blocker.unblock(); m_thread_blocker.unblock();

View File

@ -1,4 +1,3 @@
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Networking/UDPSocket.h> #include <kernel/Networking/UDPSocket.h>
#include <kernel/Thread.h> #include <kernel/Thread.h>
@ -94,12 +93,12 @@ namespace Kernel
} }
ASSERT(m_port != PORT_NONE); ASSERT(m_port != PORT_NONE);
SpinLockGuard guard(m_packet_lock); auto state = m_packet_lock.lock();
while (m_packets.empty()) while (m_packets.empty())
{ {
SpinLockGuardAsMutex smutex(guard); m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker));
state = m_packet_lock.lock();
} }
auto packet_info = m_packets.front(); auto packet_info = m_packets.front();
@ -121,6 +120,8 @@ namespace Kernel
m_packet_total_size -= packet_info.packet_size; m_packet_total_size -= packet_info.packet_size;
m_packet_lock.unlock(state);
if (address && address_len) if (address && address_len)
{ {
if (*address_len > (socklen_t)sizeof(sockaddr_storage)) if (*address_len > (socklen_t)sizeof(sockaddr_storage))

View File

@ -1,6 +1,5 @@
#include <BAN/HashMap.h> #include <BAN/HashMap.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Networking/NetworkManager.h> #include <kernel/Networking/NetworkManager.h>
#include <kernel/Networking/UNIX/Socket.h> #include <kernel/Networking/UNIX/Socket.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
@ -17,8 +16,6 @@ namespace Kernel
static constexpr size_t s_packet_buffer_size = 10 * PAGE_SIZE; static constexpr size_t s_packet_buffer_size = 10 * PAGE_SIZE;
// FIXME: why is this using spinlocks instead of mutexes??
BAN::ErrorOr<BAN::RefPtr<UnixDomainSocket>> UnixDomainSocket::create(Socket::Type socket_type, const Socket::Info& info) BAN::ErrorOr<BAN::RefPtr<UnixDomainSocket>> UnixDomainSocket::create(Socket::Type socket_type, const Socket::Info& info)
{ {
auto socket = TRY(BAN::RefPtr<UnixDomainSocket>::create(socket_type, info)); auto socket = TRY(BAN::RefPtr<UnixDomainSocket>::create(socket_type, info));
@ -94,16 +91,13 @@ namespace Kernel
if (!connection_info.listening) if (!connection_info.listening)
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
while (connection_info.pending_connections.empty())
TRY(Thread::current().block_or_eintr_indefinite(connection_info.pending_thread_blocker));
BAN::RefPtr<UnixDomainSocket> pending; BAN::RefPtr<UnixDomainSocket> pending;
{ {
SpinLockGuard guard(connection_info.pending_lock); SpinLockGuard _(connection_info.pending_lock);
SpinLockGuardAsMutex smutex(guard);
while (connection_info.pending_connections.empty())
TRY(Thread::current().block_or_eintr_indefinite(connection_info.pending_thread_blocker, &smutex));
pending = connection_info.pending_connections.front(); pending = connection_info.pending_connections.front();
connection_info.pending_connections.pop(); connection_info.pending_connections.pop();
connection_info.pending_thread_blocker.unblock(); connection_info.pending_thread_blocker.unblock();
@ -182,18 +176,16 @@ namespace Kernel
for (;;) for (;;)
{ {
auto& target_info = target->m_info.get<ConnectionInfo>(); auto& target_info = target->m_info.get<ConnectionInfo>();
SpinLockGuard guard(target_info.pending_lock);
if (target_info.pending_connections.size() < target_info.pending_connections.capacity())
{ {
MUST(target_info.pending_connections.push(this)); SpinLockGuard _(target_info.pending_lock);
target_info.pending_thread_blocker.unblock(); if (target_info.pending_connections.size() < target_info.pending_connections.capacity())
break; {
MUST(target_info.pending_connections.push(this));
target_info.pending_thread_blocker.unblock();
break;
}
} }
TRY(Thread::current().block_or_eintr_indefinite(target_info.pending_thread_blocker));
SpinLockGuardAsMutex smutex(guard);
TRY(Thread::current().block_or_eintr_indefinite(target_info.pending_thread_blocker, &smutex));
} }
target->epoll_notify(EPOLLIN); target->epoll_notify(EPOLLIN);
@ -277,8 +269,9 @@ namespace Kernel
auto state = m_packet_lock.lock(); auto state = m_packet_lock.lock();
while (m_packet_sizes.full() || m_packet_size_total + packet.size() > s_packet_buffer_size) while (m_packet_sizes.full() || m_packet_size_total + packet.size() > s_packet_buffer_size)
{ {
SpinLockAsMutex smutex(m_packet_lock, state); m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker));
state = m_packet_lock.lock();
} }
uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr() + m_packet_size_total); uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr() + m_packet_size_total);
@ -401,19 +394,14 @@ namespace Kernel
auto& connection_info = m_info.get<ConnectionInfo>(); auto& connection_info = m_info.get<ConnectionInfo>();
bool expected = true; bool expected = true;
if (connection_info.target_closed.compare_exchange(expected, false)) if (connection_info.target_closed.compare_exchange(expected, false))
{
m_packet_lock.unlock(state);
return 0; return 0;
}
if (!connection_info.connection) if (!connection_info.connection)
{
m_packet_lock.unlock(state);
return BAN::Error::from_errno(ENOTCONN); return BAN::Error::from_errno(ENOTCONN);
}
} }
SpinLockAsMutex smutex(m_packet_lock, state); m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker));
state = m_packet_lock.lock();
} }
uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr()); uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr());

View File

@ -279,8 +279,6 @@ namespace Kernel
if (parent.pid() != m_parent) if (parent.pid() != m_parent)
return BAN::Iteration::Continue; return BAN::Iteration::Continue;
LockGuard _(parent.m_process_lock);
for (auto& child : parent.m_child_exit_statuses) for (auto& child : parent.m_child_exit_statuses)
{ {
if (child.pid != pid()) if (child.pid != pid())
@ -769,13 +767,13 @@ namespace Kernel
return child.pid == pid; return child.pid == pid;
}; };
LockGuard _(m_process_lock);
for (;;) for (;;)
{ {
pid_t exited_pid = 0; pid_t exited_pid = 0;
int exit_code = 0; int exit_code = 0;
{ {
SpinLockGuard _(m_child_exit_lock);
bool found = false; bool found = false;
for (auto& child : m_child_exit_statuses) for (auto& child : m_child_exit_statuses)
{ {
@ -798,6 +796,7 @@ namespace Kernel
{ {
if (stat_loc) if (stat_loc)
{ {
LockGuard _(m_process_lock);
TRY(validate_pointer_access(stat_loc, sizeof(stat_loc), true)); TRY(validate_pointer_access(stat_loc, sizeof(stat_loc), true));
*stat_loc = exit_code; *stat_loc = exit_code;
} }
@ -811,7 +810,7 @@ namespace Kernel
if (options & WNOHANG) if (options & WNOHANG)
return 0; return 0;
m_child_exit_blocker.block_indefinite(&m_process_lock); m_child_exit_blocker.block_indefinite();
} }
} }
@ -2610,7 +2609,11 @@ namespace Kernel
for (;;) for (;;)
{ {
TRY(Thread::current().block_or_eintr_indefinite(m_pthread_exit_blocker, &m_process_lock)); {
LockFreeGuard _(m_process_lock);
m_pthread_exit_blocker.block_with_timeout_ms(100);
}
if (wait_thread()) if (wait_thread())
return 0; return 0;
} }

View File

@ -1,7 +1,6 @@
#include <BAN/Optional.h> #include <BAN/Optional.h>
#include <BAN/Sort.h> #include <BAN/Sort.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Process.h> #include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/Thread.h> #include <kernel/Thread.h>
@ -600,7 +599,7 @@ namespace Kernel
return {}; return {};
} }
void Scheduler::block_current_thread(ThreadBlocker* blocker, uint64_t wake_time_ns, BaseMutex* mutex) void Scheduler::block_current_thread(ThreadBlocker* blocker, uint64_t wake_time_ns)
{ {
auto state = Processor::get_interrupt_state(); auto state = Processor::get_interrupt_state();
Processor::set_interrupt_state(InterruptState::Disabled); Processor::set_interrupt_state(InterruptState::Disabled);
@ -613,23 +612,9 @@ namespace Kernel
if (blocker) if (blocker)
blocker->add_thread_to_block_queue(m_current); blocker->add_thread_to_block_queue(m_current);
update_most_loaded_node_queue(m_current, &m_block_queue); update_most_loaded_node_queue(m_current, &m_block_queue);
uint32_t lock_depth = 0;
if (mutex != nullptr)
{
ASSERT(mutex->is_locked() && mutex->locker() == m_current->thread->tid());
lock_depth = mutex->lock_depth();
}
for (uint32_t i = 0; i < lock_depth; i++)
mutex->unlock();
Processor::yield(); Processor::yield();
Processor::set_interrupt_state(state); Processor::set_interrupt_state(state);
for (uint32_t i = 0; i < lock_depth; i++)
mutex->lock();
} }
void Scheduler::unblock_thread(Thread* thread) void Scheduler::unblock_thread(Thread* thread)

View File

@ -1,4 +1,5 @@
#include <kernel/BootInfo.h> #include <kernel/BootInfo.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Memory/PageTable.h> #include <kernel/Memory/PageTable.h>
#include <kernel/Storage/DiskCache.h> #include <kernel/Storage/DiskCache.h>

View File

@ -1,4 +1,4 @@
#include <kernel/Lock/SpinLockAsMutex.h> #include <kernel/Lock/LockGuard.h>
#include <kernel/Storage/NVMe/Queue.h> #include <kernel/Storage/NVMe/Queue.h>
#include <kernel/Thread.h> #include <kernel/Thread.h>
#include <kernel/Timer/Timer.h> #include <kernel/Timer/Timer.h>
@ -72,7 +72,7 @@ namespace Kernel
// scheduler has put the current thread blocking. // scheduler has put the current thread blocking.
// EINTR should also be handled here. // EINTR should also be handled here.
while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time_ms + s_nvme_command_timeout_ms) while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time_ms + s_nvme_command_timeout_ms)
m_thread_blocker.block_with_wake_time_ms(start_time_ms + s_nvme_command_timeout_ms, nullptr); m_thread_blocker.block_with_wake_time_ms(start_time_ms + s_nvme_command_timeout_ms);
if (m_done_mask & cid_mask) if (m_done_mask & cid_mask)
{ {
@ -87,12 +87,12 @@ namespace Kernel
uint16_t NVMeQueue::reserve_cid() uint16_t NVMeQueue::reserve_cid()
{ {
SpinLockGuard guard(m_lock); auto state = m_lock.lock();
while (~m_used_mask == 0) while (~m_used_mask == 0)
{ {
SpinLockGuardAsMutex smutex(guard); m_lock.unlock(state);
m_thread_blocker.block_with_timeout_ms(s_nvme_command_timeout_ms, &smutex); m_thread_blocker.block_with_timeout_ms(s_nvme_command_timeout_ms);
state = m_lock.lock();
} }
uint16_t cid = 0; uint16_t cid = 0;
@ -104,6 +104,7 @@ namespace Kernel
m_used_mask |= (size_t)1 << cid; m_used_mask |= (size_t)1 << cid;
m_lock.unlock(state);
return cid; return cid;
} }

View File

@ -4,12 +4,10 @@
#include <kernel/Process.h> #include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/Syscall.h> #include <kernel/Syscall.h>
#include <kernel/Timer/Timer.h>
#include <termios.h> #include <termios.h>
#define DUMP_ALL_SYSCALLS 0 #define DUMP_ALL_SYSCALLS 0
#define DUMP_LONG_SYSCALLS 0
namespace Kernel namespace Kernel
{ {
@ -42,7 +40,7 @@ namespace Kernel
{ {
ASSERT(GDT::is_user_segment(interrupt_stack->cs)); ASSERT(GDT::is_user_segment(interrupt_stack->cs));
Processor::set_interrupt_state(InterruptState::Enabled); asm volatile("sti");
BAN::ErrorOr<long> ret = BAN::Error::from_errno(ENOSYS); BAN::ErrorOr<long> ret = BAN::Error::from_errno(ENOSYS);
@ -52,10 +50,6 @@ namespace Kernel
dprintln("{} pid {}: {}", process_path, Process::current().pid(), s_syscall_names[syscall]); dprintln("{} pid {}: {}", process_path, Process::current().pid(), s_syscall_names[syscall]);
#endif #endif
#if DUMP_LONG_SYSCALLS
const uint64_t start_ns = SystemTimer::get().ns_since_boot();
#endif
if (syscall < 0 || syscall >= __SYSCALL_COUNT) if (syscall < 0 || syscall >= __SYSCALL_COUNT)
dwarnln("No syscall {}", syscall); dwarnln("No syscall {}", syscall);
else if (syscall == SYS_FORK) else if (syscall == SYS_FORK)
@ -68,7 +62,7 @@ namespace Kernel
ret = (Process::current().*s_syscall_handlers[syscall])(arg1, arg2, arg3, arg4, arg5); ret = (Process::current().*s_syscall_handlers[syscall])(arg1, arg2, arg3, arg4, arg5);
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
Processor::set_interrupt_state(InterruptState::Disabled); asm volatile("cli");
#if DUMP_ALL_SYSCALLS #if DUMP_ALL_SYSCALLS
if (ret.is_error()) if (ret.is_error())
@ -80,17 +74,6 @@ namespace Kernel
dwarnln("{} pid {}: {}: ENOTSUP", process_path, Process::current().pid(), s_syscall_names[syscall]); dwarnln("{} pid {}: {}: ENOTSUP", process_path, Process::current().pid(), s_syscall_names[syscall]);
#endif #endif
#if DUMP_LONG_SYSCALLS
const uint64_t end_ns = SystemTimer::get().ns_since_boot();
const uint64_t duration_us = (end_ns - start_ns) / 1000;
if (duration_us > 1'000)
dwarnln("{} {} took {}.{3} ms",
Process::current().name(),
s_syscall_names[syscall],
duration_us / 1000, duration_us % 1000
);
#endif
if (ret.is_error() && ret.error().is_kernel_error()) if (ret.is_error() && ret.error().is_kernel_error())
Kernel::panic("Kernel error while returning to userspace {}", ret.error()); Kernel::panic("Kernel error while returning to userspace {}", ret.error());

View File

@ -1,6 +1,5 @@
#include <kernel/Device/DeviceNumbers.h> #include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Terminal/PseudoTerminal.h> #include <kernel/Terminal/PseudoTerminal.h>
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
@ -89,15 +88,17 @@ namespace Kernel
bool PseudoTerminalMaster::putchar(uint8_t ch) bool PseudoTerminalMaster::putchar(uint8_t ch)
{ {
SpinLockGuard _(m_buffer_lock); {
SpinLockGuard _(m_buffer_lock);
if (m_buffer_size >= m_buffer->size()) if (m_buffer_size >= m_buffer->size())
return false; return false;
reinterpret_cast<uint8_t*>(m_buffer->vaddr())[(m_buffer_tail + m_buffer_size) % m_buffer->size()] = ch; reinterpret_cast<uint8_t*>(m_buffer->vaddr())[(m_buffer_tail + m_buffer_size) % m_buffer->size()] = ch;
m_buffer_size++; m_buffer_size++;
m_buffer_blocker.unblock(); m_buffer_blocker.unblock();
}
epoll_notify(EPOLLIN); epoll_notify(EPOLLIN);
@ -106,12 +107,13 @@ namespace Kernel
BAN::ErrorOr<size_t> PseudoTerminalMaster::read_impl(off_t, BAN::ByteSpan buffer) BAN::ErrorOr<size_t> PseudoTerminalMaster::read_impl(off_t, BAN::ByteSpan buffer)
{ {
SpinLockGuard guard(m_buffer_lock); auto state = m_buffer_lock.lock();
while (m_buffer_size == 0) while (m_buffer_size == 0)
{ {
SpinLockGuardAsMutex smutex(guard); m_buffer_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_buffer_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_buffer_blocker));
m_buffer_lock.lock();
} }
const size_t to_copy = BAN::Math::min(buffer.size(), m_buffer_size); const size_t to_copy = BAN::Math::min(buffer.size(), m_buffer_size);
@ -130,6 +132,8 @@ namespace Kernel
m_buffer_size -= to_copy; m_buffer_size -= to_copy;
m_buffer_tail = (m_buffer_tail + to_copy) % m_buffer->size(); m_buffer_tail = (m_buffer_tail + to_copy) % m_buffer->size();
m_buffer_lock.unlock(state);
epoll_notify(EPOLLOUT); epoll_notify(EPOLLOUT);
return to_copy; return to_copy;

View File

@ -92,8 +92,6 @@ namespace Kernel
if (flags & ~(TTY_FLAG_ENABLE_INPUT | TTY_FLAG_ENABLE_OUTPUT)) if (flags & ~(TTY_FLAG_ENABLE_INPUT | TTY_FLAG_ENABLE_OUTPUT))
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
LockGuard _(m_mutex);
switch (command) switch (command)
{ {
case TTY_CMD_SET: case TTY_CMD_SET:
@ -131,16 +129,12 @@ namespace Kernel
while (true) while (true)
{ {
{ while (!TTY::current()->m_tty_ctrl.receive_input)
LockGuard _(TTY::current()->m_mutex); TTY::current()->m_tty_ctrl.thread_blocker.block_indefinite();
while (!TTY::current()->m_tty_ctrl.receive_input)
TTY::current()->m_tty_ctrl.thread_blocker.block_indefinite(&TTY::current()->m_mutex);
}
while (TTY::current()->m_tty_ctrl.receive_input) while (TTY::current()->m_tty_ctrl.receive_input)
{ {
LockGuard _(keyboard_inode->m_mutex); LockGuard _(keyboard_inode->m_mutex);
if (!keyboard_inode->can_read()) if (!keyboard_inode->can_read())
{ {
SystemTimer::get().sleep_ms(1); SystemTimer::get().sleep_ms(1);
@ -401,7 +395,10 @@ namespace Kernel
BAN::ErrorOr<size_t> TTY::read_impl(off_t, BAN::ByteSpan buffer) BAN::ErrorOr<size_t> TTY::read_impl(off_t, BAN::ByteSpan buffer)
{ {
while (!m_output.flush) while (!m_output.flush)
TRY(Thread::current().block_or_eintr_indefinite(m_output.thread_blocker, &m_mutex)); {
LockFreeGuard _(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_output.thread_blocker));
}
if (m_output.bytes == 0) if (m_output.bytes == 0)
{ {
@ -431,19 +428,12 @@ namespace Kernel
BAN::ErrorOr<size_t> TTY::write_impl(off_t, BAN::ConstByteSpan buffer) BAN::ErrorOr<size_t> TTY::write_impl(off_t, BAN::ConstByteSpan buffer)
{ {
SpinLockGuard _(m_write_lock);
size_t written = 0; size_t written = 0;
for (; written < buffer.size(); written++)
{ if (!putchar(buffer[written]))
SpinLockGuard _(m_write_lock); break;
for (; written < buffer.size(); written++) update_cursor();
if (!putchar(buffer[written]))
break;
update_cursor();
}
if (can_write_impl())
epoll_notify(EPOLLOUT);
return written; return written;
} }

View File

@ -149,10 +149,10 @@ namespace Kernel
BAN::Optional<TerminalDriver::Color> VirtualTTY::get_8bit_color() BAN::Optional<TerminalDriver::Color> VirtualTTY::get_8bit_color()
{ {
ASSERT(m_ansi_state.nums[1] == 5); ASSERT(m_ansi_state.nums[1] == 5);
if (m_ansi_state.nums[2] < 0) if (m_ansi_state.nums[2] < 1)
return {}; return {};
const uint8_t code = BAN::Math::min(m_ansi_state.nums[2], 255); const uint8_t code = BAN::Math::min(m_ansi_state.nums[2], 256) - 1;
if (code < 16) if (code < 16)
return m_palette[code]; return m_palette[code];
@ -171,12 +171,12 @@ namespace Kernel
BAN::Optional<TerminalDriver::Color> VirtualTTY::get_24bit_color() BAN::Optional<TerminalDriver::Color> VirtualTTY::get_24bit_color()
{ {
ASSERT(m_ansi_state.nums[1] == 2); ASSERT(m_ansi_state.nums[1] == 2);
if (m_ansi_state.nums[2] < 0) return {}; if (m_ansi_state.nums[2] < 1) return {};
if (m_ansi_state.nums[3] < 0) return {}; if (m_ansi_state.nums[3] < 1) return {};
if (m_ansi_state.nums[4] < 0) return {}; if (m_ansi_state.nums[4] < 1) return {};
const uint8_t r = BAN::Math::min(m_ansi_state.nums[2], 255); const uint8_t r = BAN::Math::min(m_ansi_state.nums[2], 256) - 1;
const uint8_t g = BAN::Math::min(m_ansi_state.nums[3], 255); const uint8_t g = BAN::Math::min(m_ansi_state.nums[3], 256) - 1;
const uint8_t b = BAN::Math::min(m_ansi_state.nums[4], 255); const uint8_t b = BAN::Math::min(m_ansi_state.nums[4], 256) - 1;
return TerminalDriver::Color(r, g, b); return TerminalDriver::Color(r, g, b);
} }
@ -251,7 +251,7 @@ namespace Kernel
// Clear from cursor to the end of screen // Clear from cursor to the end of screen
for (uint32_t i = m_column; i < m_width; i++) for (uint32_t i = m_column; i < m_width; i++)
putchar_at(' ', i, m_row); putchar_at(' ', i, m_row);
for (uint32_t row = m_row + 1; row < m_height; row++) for (uint32_t row = 0; row < m_height; row++)
for (uint32_t col = 0; col < m_width; col++) for (uint32_t col = 0; col < m_width; col++)
putchar_at(' ', col, row); putchar_at(' ', col, row);
return reset_ansi(); return reset_ansi();

View File

@ -581,27 +581,27 @@ namespace Kernel
return {}; return {};
} }
BAN::ErrorOr<void> Thread::block_or_eintr_indefinite(ThreadBlocker& thread_blocker, BaseMutex* mutex) BAN::ErrorOr<void> Thread::block_or_eintr_indefinite(ThreadBlocker& thread_blocker)
{ {
if (is_interrupted_by_signal()) if (is_interrupted_by_signal())
return BAN::Error::from_errno(EINTR); return BAN::Error::from_errno(EINTR);
thread_blocker.block_indefinite(mutex); thread_blocker.block_indefinite();
if (is_interrupted_by_signal()) if (is_interrupted_by_signal())
return BAN::Error::from_errno(EINTR); return BAN::Error::from_errno(EINTR);
return {}; return {};
} }
BAN::ErrorOr<void> Thread::block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout, BaseMutex* mutex) BAN::ErrorOr<void> Thread::block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout)
{ {
const uint64_t wake_time_ns = SystemTimer::get().ns_since_boot() + timeout_ns; const uint64_t wake_time_ns = SystemTimer::get().ns_since_boot() + timeout_ns;
return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ns, etimedout, mutex); return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ns, etimedout);
} }
BAN::ErrorOr<void> Thread::block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout, BaseMutex* mutex) BAN::ErrorOr<void> Thread::block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout)
{ {
if (is_interrupted_by_signal()) if (is_interrupted_by_signal())
return BAN::Error::from_errno(EINTR); return BAN::Error::from_errno(EINTR);
thread_blocker.block_with_wake_time_ns(wake_time_ns, mutex); thread_blocker.block_with_wake_time_ns(wake_time_ns);
if (is_interrupted_by_signal()) if (is_interrupted_by_signal())
return BAN::Error::from_errno(EINTR); return BAN::Error::from_errno(EINTR);
if (etimedout && SystemTimer::get().ms_since_boot() >= wake_time_ns) if (etimedout && SystemTimer::get().ms_since_boot() >= wake_time_ns)

View File

@ -5,19 +5,19 @@
namespace Kernel namespace Kernel
{ {
void ThreadBlocker::block_indefinite(BaseMutex* mutex) void ThreadBlocker::block_indefinite()
{ {
Processor::scheduler().block_current_thread(this, static_cast<uint64_t>(-1), mutex); Processor::scheduler().block_current_thread(this, static_cast<uint64_t>(-1));
} }
void ThreadBlocker::block_with_timeout_ns(uint64_t timeout_ns, BaseMutex* mutex) void ThreadBlocker::block_with_timeout_ns(uint64_t timeout_ns)
{ {
Processor::scheduler().block_current_thread(this, SystemTimer::get().ns_since_boot() + timeout_ns, mutex); Processor::scheduler().block_current_thread(this, SystemTimer::get().ns_since_boot() + timeout_ns);
} }
void ThreadBlocker::block_with_wake_time_ns(uint64_t wake_time_ns, BaseMutex* mutex) void ThreadBlocker::block_with_wake_time_ns(uint64_t wake_time_ns)
{ {
Processor::scheduler().block_current_thread(this, wake_time_ns, mutex); Processor::scheduler().block_current_thread(this, wake_time_ns);
} }
void ThreadBlocker::unblock() void ThreadBlocker::unblock()

View File

@ -83,7 +83,9 @@ namespace Kernel
{ {
if (ns == 0) if (ns == 0)
return; return;
Processor::scheduler().block_current_thread(nullptr, ns_since_boot() + ns, nullptr);
const uint64_t wake_time_ns = ns_since_boot() + ns;
Processor::scheduler().block_current_thread(nullptr, wake_time_ns);
} }
timespec SystemTimer::real_time() const timespec SystemTimer::real_time() const

View File

@ -269,8 +269,7 @@ namespace Kernel
m_is_init_done = true; m_is_init_done = true;
} }
// FIXME: race condition m_changed_port_blocker.block_with_timeout_ms(100);
m_changed_port_blocker.block_with_timeout_ms(100, nullptr);
continue; continue;
} }

View File

@ -322,8 +322,7 @@ namespace Kernel
m_ports_initialized = true; m_ports_initialized = true;
} }
// FIXME: prevent race condition m_port_thread_blocker.block_with_timeout_ms(100);
m_port_thread_blocker.block_with_timeout_ms(100, nullptr);
expected = true; expected = true;
} }
} }

View File

@ -93,7 +93,7 @@ namespace LibGUI
draw_character(text[i], font, tl_x + (int32_t)(i * font.width()), tl_y, color); draw_character(text[i], font, tl_x + (int32_t)(i * font.width()), tl_y, color);
} }
void Texture::shift_vertical(int32_t amount) void Texture::shift_vertical(int32_t amount, uint32_t fill_color)
{ {
const uint32_t amount_abs = BAN::Math::abs(amount); const uint32_t amount_abs = BAN::Math::abs(amount);
if (amount_abs == 0 || amount_abs >= height()) if (amount_abs == 0 || amount_abs >= height())
@ -102,9 +102,15 @@ namespace LibGUI
uint32_t* dst = (amount > 0) ? m_pixels.data() + width() * amount_abs : m_pixels.data(); uint32_t* dst = (amount > 0) ? m_pixels.data() + width() * amount_abs : m_pixels.data();
uint32_t* src = (amount < 0) ? m_pixels.data() + width() * amount_abs : m_pixels.data(); uint32_t* src = (amount < 0) ? m_pixels.data() + width() * amount_abs : m_pixels.data();
memmove(dst, src, width() * (height() - amount_abs) * 4); memmove(dst, src, width() * (height() - amount_abs) * 4);
const uint32_t y_lo = (amount < 0) ? height() - amount_abs : 0;
const uint32_t y_hi = (amount < 0) ? height() : amount_abs;
for (uint32_t y = y_lo; y < y_hi; y++)
for (uint32_t x = 0; x < width(); x++)
set_pixel(x, y, fill_color);
} }
void Texture::copy_horizontal_slice(int32_t dst_y, int32_t src_y, uint32_t uamount) void Texture::copy_horizontal_slice(int32_t dst_y, int32_t src_y, uint32_t uamount, uint32_t fill_color)
{ {
int32_t amount = uamount; int32_t amount = uamount;
if (dst_y < 0) if (dst_y < 0)
@ -128,10 +134,18 @@ namespace LibGUI
copy_amount * width() * 4 copy_amount * width() * 4
); );
} }
const uint32_t fill_y_off = (src_y < copy_src_y) ? 0 : copy_amount;
const uint32_t fill_amount = amount - copy_amount;
for (uint32_t i = 0; i < fill_amount; i++)
for (uint32_t x = 0; x < width(); x++)
set_pixel(x, dst_y + fill_y_off + i, fill_color);
} }
void Texture::copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height) void Texture::copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height, uint32_t fill_color)
{ {
fill_rect(dst_x, dst_y, width, height, fill_color);
if (!clamp_to_texture(dst_x, dst_y, width, height)) if (!clamp_to_texture(dst_x, dst_y, width, height))
return; return;
if (!clamp_to_texture(src_x, src_y, width, height)) if (!clamp_to_texture(src_x, src_y, width, height))
@ -142,8 +156,8 @@ namespace LibGUI
{ {
const uint32_t y_off = copy_dir ? i : height - i - 1; const uint32_t y_off = copy_dir ? i : height - i - 1;
memmove( memmove(
&m_pixels[(dst_y + y_off) * this->width() + dst_x], &m_pixels[(dst_y + y_off) * this->width()],
&m_pixels[(src_y + y_off) * this->width() + src_x], &m_pixels[(src_y + y_off) * this->width()],
width * 4 width * 4
); );
} }

View File

@ -308,8 +308,7 @@ bool Terminal::read_shell()
{ {
const uint32_t scroll = m_cursor.y + newline_count - rows() + 1; const uint32_t scroll = m_cursor.y + newline_count - rows() + 1;
m_cursor.y -= scroll; m_cursor.y -= scroll;
m_window->texture().shift_vertical(-scroll * (int32_t)m_font.height()); m_window->texture().shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
m_window->texture().fill_rect(0, m_window->height() - scroll * m_font.height(), m_window->width(), scroll * m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() }; should_invalidate = { 0, 0, m_window->width(), m_window->height() };
} }
@ -350,9 +349,6 @@ void Terminal::handle_sgr(int32_t value)
case 10: case 10:
// default font // default font
break; break;
case 22:
m_is_bold = false;
break;
case 27: case 27:
m_colors_inverted = false; m_colors_inverted = false;
break; break;
@ -383,10 +379,10 @@ void Terminal::handle_sgr(int32_t value)
BAN::Optional<uint32_t> Terminal::get_8bit_color() BAN::Optional<uint32_t> Terminal::get_8bit_color()
{ {
ASSERT(m_csi_info.fields[1] == 5); ASSERT(m_csi_info.fields[1] == 5);
if (m_csi_info.fields[2] < 0) if (m_csi_info.fields[2] < 1)
return {}; return {};
const uint8_t code = BAN::Math::min(m_csi_info.fields[2], 255); const uint8_t code = BAN::Math::min(m_csi_info.fields[2], 256) - 1;
if (code < 8) if (code < 8)
return s_colors_dark[code]; return s_colors_dark[code];
if (code < 16) if (code < 16)
@ -407,12 +403,12 @@ BAN::Optional<uint32_t> Terminal::get_8bit_color()
BAN::Optional<uint32_t> Terminal::get_24bit_color() BAN::Optional<uint32_t> Terminal::get_24bit_color()
{ {
ASSERT(m_csi_info.fields[1] == 2); ASSERT(m_csi_info.fields[1] == 2);
if (m_csi_info.fields[2] < 0) return {}; if (m_csi_info.fields[2] < 1) return {};
if (m_csi_info.fields[3] < 0) return {}; if (m_csi_info.fields[3] < 1) return {};
if (m_csi_info.fields[4] < 0) return {}; if (m_csi_info.fields[4] < 1) return {};
const uint8_t r = BAN::Math::min(m_csi_info.fields[2], 255); const uint8_t r = BAN::Math::min(m_csi_info.fields[2], 256) - 1;
const uint8_t g = BAN::Math::min(m_csi_info.fields[3], 255); const uint8_t g = BAN::Math::min(m_csi_info.fields[3], 256) - 1;
const uint8_t b = BAN::Math::min(m_csi_info.fields[4], 255); const uint8_t b = BAN::Math::min(m_csi_info.fields[4], 256) - 1;
return b | (g << 8) | (r << 16) | (0xCC << 24); return b | (g << 8) | (r << 16) | (0xCC << 24);
} }
@ -544,7 +540,7 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t src_y = m_cursor.y * m_font.height(); const uint32_t src_y = m_cursor.y * m_font.height();
const uint32_t dst_y = src_y + count * m_font.height(); const uint32_t dst_y = src_y + count * m_font.height();
texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y); texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
texture.fill_rect(0, src_y, m_window->width(), count * m_font.height(), m_bg_color); texture.fill_rect(0, src_y, m_window->width(), count * m_font.height(), m_bg_color);
should_invalidate = { should_invalidate = {
0, 0,
@ -561,7 +557,7 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t dst_y = m_cursor.y * m_font.height(); const uint32_t dst_y = m_cursor.y * m_font.height();
const uint32_t src_y = dst_y + count * m_font.height(); const uint32_t src_y = dst_y + count * m_font.height();
texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y); texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
texture.fill_rect(0, m_window->height() - count * m_font.height(), m_window->width(), count * m_font.height(), m_bg_color); texture.fill_rect(0, m_window->height() - count * m_font.height(), m_window->width(), count * m_font.height(), m_bg_color);
should_invalidate = { should_invalidate = {
0, 0,
@ -572,24 +568,6 @@ Rectangle Terminal::handle_csi(char ch)
break; break;
} }
case 'P':
{
const uint32_t count = (m_csi_info.fields[0] == -1) ? 1 : m_csi_info.fields[0];
const uint32_t dst_x = m_cursor.x * m_font.width();
const uint32_t src_x = (m_cursor.x + count) * m_font.width();
const uint32_t y = m_cursor.y * m_font.height();
texture.copy_rect(dst_x, y, src_x, y, m_window->width() - src_x, m_font.height());
texture.fill_rect(m_window->width() - count * m_font.width(), y, count * m_font.width(), m_font.height(), m_bg_color);
should_invalidate = {
dst_x,
y,
m_window->width() - dst_x,
m_font.height()
};
break;
}
case '@': case '@':
{ {
const uint32_t count = (m_csi_info.fields[0] == -1) ? 1 : m_csi_info.fields[0]; const uint32_t count = (m_csi_info.fields[0] == -1) ? 1 : m_csi_info.fields[0];
@ -597,7 +575,7 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t src_x = m_cursor.x * m_font.width(); const uint32_t src_x = m_cursor.x * m_font.width();
const uint32_t y = m_cursor.y * m_font.height(); const uint32_t y = m_cursor.y * m_font.height();
texture.copy_rect(dst_x, y, src_x, y, m_window->width() - dst_x, m_font.height()); texture.copy_rect(dst_x, y, src_x, y, m_window->width() - dst_x, m_font.height(), m_bg_color);
texture.fill_rect(src_x, y, count * m_font.width(), m_font.height(), m_bg_color); texture.fill_rect(src_x, y, count * m_font.width(), m_font.height(), m_bg_color);
should_invalidate = { should_invalidate = {
src_x, src_x,
@ -703,8 +681,7 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
{ {
const uint32_t scroll = m_cursor.y - rows() + 1; const uint32_t scroll = m_cursor.y - rows() + 1;
m_cursor.y -= scroll; m_cursor.y -= scroll;
texture.shift_vertical(-scroll * (int32_t)m_font.height()); texture.shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
texture.fill_rect(0, m_window->width() - scroll * m_font.width(), m_window->width(), scroll * m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() }; should_invalidate = { 0, 0, m_window->width(), m_window->height() };
} }
@ -731,8 +708,7 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
{ {
const uint32_t scroll = m_cursor.y - rows() + 1; const uint32_t scroll = m_cursor.y - rows() + 1;
m_cursor.y -= scroll; m_cursor.y -= scroll;
texture.shift_vertical(-scroll * (int32_t)m_font.height()); texture.shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
texture.fill_rect(0, m_window->width() - scroll * m_font.width(), m_window->width(), scroll * m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() }; should_invalidate = { 0, 0, m_window->width(), m_window->height() };
} }