FIFO.cpp 3.5 KB

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