Let each MasterPTY create its slave.

This commit is contained in:
Andreas Kling 2019-01-16 02:11:50 +01:00
parent 9c51d9dfcd
commit 310a5f4199
Notes: sideshowbarker 2024-07-19 16:01:39 +09:00
5 changed files with 18 additions and 30 deletions

View file

@ -3,9 +3,10 @@
MasterPTY::MasterPTY(unsigned index)
: CharacterDevice(10, index)
, m_slave(*new SlavePTY(*this, index))
, m_index(index)
{
VFS::the().register_character_device(m_slave);
}
MasterPTY::~MasterPTY()
@ -14,7 +15,6 @@ MasterPTY::~MasterPTY()
String MasterPTY::pts_name() const
{
dbgprintf("MasterPTY::pts_name requested for index %u!\n", m_index);
char buffer[32];
ksprintf(buffer, "/dev/pts%u", m_index);
return buffer;
@ -27,7 +27,7 @@ ssize_t MasterPTY::read(Process&, byte* buffer, size_t size)
ssize_t MasterPTY::write(Process&, const byte* buffer, size_t size)
{
m_slave->on_master_write(buffer, size);
m_slave.on_master_write(buffer, size);
return size;
}

View file

@ -6,10 +6,10 @@
class SlavePTY;
class MasterPTY final : public CharacterDevice {
AK_MAKE_ETERNAL
public:
explicit MasterPTY(unsigned index);
virtual ~MasterPTY() override;
void set_slave(SlavePTY& slave) { m_slave = &slave; }
virtual ssize_t read(Process&, byte*, size_t) override;
virtual ssize_t write(Process&, const byte*, size_t) override;
@ -21,7 +21,7 @@ public:
void on_slave_write(const byte*, size_t);
private:
SlavePTY& m_slave;
unsigned m_index;
SlavePTY* m_slave { nullptr };
DoubleBuffer m_buffer;
};

View file

@ -1,8 +1,9 @@
#include "SlavePTY.h"
#include "MasterPTY.h"
SlavePTY::SlavePTY(unsigned index)
SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
: TTY(11, index)
, m_master(master)
, m_index(index)
{
set_size(80, 25);
@ -27,10 +28,10 @@ void SlavePTY::on_master_write(const byte* buffer, size_t size)
void SlavePTY::on_tty_write(const byte* data, size_t size)
{
m_master->on_slave_write(data, size);
m_master.on_slave_write(data, size);
}
bool SlavePTY::can_write(Process& process) const
{
return m_master->can_write(process);
return m_master.can_write(process);
}

View file

@ -5,10 +5,9 @@
class MasterPTY;
class SlavePTY final : public TTY {
AK_MAKE_ETERNAL
public:
explicit SlavePTY(unsigned index);
virtual ~SlavePTY() override;
void set_master(MasterPTY& master) { m_master = &master; }
virtual String tty_name() const override;
@ -19,7 +18,10 @@ protected:
virtual bool can_write(Process&) const override;
private:
friend class MasterPTY;
SlavePTY(MasterPTY&, unsigned index);
MasterPTY& m_master;
unsigned m_index;
MasterPTY* m_master { nullptr };
};

View file

@ -23,7 +23,6 @@
#include "Scheduler.h"
#include "PS2MouseDevice.h"
#include "MasterPTY.h"
#include "SlavePTY.h"
#define SPAWN_GUI_TEST_APP
//#define SPAWN_MULTIPLE_SHELLS
@ -42,10 +41,6 @@ MasterPTY* ptm0;
MasterPTY* ptm1;
MasterPTY* ptm2;
MasterPTY* ptm3;
SlavePTY* pts0;
SlavePTY* pts1;
SlavePTY* pts2;
SlavePTY* pts3;
#ifdef STRESS_TEST_SPAWNING
static void spawn_stress() NORETURN;
@ -66,16 +61,6 @@ static void spawn_stress()
}
#endif
static void make_pty_pair(unsigned index)
{
auto* master = new MasterPTY(index);
auto* slave = new SlavePTY(index);
master->set_slave(*slave);
slave->set_master(*master);
VFS::the().register_character_device(*master);
VFS::the().register_character_device(*slave);
}
static void init_stage2() NORETURN;
static void init_stage2()
{
@ -95,10 +80,10 @@ static void init_stage2()
auto dev_random = make<RandomDevice>();
vfs->register_character_device(*dev_random);
make_pty_pair(0);
make_pty_pair(1);
make_pty_pair(2);
make_pty_pair(3);
VFS::the().register_character_device(*new MasterPTY(0));
VFS::the().register_character_device(*new MasterPTY(1));
VFS::the().register_character_device(*new MasterPTY(2));
VFS::the().register_character_device(*new MasterPTY(3));
vfs->register_character_device(*keyboard);
vfs->register_character_device(*ps2mouse);