mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 10:12:25 -05:00
b91c49364d
This makes it more symmetrical with adopt_own() (which is used to create a NonnullOwnPtr from the result of a naked new.)
60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "PTYMultiplexer.h"
|
|
#include "MasterPTY.h"
|
|
#include <AK/Singleton.h>
|
|
#include <Kernel/Debug.h>
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
#include <Kernel/Process.h>
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
namespace Kernel {
|
|
|
|
static const unsigned s_max_pty_pairs = 8;
|
|
static AK::Singleton<PTYMultiplexer> s_the;
|
|
|
|
PTYMultiplexer& PTYMultiplexer::the()
|
|
{
|
|
return *s_the;
|
|
}
|
|
|
|
UNMAP_AFTER_INIT PTYMultiplexer::PTYMultiplexer()
|
|
: CharacterDevice(5, 2)
|
|
{
|
|
m_freelist.ensure_capacity(s_max_pty_pairs);
|
|
for (int i = s_max_pty_pairs; i > 0; --i)
|
|
m_freelist.unchecked_append(i - 1);
|
|
}
|
|
|
|
UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
|
|
{
|
|
}
|
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options)
|
|
{
|
|
LOCKER(m_lock);
|
|
if (m_freelist.is_empty())
|
|
return EBUSY;
|
|
auto master_index = m_freelist.take_last();
|
|
auto master = adopt_ref(*new MasterPTY(master_index));
|
|
dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index());
|
|
auto description = FileDescription::create(move(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)
|
|
{
|
|
LOCKER(m_lock);
|
|
m_freelist.append(index);
|
|
dbgln_if(PTMX_DEBUG, "PTYMultiplexer: {} added to freelist", index);
|
|
}
|
|
|
|
}
|