FIFO.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <Kernel/FileSystem/FIFO.h>
  10. #include <Kernel/FileSystem/FileDescription.h>
  11. #include <Kernel/Locking/Lockable.h>
  12. #include <Kernel/Locking/Mutex.h>
  13. #include <Kernel/Process.h>
  14. #include <Kernel/Thread.h>
  15. namespace Kernel {
  16. static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;
  17. static Lockable<HashTable<FIFO*>>& all_fifos()
  18. {
  19. return *s_table;
  20. }
  21. static int s_next_fifo_id = 1;
  22. RefPtr<FIFO> FIFO::try_create(uid_t uid)
  23. {
  24. auto buffer = DoubleBuffer::try_create();
  25. if (buffer)
  26. return adopt_ref_if_nonnull(new (nothrow) FIFO(uid, buffer.release_nonnull()));
  27. return {};
  28. }
  29. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction(FIFO::Direction direction)
  30. {
  31. auto description = FileDescription::create(*this);
  32. if (!description.is_error()) {
  33. attach(direction);
  34. description.value()->set_fifo_direction({}, direction);
  35. }
  36. return description;
  37. }
  38. KResultOr<NonnullRefPtr<FileDescription>> FIFO::open_direction_blocking(FIFO::Direction direction)
  39. {
  40. MutexLocker locker(m_open_lock);
  41. auto description = open_direction(direction);
  42. if (description.is_error())
  43. return description;
  44. if (direction == Direction::Reader) {
  45. m_read_open_queue.wake_all();
  46. if (m_writers == 0) {
  47. locker.unlock();
  48. m_write_open_queue.wait_forever("FIFO");
  49. locker.lock();
  50. }
  51. }
  52. if (direction == Direction::Writer) {
  53. m_write_open_queue.wake_all();
  54. if (m_readers == 0) {
  55. locker.unlock();
  56. m_read_open_queue.wait_forever("FIFO");
  57. locker.lock();
  58. }
  59. }
  60. return description;
  61. }
  62. FIFO::FIFO(uid_t uid, NonnullOwnPtr<DoubleBuffer> buffer)
  63. : m_buffer(move(buffer))
  64. , m_uid(uid)
  65. {
  66. MutexLocker locker(all_fifos().lock());
  67. all_fifos().resource().set(this);
  68. m_fifo_id = ++s_next_fifo_id;
  69. // Use the same block condition for read and write
  70. m_buffer->set_unblock_callback([this]() {
  71. evaluate_block_conditions();
  72. });
  73. }
  74. FIFO::~FIFO()
  75. {
  76. MutexLocker locker(all_fifos().lock());
  77. all_fifos().resource().remove(this);
  78. }
  79. void FIFO::attach(Direction direction)
  80. {
  81. if (direction == Direction::Reader) {
  82. ++m_readers;
  83. } else if (direction == Direction::Writer) {
  84. ++m_writers;
  85. }
  86. evaluate_block_conditions();
  87. }
  88. void FIFO::detach(Direction direction)
  89. {
  90. if (direction == Direction::Reader) {
  91. VERIFY(m_readers);
  92. --m_readers;
  93. } else if (direction == Direction::Writer) {
  94. VERIFY(m_writers);
  95. --m_writers;
  96. }
  97. evaluate_block_conditions();
  98. }
  99. bool FIFO::can_read(const FileDescription&, size_t) const
  100. {
  101. return !m_buffer->is_empty() || !m_writers;
  102. }
  103. bool FIFO::can_write(const FileDescription&, size_t) const
  104. {
  105. return m_buffer->space_for_writing() || !m_readers;
  106. }
  107. KResultOr<size_t> FIFO::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
  108. {
  109. if (!m_writers && m_buffer->is_empty())
  110. return 0;
  111. return m_buffer->read(buffer, size);
  112. }
  113. KResultOr<size_t> FIFO::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size)
  114. {
  115. if (!m_readers) {
  116. Thread::current()->send_signal(SIGPIPE, Process::current());
  117. return EPIPE;
  118. }
  119. return m_buffer->write(buffer, size);
  120. }
  121. String FIFO::absolute_path(const FileDescription&) const
  122. {
  123. return String::formatted("fifo:{}", m_fifo_id);
  124. }
  125. KResult FIFO::stat(::stat& st) const
  126. {
  127. memset(&st, 0, sizeof(st));
  128. st.st_mode = S_IFIFO;
  129. return KSuccess;
  130. }
  131. }