FIFO.cpp 3.6 KB

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