|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, The SerenityOS developers. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <Kernel/Devices/AsyncDeviceRequest.h> |
| 28 | +#include <Kernel/Devices/Device.h> |
| 29 | + |
| 30 | +namespace Kernel { |
| 31 | + |
| 32 | +AsyncDeviceRequest::AsyncDeviceRequest(Device& device) |
| 33 | + : m_device(device) |
| 34 | + , m_process(*Process::current()) |
| 35 | +{ |
| 36 | +} |
| 37 | + |
| 38 | +AsyncDeviceRequest::~AsyncDeviceRequest() |
| 39 | +{ |
| 40 | + { |
| 41 | + ScopedSpinLock lock(m_lock); |
| 42 | + ASSERT(is_completed_result(m_result)); |
| 43 | + ASSERT(m_sub_requests_pending.is_empty()); |
| 44 | + } |
| 45 | + |
| 46 | + // We should not need any locking here anymore. The destructor should |
| 47 | + // only be called until either wait() or cancel() (once implemented) returned. |
| 48 | + // At that point no sub-request should be adding more requests and all |
| 49 | + // sub-requests should be completed (either succeeded, failed, or cancelled). |
| 50 | + // Which means there should be no more pending sub-requests and the |
| 51 | + // entire AsyncDeviceRequest hirarchy should be immutable. |
| 52 | + for (auto& sub_request : m_sub_requests_complete) { |
| 53 | + ASSERT(is_completed_result(sub_request.m_result)); // Shouldn't need any locking anymore |
| 54 | + ASSERT(sub_request.m_parent_request == this); |
| 55 | + sub_request.m_parent_request = nullptr; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void AsyncDeviceRequest::request_finished() |
| 60 | +{ |
| 61 | + if (m_parent_request) |
| 62 | + m_parent_request->sub_request_finished(*this); |
| 63 | + |
| 64 | + // Trigger processing the next request |
| 65 | + m_device.process_next_queued_request({}, *this); |
| 66 | + |
| 67 | + // Wake anyone who may be waiting |
| 68 | + m_queue.wake_all(); |
| 69 | +} |
| 70 | + |
| 71 | +auto AsyncDeviceRequest::wait(timeval* timeout) -> RequestWaitResult |
| 72 | +{ |
| 73 | + ASSERT(!m_parent_request); |
| 74 | + auto request_result = get_request_result(); |
| 75 | + if (is_completed_result(request_result)) |
| 76 | + return { request_result, Thread::BlockResult::NotBlocked }; |
| 77 | + auto wait_result = Thread::current()->wait_on(m_queue, name(), timeout); |
| 78 | + return { get_request_result(), wait_result }; |
| 79 | +} |
| 80 | + |
| 81 | +auto AsyncDeviceRequest::get_request_result() const -> RequestResult |
| 82 | +{ |
| 83 | + ScopedSpinLock lock(m_lock); |
| 84 | + return m_result; |
| 85 | +} |
| 86 | + |
| 87 | +void AsyncDeviceRequest::add_sub_request(NonnullRefPtr<AsyncDeviceRequest> sub_request) |
| 88 | +{ |
| 89 | + // Sub-requests cannot be for the same device |
| 90 | + ASSERT(&m_device != &sub_request->m_device); |
| 91 | + ASSERT(sub_request->m_parent_request == nullptr); |
| 92 | + sub_request->m_parent_request = this; |
| 93 | + |
| 94 | + bool should_start; |
| 95 | + { |
| 96 | + ScopedSpinLock lock(m_lock); |
| 97 | + ASSERT(!is_completed_result(m_result)); |
| 98 | + m_sub_requests_pending.append(sub_request); |
| 99 | + should_start = (m_result == Started); |
| 100 | + } |
| 101 | + if (should_start) |
| 102 | + sub_request->do_start(); |
| 103 | +} |
| 104 | + |
| 105 | +void AsyncDeviceRequest::sub_request_finished(AsyncDeviceRequest& sub_request) |
| 106 | +{ |
| 107 | + bool all_completed; |
| 108 | + { |
| 109 | + ScopedSpinLock lock(m_lock); |
| 110 | + ASSERT(m_result == Started); |
| 111 | + size_t index; |
| 112 | + for (index = 0; index < m_sub_requests_pending.size(); index++) { |
| 113 | + if (&m_sub_requests_pending[index] == &sub_request) { |
| 114 | + NonnullRefPtr<AsyncDeviceRequest> request(m_sub_requests_pending[index]); |
| 115 | + m_sub_requests_pending.remove(index); |
| 116 | + m_sub_requests_complete.append(move(request)); |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + ASSERT(index < m_sub_requests_pending.size()); |
| 121 | + all_completed = m_sub_requests_pending.is_empty(); |
| 122 | + if (all_completed) { |
| 123 | + // Aggregate any errors |
| 124 | + bool any_failures = false; |
| 125 | + bool any_memory_faults = false; |
| 126 | + for (index = 0; index < m_sub_requests_complete.size(); index++) { |
| 127 | + auto& sub_request = m_sub_requests_complete[index]; |
| 128 | + auto sub_result = sub_request.get_request_result(); |
| 129 | + ASSERT(is_completed_result(sub_result)); |
| 130 | + switch (sub_result) { |
| 131 | + case Failure: |
| 132 | + any_failures = true; |
| 133 | + break; |
| 134 | + case MemoryFault: |
| 135 | + any_memory_faults = true; |
| 136 | + break; |
| 137 | + default: |
| 138 | + break; |
| 139 | + } |
| 140 | + if (any_failures && any_memory_faults) |
| 141 | + break; // Stop checking if all error conditions were found |
| 142 | + } |
| 143 | + if (any_failures) |
| 144 | + m_result = Failure; |
| 145 | + else if (any_memory_faults) |
| 146 | + m_result = MemoryFault; |
| 147 | + else |
| 148 | + m_result = Success; |
| 149 | + } |
| 150 | + } |
| 151 | + if (all_completed) |
| 152 | + request_finished(); |
| 153 | +} |
| 154 | + |
| 155 | +void AsyncDeviceRequest::complete(RequestResult result) |
| 156 | +{ |
| 157 | + ASSERT(result == Success || result == Failure || result == MemoryFault); |
| 158 | + ScopedCritical critical; |
| 159 | + { |
| 160 | + ScopedSpinLock lock(m_lock); |
| 161 | + ASSERT(m_result == Started); |
| 162 | + m_result = result; |
| 163 | + } |
| 164 | + if (Processor::current().in_irq()) { |
| 165 | + ref(); // Make sure we don't get freed |
| 166 | + Processor::deferred_call_queue([this]() { |
| 167 | + request_finished(); |
| 168 | + unref(); |
| 169 | + }); |
| 170 | + } else { |
| 171 | + request_finished(); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +} |
0 commit comments