FIFO.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <AK/HashTable.h>
  2. #include <AK/StdLibExtras.h>
  3. #include <Kernel/FileSystem/FIFO.h>
  4. #include <Kernel/FileSystem/FileDescription.h>
  5. #include <Kernel/Lock.h>
  6. #include <Kernel/Process.h>
  7. #include <Kernel/Thread.h>
  8. //#define FIFO_DEBUG
  9. Lockable<HashTable<FIFO*>>& all_fifos()
  10. {
  11. static Lockable<HashTable<FIFO*>>* s_table;
  12. if (!s_table)
  13. s_table = new Lockable<HashTable<FIFO*>>;
  14. return *s_table;
  15. }
  16. RefPtr<FIFO> FIFO::from_fifo_id(u32 id)
  17. {
  18. auto* ptr = reinterpret_cast<FIFO*>(id);
  19. LOCKER(all_fifos().lock());
  20. if (auto it = all_fifos().resource().find(ptr); it == all_fifos().resource().end())
  21. return nullptr;
  22. return ptr;
  23. }
  24. NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
  25. {
  26. return adopt(*new FIFO(uid));
  27. }
  28. NonnullRefPtr<FileDescription> FIFO::open_direction(FIFO::Direction direction)
  29. {
  30. auto description = FileDescription::create(*this);
  31. attach(direction);
  32. description->set_fifo_direction({}, direction);
  33. return description;
  34. }
  35. FIFO::FIFO(uid_t uid)
  36. : m_uid(uid)
  37. {
  38. LOCKER(all_fifos().lock());
  39. all_fifos().resource().set(this);
  40. }
  41. FIFO::~FIFO()
  42. {
  43. LOCKER(all_fifos().lock());
  44. all_fifos().resource().remove(this);
  45. }
  46. void FIFO::attach(Direction direction)
  47. {
  48. if (direction == Direction::Reader) {
  49. ++m_readers;
  50. #ifdef FIFO_DEBUG
  51. kprintf("open reader (%u)\n", m_readers);
  52. #endif
  53. } else if (direction == Direction::Writer) {
  54. ++m_writers;
  55. #ifdef FIFO_DEBUG
  56. kprintf("open writer (%u)\n", m_writers);
  57. #endif
  58. }
  59. }
  60. void FIFO::detach(Direction direction)
  61. {
  62. if (direction == Direction::Reader) {
  63. #ifdef FIFO_DEBUG
  64. kprintf("close reader (%u - 1)\n", m_readers);
  65. #endif
  66. ASSERT(m_readers);
  67. --m_readers;
  68. } else if (direction == Direction::Writer) {
  69. #ifdef FIFO_DEBUG
  70. kprintf("close writer (%u - 1)\n", m_writers);
  71. #endif
  72. ASSERT(m_writers);
  73. --m_writers;
  74. }
  75. }
  76. bool FIFO::can_read(FileDescription&) const
  77. {
  78. return !m_buffer.is_empty() || !m_writers;
  79. }
  80. bool FIFO::can_write(FileDescription&) const
  81. {
  82. return m_buffer.bytes_in_write_buffer() < 4096 || !m_readers;
  83. }
  84. ssize_t FIFO::read(FileDescription&, u8* buffer, ssize_t size)
  85. {
  86. if (!m_writers && m_buffer.is_empty())
  87. return 0;
  88. #ifdef FIFO_DEBUG
  89. dbgprintf("fifo: read(%u)\n", size);
  90. #endif
  91. ssize_t nread = m_buffer.read(buffer, size);
  92. #ifdef FIFO_DEBUG
  93. dbgprintf(" -> read (%c) %u\n", buffer[0], nread);
  94. #endif
  95. return nread;
  96. }
  97. ssize_t FIFO::write(FileDescription&, const u8* buffer, ssize_t size)
  98. {
  99. if (!m_readers) {
  100. current->process().send_signal(SIGPIPE, &current->process());
  101. return -EPIPE;
  102. }
  103. #ifdef FIFO_DEBUG
  104. dbgprintf("fifo: write(%p, %u)\n", buffer, size);
  105. #endif
  106. return m_buffer.write(buffer, size);
  107. }
  108. String FIFO::absolute_path(const FileDescription&) const
  109. {
  110. return String::format("fifo:%u", this);
  111. }