diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 474706b7a2e..1be12a55b39 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -212,7 +212,7 @@ ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t } load_receive_deadline(); - current->block(*new Thread::ReceiveBlocker(description)); + current->block(description); LOCKER(lock()); if (!m_can_read) { diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 1e803e279a6..348f924f28e 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -162,7 +162,7 @@ KResult TCPSocket::protocol_connect(FileDescription& description, ShouldBlock sh m_state = State::Connecting; if (should_block == ShouldBlock::Yes) { - current->block(*new Thread::ConnectBlocker(description)); + current->block(description); ASSERT(is_connected()); return KSuccess; } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 9d6eee3c4e8..a8c3ae659e5 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -869,7 +869,7 @@ ssize_t Process::sys$writev(int fd, const struct iovec* iov, int iov_count) } if (current->has_unmasked_pending_signals()) { - current->block(*new Thread::SemiPermanentBlocker(Thread::SemiPermanentBlocker::Reason::Signal)); + current->block(Thread::SemiPermanentBlocker::Reason::Signal); if (nwritten == 0) return -EINTR; } @@ -900,7 +900,7 @@ ssize_t Process::do_write(FileDescription& description, const u8* data, int data #ifdef IO_DEBUG dbgprintf("block write on %d\n", fd); #endif - current->block(*new Thread::WriteBlocker(description)); + current->block(description); } ssize_t rc = description.write(data + nwritten, data_size - nwritten); #ifdef IO_DEBUG @@ -914,7 +914,7 @@ ssize_t Process::do_write(FileDescription& description, const u8* data, int data if (rc == 0) break; if (current->has_unmasked_pending_signals()) { - current->block(*new Thread::SemiPermanentBlocker(Thread::SemiPermanentBlocker::Reason::Signal)); + current->block(Thread::SemiPermanentBlocker::Reason::Signal); if (nwritten == 0) return -EINTR; } @@ -939,7 +939,7 @@ ssize_t Process::sys$write(int fd, const u8* data, ssize_t size) return -EBADF; auto nwritten = do_write(*description, data, size); if (current->has_unmasked_pending_signals()) { - current->block(*new Thread::SemiPermanentBlocker(Thread::SemiPermanentBlocker::Reason::Signal)); + current->block(Thread::SemiPermanentBlocker::Reason::Signal); if (nwritten == 0) return -EINTR; } @@ -962,7 +962,7 @@ ssize_t Process::sys$read(int fd, u8* buffer, ssize_t size) return -EBADF; if (description->is_blocking()) { if (!description->can_read()) { - current->block(*new Thread::ReadBlocker(*description)); + current->block(*description); if (current->m_was_interrupted_while_blocked) return -EINTR; } @@ -1265,7 +1265,7 @@ int Process::sys$kill(pid_t pid, int signal) } if (pid == m_pid) { current->send_signal(signal, this); - current->block(*new Thread::SemiPermanentBlocker(Thread::SemiPermanentBlocker::Reason::Signal)); + current->block(Thread::SemiPermanentBlocker::Reason::Signal); return 0; } InterruptDisabler disabler; @@ -1442,7 +1442,7 @@ pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options) } pid_t waitee_pid = waitee; - current->block(*new Thread::WaitBlocker(options, waitee_pid)); + current->block(options, waitee_pid); if (current->m_was_interrupted_while_blocked) return -EINTR; @@ -1827,7 +1827,7 @@ int Process::sys$select(const Syscall::SC_select_params* params) #endif if (!params->timeout || select_has_timeout) - current->block(*new Thread::SelectBlocker(timeout, select_has_timeout, rfds, wfds, efds)); + current->block(timeout, select_has_timeout, rfds, wfds, efds); int marked_fd_count = 0; auto mark_fds = [&](auto* fds, auto& vector, auto should_mark) { @@ -1882,7 +1882,7 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout) #endif if (has_timeout|| timeout < 0) - current->block(*new Thread::SelectBlocker(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector())); + current->block(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector()); int fds_with_revents = 0; @@ -2125,7 +2125,7 @@ int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* a auto& socket = *accepting_socket_description->socket(); if (!socket.can_accept()) { if (accepting_socket_description->is_blocking()) { - current->block(*new Thread::AcceptBlocker(*accepting_socket_description)); + current->block(*accepting_socket_description); if (current->m_was_interrupted_while_blocked) return -EINTR; } else { diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 2375c2c1794..0f71f37dd32 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -110,9 +110,7 @@ void Thread::unblock() void Thread::block_until(const char* state_string, Function&& condition) { - m_blocker = make(state_string, condition); - block_helper(); - Scheduler::yield(); + block(state_string, condition); } void Thread::block_helper() @@ -126,17 +124,11 @@ void Thread::block_helper() process().big_lock().lock(); } -void Thread::block(Blocker& blocker) -{ - m_blocker = &blocker; - block_helper(); -} - u64 Thread::sleep(u32 ticks) { ASSERT(state() == Thread::Running); u64 wakeup_time = g_uptime + ticks; - current->block(*new Thread::SleepBlocker(wakeup_time)); + current->block(wakeup_time); return wakeup_time; } @@ -532,7 +524,7 @@ KResult Thread::wait_for_connect(FileDescription& description) auto& socket = *description.socket(); if (socket.is_connected()) return KSuccess; - block(*new Thread::ConnectBlocker(description)); + block(description); Scheduler::yield(); if (!socket.is_connected()) return KResult(-ECONNREFUSED); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 7bed97d26fd..57ad89ad3f3 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -208,9 +208,17 @@ public: u32 ticks() const { return m_ticks; } u64 sleep(u32 ticks); - void block(Blocker& blocker); - void unblock(); + template + void block(Args&& ... args) + { + ASSERT(!m_blocker); + T t(AK::forward(args)...); + m_blocker = &t; + block_helper(); + }; + + void unblock(); void block_until(const char* state_string, Function&&); KResult wait_for_connect(FileDescription&); @@ -292,7 +300,7 @@ private: RefPtr m_kernel_stack_for_signal_handler_region; SignalActionData m_signal_action_data[32]; Region* m_signal_stack_user_region { nullptr }; - OwnPtr m_blocker; + Blocker* m_blocker { nullptr }; FPUState* m_fpu_state { nullptr }; InlineLinkedList* m_thread_list { nullptr }; State m_state { Invalid }; diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 70a9ca8c926..e6290b26bb5 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -242,7 +242,7 @@ extern "C" [[noreturn]] void init() current->process().set_priority(Process::LowPriority); for (;;) { Thread::finalize_dying_threads(); - current->block(*new Thread::SemiPermanentBlocker(Thread::SemiPermanentBlocker::Reason::Lurking)); + current->block(Thread::SemiPermanentBlocker::Reason::Lurking); Scheduler::yield(); } });