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);
m_freelist.with_exclusive([&](auto& freelist) {
freelist.ensure_capacity(max_pty_pairs);
for (int i = max_pty_pairs; i > 0; --i)
m_freelist.unchecked_append(i - 1);
freelist.unchecked_append(i - 1);
});
}
UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
@ -35,10 +37,11 @@ UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options)
{
MutexLocker locker(m_lock);
if (m_freelist.is_empty())
return m_freelist.with_exclusive([&](auto& freelist) -> KResultOr<NonnullRefPtr<FileDescription>> {
if (freelist.is_empty())
return EBUSY;
auto master_index = m_freelist.take_last();
auto master_index = freelist.take_last();
auto master = MasterPTY::try_create(master_index);
if (!master)
return ENOMEM;
@ -49,13 +52,15 @@ KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int 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);
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;
};
}