FIFO.cpp 3.4 KB

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