FIFO.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. static int s_next_fifo_id = 1;
  17. NonnullRefPtr<FIFO> FIFO::create(uid_t uid)
  18. {
  19. return adopt(*new FIFO(uid));
  20. }
  21. NonnullRefPtr<FileDescription> FIFO::open_direction(FIFO::Direction direction)
  22. {
  23. auto description = FileDescription::create(*this);
  24. attach(direction);
  25. description->set_fifo_direction({}, direction);
  26. return description;
  27. }
  28. FIFO::FIFO(uid_t uid)
  29. : m_uid(uid)
  30. {
  31. LOCKER(all_fifos().lock());
  32. all_fifos().resource().set(this);
  33. m_fifo_id = ++s_next_fifo_id;
  34. }
  35. FIFO::~FIFO()
  36. {
  37. LOCKER(all_fifos().lock());
  38. all_fifos().resource().remove(this);
  39. }
  40. void FIFO::attach(Direction direction)
  41. {
  42. if (direction == Direction::Reader) {
  43. ++m_readers;
  44. #ifdef FIFO_DEBUG
  45. kprintf("open reader (%u)\n", m_readers);
  46. #endif
  47. } else if (direction == Direction::Writer) {
  48. ++m_writers;
  49. #ifdef FIFO_DEBUG
  50. kprintf("open writer (%u)\n", m_writers);
  51. #endif
  52. }
  53. }
  54. void FIFO::detach(Direction direction)
  55. {
  56. if (direction == Direction::Reader) {
  57. #ifdef FIFO_DEBUG
  58. kprintf("close reader (%u - 1)\n", m_readers);
  59. #endif
  60. ASSERT(m_readers);
  61. --m_readers;
  62. } else if (direction == Direction::Writer) {
  63. #ifdef FIFO_DEBUG
  64. kprintf("close writer (%u - 1)\n", m_writers);
  65. #endif
  66. ASSERT(m_writers);
  67. --m_writers;
  68. }
  69. }
  70. bool FIFO::can_read(const FileDescription&) const
  71. {
  72. return !m_buffer.is_empty() || !m_writers;
  73. }
  74. bool FIFO::can_write(const FileDescription&) const
  75. {
  76. return m_buffer.bytes_in_write_buffer() < 4096 || !m_readers;
  77. }
  78. ssize_t FIFO::read(FileDescription&, u8* buffer, ssize_t size)
  79. {
  80. if (!m_writers && m_buffer.is_empty())
  81. return 0;
  82. #ifdef FIFO_DEBUG
  83. dbgprintf("fifo: read(%u)\n", size);
  84. #endif
  85. ssize_t nread = m_buffer.read(buffer, size);
  86. #ifdef FIFO_DEBUG
  87. dbgprintf(" -> read (%c) %u\n", buffer[0], nread);
  88. #endif
  89. return nread;
  90. }
  91. ssize_t FIFO::write(FileDescription&, const u8* buffer, ssize_t size)
  92. {
  93. if (!m_readers) {
  94. current->process().send_signal(SIGPIPE, &current->process());
  95. return -EPIPE;
  96. }
  97. #ifdef FIFO_DEBUG
  98. dbgprintf("fifo: write(%p, %u)\n", buffer, size);
  99. #endif
  100. return m_buffer.write(buffer, size);
  101. }
  102. String FIFO::absolute_path(const FileDescription&) const
  103. {
  104. return String::format("fifo:%u", m_fifo_id);
  105. }