| /* |
| * Copyright © 2019 Google, LLC |
| * |
| * Permission is hereby granted, free of charge, to any person obtaining a |
| * copy of this software and associated documentation files (the "Software"), |
| * to deal in the Software without restriction, including without limitation |
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| * and/or sell copies of the Software, and to permit persons to whom the |
| * Software is furnished to do so, subject to the following conditions: |
| * |
| * The above copyright notice and this permission notice (including the next |
| * paragraph) shall be included in all copies or substantial portions of the |
| * Software. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| * IN THE SOFTWARE. |
| */ |
| |
| #ifndef INFLIGHT_LIST_H |
| #define INFLIGHT_LIST_H |
| |
| #include <assert.h> |
| #include <lib/magma/magma.h> |
| #include <stdbool.h> |
| #include <stdio.h> |
| |
| #include <deque> |
| #include <mutex> |
| |
| // A convenience utility for maintaining a list of inflight command buffers, |
| // by reading completed buffer ids from the magma notification channel. |
| |
| typedef magma_status_t (*wait_notification_channel_t)(magma_handle_t channel, int64_t timeout_ns); |
| |
| typedef magma_status_t (*read_notification_channel_t)(magma_connection_t connection, void* buffer, |
| uint64_t buffer_size, |
| uint64_t* buffer_size_out, |
| magma_bool_t* more_data_out); |
| |
| class InflightList { |
| public: |
| InflightList(); |
| ~InflightList(); |
| |
| void add(uint64_t buffer_id); |
| bool remove(uint64_t buffer_id); |
| |
| bool is_inflight(uint64_t buffer_id); |
| |
| void update(magma_connection_t connection); |
| |
| // Returns true if the list lock was obtained. Threadsafe. |
| bool TryUpdate(magma_connection_t connection); |
| |
| // Wait for the given |buffer_id| to be removed from the inflight list. Threadsafe. |
| magma_status_t WaitForBuffer(magma_connection_t connection, |
| magma_handle_t notification_channel, uint64_t buffer_id, |
| uint64_t timeout_ns); |
| |
| // Adds the given buffers to the inflight list and services the notification channel. Threadsafe. |
| void AddAndUpdate(magma_connection_t connection, |
| struct magma_exec_resource* resources, uint32_t count); |
| |
| private: |
| wait_notification_channel_t wait_; |
| read_notification_channel_t read_; |
| |
| std::deque<uint64_t> buffers_; |
| std::mutex mutex_; |
| uint64_t notification_buffer[4096 / sizeof(uint64_t)]; |
| }; |
| |
| #endif // INFLIGHT_LIST_H |