FIFO.cpp 3.4 KB

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