serenity/Kernel/FileSystem/FIFO.cpp

156 lines
3.4 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashTable.h>
#include <AK/Singleton.h>
#include <AK/StdLibExtras.h>
#include <Kernel/FileSystem/FIFO.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Mutex.h>
#include <Kernel/Process.h>
#include <Kernel/Thread.h>
namespace Kernel {
static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;
Kernel: Mark compilation-unit-only functions as static This enables a nice warning in case a function becomes dead code. Also, in case of signal_trampoline_dummy, marking it external (non-static) prevents it from being 'optimized away', which would lead to surprising and weird linker errors. I found these places by using -Wmissing-declarations. The Kernel still shows these issues, which I think are false-positives, but don't want to touch: - Kernel/Arch/i386/CPU.cpp:1081:17: void Kernel::enter_thread_context(Kernel::Thread*, Kernel::Thread*) - Kernel/Arch/i386/CPU.cpp:1170:17: void Kernel::context_first_init(Kernel::Thread*, Kernel::Thread*, Kernel::TrapFrame*) - Kernel/Arch/i386/CPU.cpp:1304:16: u32 Kernel::do_init_context(Kernel::Thread*, u32) - Kernel/Arch/i386/CPU.cpp:1347:17: void Kernel::pre_init_finished() - Kernel/Arch/i386/CPU.cpp:1360:17: void Kernel::post_init_finished() No idea, not gonna touch it. - Kernel/init.cpp:104:30: void Kernel::init() - Kernel/init.cpp:167:30: void Kernel::init_ap(u32, Kernel::Processor*) - Kernel/init.cpp:184:17: void Kernel::init_finished(u32) Called by boot.S. - Kernel/init.cpp:383:16: int Kernel::__cxa_atexit(void (*)(void*), void*, void*) - Kernel/StdLib.cpp:285:19: void __cxa_pure_virtual() - Kernel/StdLib.cpp:300:19: void __stack_chk_fail() - Kernel/StdLib.cpp:305:19: void __stack_chk_fail_local() Not sure how to tell the compiler that the compiler is already using them. Also, maybe __cxa_atexit should go into StdLib.cpp? - Kernel/Modules/TestModule.cpp:31:17: void module_init() - Kernel/Modules/TestModule.cpp:40:17: void module_fini() Could maybe go into a new header. This would also provide type-checking for new modules.
2020-08-10 21:12:13 +02:00
static Lockable<HashTable<FIFO*>>& all_fifos()
{
return *s_table;
}
static int s_next_fifo_id = 1;
NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
{
return adopt_ref(*new FIFO(uid));
}
KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction(FIFO::Direction direction)
2019-04-29 04:55:54 +02:00
{
auto description = FileDescription::create(*this);
if (!description.is_error()) {
attach(direction);
description.value()->set_fifo_direction({}, direction);
}
return description;
2019-04-29 04:55:54 +02:00
}
KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction_blocking(FIFO::Direction direction)
2020-07-16 15:23:03 -06:00
{
Locker locker(m_open_lock);
auto description = open_direction(direction);
if (description.is_error())
return description;
2020-07-16 15:23:03 -06:00
if (direction == Direction::Reader) {
m_read_open_queue.wake_all();
if (m_writers == 0) {
locker.unlock();
m_write_open_queue.wait_forever("FIFO");
2020-07-16 15:23:03 -06:00
locker.lock();
}
}
if (direction == Direction::Writer) {
m_write_open_queue.wake_all();
if (m_readers == 0) {
locker.unlock();
m_read_open_queue.wait_forever("FIFO");
2020-07-16 15:23:03 -06:00
locker.lock();
}
}
return description;
}
FIFO::FIFO(uid_t uid)
: m_uid(uid)
{
Locker locker(all_fifos().lock());
all_fifos().resource().set(this);
m_fifo_id = ++s_next_fifo_id;
// Use the same block condition for read and write
m_buffer.set_unblock_callback([this]() {
evaluate_block_conditions();
});
}
FIFO::~FIFO()
{
Locker locker(all_fifos().lock());
all_fifos().resource().remove(this);
}
2019-04-29 04:55:54 +02:00
void FIFO::attach(Direction direction)
{
if (direction == Direction::Reader) {
++m_readers;
} else if (direction == Direction::Writer) {
++m_writers;
}
evaluate_block_conditions();
}
2019-04-29 04:55:54 +02:00
void FIFO::detach(Direction direction)
{
if (direction == Direction::Reader) {
VERIFY(m_readers);
--m_readers;
} else if (direction == Direction::Writer) {
VERIFY(m_writers);
--m_writers;
}
evaluate_block_conditions();
}
bool FIFO::can_read(const FileDescription&, size_t) const
{
return !m_buffer.is_empty() || !m_writers;
}
bool FIFO::can_write(const FileDescription&, size_t) const
{
return m_buffer.space_for_writing() || !m_readers;
}
KResultOr<size_t> FIFO::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
if (!m_writers && m_buffer.is_empty())
return 0;
return m_buffer.read(buffer, size);
}
KResultOr<size_t> FIFO::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size)
{
if (!m_readers) {
Thread::current()->send_signal(SIGPIPE, Process::current());
return EPIPE;
}
return m_buffer.write(buffer, size);
}
2019-04-29 04:55:54 +02:00
String FIFO::absolute_path(const FileDescription&) const
2019-04-29 04:55:54 +02:00
{
return String::formatted("fifo:{}", m_fifo_id);
2019-04-29 04:55:54 +02:00
}
KResult FIFO::stat(::stat& st) const
{
memset(&st, 0, sizeof(st));
st.st_mode = S_IFIFO;
return KSuccess;
}
}