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()
: 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<NonnullRefPtr<FileDescription>> 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<NonnullRefPtr<FileDescription>> {
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<MasterPTY>, 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);
});
}
}

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
*/
@ -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<unsigned, max_pty_pairs> m_freelist;
ProtectedValue<Vector<unsigned, max_pty_pairs>> m_freelist;
};
}