diff options
author | Robin Burchell <robin+git@viroteck.net> | 2019-07-19 10:12:50 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-07-19 11:03:22 +0200 |
commit | cd76b691fbeb37977c9579d1a12715ddc10ab6c6 (patch) | |
tree | 88f800595293ff7a6672111b6eed2b9016648ca4 | |
parent | 99c53776530d3831d0e280e5733e96f971f46c96 (diff) | |
download | serenity-cd76b691fbeb37977c9579d1a12715ddc10ab6c6.zip |
Kernel: Remove memory allocations from the new Blocker API
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 2 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 2 | ||||
-rw-r--r-- | Kernel/Process.cpp | 20 | ||||
-rw-r--r-- | Kernel/Thread.cpp | 14 | ||||
-rw-r--r-- | Kernel/Thread.h | 14 | ||||
-rw-r--r-- | Kernel/init.cpp | 2 |
6 files changed, 27 insertions, 27 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 474706b7a2..1be12a55b3 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<Thread::ReceiveBlocker>(description); LOCKER(lock()); if (!m_can_read) { diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 1e803e279a..348f924f28 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<Thread::ConnectBlocker>(description); ASSERT(is_connected()); return KSuccess; } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 9d6eee3c4e..a8c3ae659e 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>(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<Thread::WriteBlocker>(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>(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>(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<Thread::ReadBlocker>(*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>(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<Thread::WaitBlocker>(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<Thread::SelectBlocker>(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<Thread::SelectBlocker>(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<Thread::AcceptBlocker>(*accepting_socket_description); if (current->m_was_interrupted_while_blocked) return -EINTR; } else { diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 2375c2c179..0f71f37dd3 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<bool()>&& condition) { - m_blocker = make<ConditionBlocker>(state_string, condition); - block_helper(); - Scheduler::yield(); + block<ConditionBlocker>(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<Thread::SleepBlocker>(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<Thread::ConnectBlocker>(description); Scheduler::yield(); if (!socket.is_connected()) return KResult(-ECONNREFUSED); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 7bed97d26f..57ad89ad3f 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 <typename T, class... Args> + void block(Args&& ... args) + { + ASSERT(!m_blocker); + T t(AK::forward<Args>(args)...); + m_blocker = &t; + block_helper(); + }; + + void unblock(); void block_until(const char* state_string, Function<bool()>&&); KResult wait_for_connect(FileDescription&); @@ -292,7 +300,7 @@ private: RefPtr<Region> m_kernel_stack_for_signal_handler_region; SignalActionData m_signal_action_data[32]; Region* m_signal_stack_user_region { nullptr }; - OwnPtr<Blocker> m_blocker; + Blocker* m_blocker { nullptr }; FPUState* m_fpu_state { nullptr }; InlineLinkedList<Thread>* m_thread_list { nullptr }; State m_state { Invalid }; diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 70a9ca8c92..e6290b26bb 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>(Thread::SemiPermanentBlocker::Reason::Lurking); Scheduler::yield(); } }); |