2020-11-02 11:16:01 -07:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-11-02 11:16:01 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-02 11:16:01 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-19 23:55:44 -07:00
|
|
|
#include <AK/IntrusiveList.h>
|
2022-08-19 20:53:40 +02:00
|
|
|
#include <Kernel/Library/NonnullLockRefPtr.h>
|
2021-09-06 17:22:36 +02:00
|
|
|
#include <Kernel/Memory/ScopedAddressSpaceSwitcher.h>
|
2020-11-02 11:16:01 -07:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class Device;
|
|
|
|
|
2021-02-05 23:36:38 -07:00
|
|
|
extern WorkQueue* g_io_work;
|
|
|
|
|
2022-08-19 17:26:07 +02:00
|
|
|
class AsyncDeviceRequest : public AtomicRefCounted<AsyncDeviceRequest> {
|
2020-11-02 11:16:01 -07:00
|
|
|
AK_MAKE_NONCOPYABLE(AsyncDeviceRequest);
|
|
|
|
AK_MAKE_NONMOVABLE(AsyncDeviceRequest);
|
|
|
|
|
|
|
|
public:
|
2021-04-20 00:03:44 -07:00
|
|
|
enum [[nodiscard]] RequestResult {
|
2020-11-02 11:16:01 -07:00
|
|
|
Pending = 0,
|
|
|
|
Started,
|
|
|
|
Success,
|
|
|
|
Failure,
|
|
|
|
MemoryFault,
|
2022-03-06 22:07:04 +02:00
|
|
|
OutOfMemory,
|
2020-11-02 11:16:01 -07:00
|
|
|
Cancelled
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestWaitResult {
|
|
|
|
friend class AsyncDeviceRequest;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RequestResult request_result() const { return m_request_result; }
|
|
|
|
Thread::BlockResult wait_result() const { return m_wait_result; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RequestWaitResult(RequestResult request_result, Thread::BlockResult wait_result)
|
|
|
|
: m_request_result(request_result)
|
|
|
|
, m_wait_result(wait_result)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestResult m_request_result;
|
|
|
|
Thread::BlockResult m_wait_result;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~AsyncDeviceRequest();
|
|
|
|
|
2021-08-05 20:54:37 +02:00
|
|
|
virtual StringView name() const = 0;
|
2020-11-02 11:16:01 -07:00
|
|
|
virtual void start() = 0;
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
void add_sub_request(NonnullLockRefPtr<AsyncDeviceRequest>);
|
2020-11-02 11:16:01 -07:00
|
|
|
|
2021-02-27 23:56:16 +01:00
|
|
|
[[nodiscard]] RequestWaitResult wait(Time* = nullptr);
|
2020-11-02 11:16:01 -07:00
|
|
|
|
2021-09-05 10:02:03 -07:00
|
|
|
void do_start(SpinlockLocker<Spinlock>&& requests_lock)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
2021-02-06 16:59:32 -07:00
|
|
|
if (is_completed_result(m_result))
|
|
|
|
return;
|
|
|
|
m_result = Started;
|
|
|
|
requests_lock.unlock();
|
|
|
|
|
|
|
|
start();
|
2020-11-02 11:16:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void complete(RequestResult result);
|
|
|
|
|
|
|
|
void set_private(void* priv)
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_private || !priv);
|
2020-11-02 11:16:01 -07:00
|
|
|
m_private = priv;
|
|
|
|
}
|
|
|
|
void* get_private() const { return m_private; }
|
|
|
|
|
|
|
|
template<typename... Args>
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> write_to_buffer(UserOrKernelBuffer& buffer, Args... args)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
if (in_target_context(buffer))
|
|
|
|
return buffer.write(forward<Args>(args)...);
|
2021-09-06 17:22:36 +02:00
|
|
|
ScopedAddressSpaceSwitcher switcher(m_process);
|
2020-11-02 11:16:01 -07:00
|
|
|
return buffer.write(forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t BUFFER_BYTES, typename... Args>
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
if (in_target_context(buffer))
|
|
|
|
return buffer.write_buffered<BUFFER_BYTES>(forward<Args>(args)...);
|
2021-09-06 17:22:36 +02:00
|
|
|
ScopedAddressSpaceSwitcher switcher(m_process);
|
2020-11-02 11:16:01 -07:00
|
|
|
return buffer.write_buffered<BUFFER_BYTES>(forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> read_from_buffer(UserOrKernelBuffer const& buffer, Args... args)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
if (in_target_context(buffer))
|
|
|
|
return buffer.read(forward<Args>(args)...);
|
2021-09-06 17:22:36 +02:00
|
|
|
ScopedAddressSpaceSwitcher switcher(m_process);
|
2020-11-02 11:16:01 -07:00
|
|
|
return buffer.read(forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t BUFFER_BYTES, typename... Args>
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> read_from_buffer_buffered(UserOrKernelBuffer const& buffer, Args... args)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
if (in_target_context(buffer))
|
|
|
|
return buffer.read_buffered<BUFFER_BYTES>(forward<Args>(args)...);
|
2021-09-06 17:22:36 +02:00
|
|
|
ScopedAddressSpaceSwitcher switcher(m_process);
|
2020-11-02 11:16:01 -07:00
|
|
|
return buffer.read_buffered<BUFFER_BYTES>(forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AsyncDeviceRequest(Device&);
|
|
|
|
|
|
|
|
RequestResult get_request_result() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void sub_request_finished(AsyncDeviceRequest&);
|
|
|
|
void request_finished();
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
[[nodiscard]] bool in_target_context(UserOrKernelBuffer const& buffer) const
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
if (buffer.is_kernel_buffer())
|
|
|
|
return true;
|
2021-08-19 22:45:07 +03:00
|
|
|
return m_process == &Process::current();
|
2020-11-02 11:16:01 -07:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:03:44 -07:00
|
|
|
[[nodiscard]] static bool is_completed_result(RequestResult result)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
|
|
|
return result > Started;
|
|
|
|
}
|
|
|
|
|
|
|
|
Device& m_device;
|
|
|
|
|
|
|
|
AsyncDeviceRequest* m_parent_request { nullptr };
|
|
|
|
RequestResult m_result { Pending };
|
2022-08-19 20:53:40 +02:00
|
|
|
IntrusiveListNode<AsyncDeviceRequest, LockRefPtr<AsyncDeviceRequest>> m_list_node;
|
2021-04-19 23:55:44 -07:00
|
|
|
|
2021-09-09 16:30:59 +04:30
|
|
|
using AsyncDeviceSubRequestList = IntrusiveList<&AsyncDeviceRequest::m_list_node>;
|
2021-04-19 23:55:44 -07:00
|
|
|
|
|
|
|
AsyncDeviceSubRequestList m_sub_requests_pending;
|
|
|
|
AsyncDeviceSubRequestList m_sub_requests_complete;
|
2020-11-02 11:16:01 -07:00
|
|
|
WaitQueue m_queue;
|
2022-08-19 20:53:40 +02:00
|
|
|
NonnullLockRefPtr<Process> m_process;
|
2020-11-02 11:16:01 -07:00
|
|
|
void* m_private { nullptr };
|
2022-08-18 21:46:28 +02:00
|
|
|
mutable Spinlock m_lock { LockRank::None };
|
2020-11-02 11:16:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|