Kernel: Fix most of mutex + block race conditions

All block functions now take an optional mutex parameter that is
atomically unlocked instead of having the user unlock it before hand.
This prevents a ton of race conditions everywhere in the code!
This commit is contained in:
2025-06-06 03:59:22 +03:00
parent 96d5ed9cc7
commit eecdad50a6
36 changed files with 374 additions and 322 deletions

View File

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