FIFO.cpp 3.7 KB

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