Kernel: Port PTYMultiplexer to ProtectedValue

This commit is contained in:
Andreas Kling 2021-08-07 15:18:14 +02:00
parent 7f2791f02e
commit 4c582b57e9
Notes: sideshowbarker 2024-07-18 07:18:26 +09:00
2 changed files with 27 additions and 24 deletions

View file

@ -24,9 +24,11 @@ PTYMultiplexer& PTYMultiplexer::the()
UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer() UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer()
: CharacterDevice(5, 2) : CharacterDevice(5, 2)
{ {
m_freelist.ensure_capacity(max_pty_pairs); m_freelist.with_exclusive([&](auto& freelist) {
for (int i = max_pty_pairs; i > 0; --i) freelist.ensure_capacity(max_pty_pairs);
m_freelist.unchecked_append(i - 1); for (int i = max_pty_pairs; i > 0; --i)
freelist.unchecked_append(i - 1);
});
} }
UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer() UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
@ -35,27 +37,30 @@ UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options) KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options)
{ {
MutexLocker locker(m_lock); return m_freelist.with_exclusive([&](auto& freelist) -> KResultOr<NonnullRefPtr<FileDescription>> {
if (m_freelist.is_empty()) if (freelist.is_empty())
return EBUSY; return EBUSY;
auto master_index = m_freelist.take_last();
auto master = MasterPTY::try_create(master_index); auto master_index = freelist.take_last();
if (!master) auto master = MasterPTY::try_create(master_index);
return ENOMEM; if (!master)
dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index()); return ENOMEM;
auto description = FileDescription::create(*master); dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index());
if (!description.is_error()) { auto description = FileDescription::create(*master);
description.value()->set_rw_mode(options); if (!description.is_error()) {
description.value()->set_file_flags(options); description.value()->set_rw_mode(options);
} description.value()->set_file_flags(options);
return description; }
return description;
});
} }
void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index) void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index)
{ {
MutexLocker locker(m_lock); m_freelist.with_exclusive([&](auto& freelist) {
m_freelist.append(index); freelist.append(index);
dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index); dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index);
});
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -43,10 +43,8 @@ private:
// ^CharacterDevice // ^CharacterDevice
virtual StringView class_name() const override { return "PTYMultiplexer"; } virtual StringView class_name() const override { return "PTYMultiplexer"; }
Mutex m_lock { "PTYMultiplexer" };
static constexpr size_t max_pty_pairs = 64; static constexpr size_t max_pty_pairs = 64;
Vector<unsigned, max_pty_pairs> m_freelist; ProtectedValue<Vector<unsigned, max_pty_pairs>> m_freelist;
}; };
} }