2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-11-12 01:28:46 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-29 04:55:54 +02:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-07-09 14:46:23 +02:00
|
|
|
#include <Kernel/FileSystem/File.h>
|
2021-07-18 09:10:27 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2020-07-16 15:23:03 -06:00
|
|
|
#include <Kernel/WaitQueue.h>
|
2018-11-12 01:28:46 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
class OpenFileDescription;
|
2019-04-29 04:55:54 +02:00
|
|
|
|
|
|
|
class FIFO final : public File {
|
2018-11-12 01:28:46 +01:00
|
|
|
public:
|
2019-07-03 21:17:35 +02:00
|
|
|
enum class Direction : u8 {
|
2019-05-28 11:53:16 +02:00
|
|
|
Neither,
|
|
|
|
Reader,
|
|
|
|
Writer
|
2018-11-12 01:28:46 +01:00
|
|
|
};
|
|
|
|
|
2023-03-10 07:53:02 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<FIFO>> try_create(UserID);
|
2019-04-29 04:55:54 +02:00
|
|
|
virtual ~FIFO() override;
|
2019-04-25 13:52:07 +02:00
|
|
|
|
2021-08-28 22:11:16 +02:00
|
|
|
UserID uid() const { return m_uid; }
|
2018-11-12 01:28:46 +01:00
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> open_direction(Direction);
|
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> open_direction_blocking(Direction);
|
2018-11-12 01:28:46 +01:00
|
|
|
|
2021-06-24 14:04:41 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
|
2019-04-29 04:55:54 +02:00
|
|
|
void attach(Direction);
|
|
|
|
void detach(Direction);
|
2021-06-24 14:04:41 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2018-11-12 01:28:46 +01:00
|
|
|
|
|
|
|
private:
|
2019-04-29 04:55:54 +02:00
|
|
|
// ^File
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
2021-12-17 11:22:27 +01:00
|
|
|
virtual ErrorOr<struct stat> stat() const override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const override;
|
2021-10-02 15:24:00 -07:00
|
|
|
virtual StringView class_name() const override { return "FIFO"sv; }
|
2019-04-29 04:55:54 +02:00
|
|
|
virtual bool is_fifo() const override { return true; }
|
|
|
|
|
2021-08-28 22:11:16 +02:00
|
|
|
explicit FIFO(UserID, NonnullOwnPtr<DoubleBuffer> buffer);
|
2018-11-12 01:28:46 +01:00
|
|
|
|
|
|
|
unsigned m_writers { 0 };
|
|
|
|
unsigned m_readers { 0 };
|
2021-08-01 02:30:52 -07:00
|
|
|
NonnullOwnPtr<DoubleBuffer> m_buffer;
|
2019-04-25 13:52:07 +02:00
|
|
|
|
2021-08-28 22:11:16 +02:00
|
|
|
UserID m_uid { 0 };
|
2020-01-07 07:29:50 +01:00
|
|
|
|
|
|
|
int m_fifo_id { 0 };
|
2020-07-16 15:23:03 -06:00
|
|
|
|
|
|
|
WaitQueue m_read_open_queue;
|
|
|
|
WaitQueue m_write_open_queue;
|
2021-07-17 21:09:51 +02:00
|
|
|
Mutex m_open_lock;
|
2018-11-12 01:28:46 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|