diff --git a/Kernel/TTY/PTYMultiplexer.cpp b/Kernel/TTY/PTYMultiplexer.cpp index a45bc6b8205..4dce9e97645 100644 --- a/Kernel/TTY/PTYMultiplexer.cpp +++ b/Kernel/TTY/PTYMultiplexer.cpp @@ -24,9 +24,11 @@ PTYMultiplexer& PTYMultiplexer::the() UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer() : CharacterDevice(5, 2) { - m_freelist.ensure_capacity(max_pty_pairs); - for (int i = max_pty_pairs; i > 0; --i) - m_freelist.unchecked_append(i - 1); + m_freelist.with_exclusive([&](auto& freelist) { + freelist.ensure_capacity(max_pty_pairs); + for (int i = max_pty_pairs; i > 0; --i) + freelist.unchecked_append(i - 1); + }); } UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer() @@ -35,27 +37,30 @@ UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer() KResultOr> PTYMultiplexer::open(int options) { - MutexLocker locker(m_lock); - if (m_freelist.is_empty()) - return EBUSY; - auto master_index = m_freelist.take_last(); - auto master = MasterPTY::try_create(master_index); - if (!master) - return ENOMEM; - dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index()); - auto description = FileDescription::create(*master); - if (!description.is_error()) { - description.value()->set_rw_mode(options); - description.value()->set_file_flags(options); - } - return description; + return m_freelist.with_exclusive([&](auto& freelist) -> KResultOr> { + if (freelist.is_empty()) + return EBUSY; + + auto master_index = freelist.take_last(); + auto master = MasterPTY::try_create(master_index); + if (!master) + return ENOMEM; + dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index()); + auto description = FileDescription::create(*master); + if (!description.is_error()) { + description.value()->set_rw_mode(options); + description.value()->set_file_flags(options); + } + return description; + }); } void PTYMultiplexer::notify_master_destroyed(Badge, unsigned index) { - MutexLocker locker(m_lock); - m_freelist.append(index); - dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index); + m_freelist.with_exclusive([&](auto& freelist) { + freelist.append(index); + dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index); + }); } } diff --git a/Kernel/TTY/PTYMultiplexer.h b/Kernel/TTY/PTYMultiplexer.h index 5f1ee38e3e1..4c7f4892420 100644 --- a/Kernel/TTY/PTYMultiplexer.h +++ b/Kernel/TTY/PTYMultiplexer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -43,10 +43,8 @@ private: // ^CharacterDevice virtual StringView class_name() const override { return "PTYMultiplexer"; } - Mutex m_lock { "PTYMultiplexer" }; - static constexpr size_t max_pty_pairs = 64; - Vector m_freelist; + ProtectedValue> m_freelist; }; }