Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

chore(f1): delete dead async loader orphans (LoaderJobQueue, AsyncHandle)

esengine37653f2788d1
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6b52d9979..f595e4371 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -234,7 +234,6 @@ else()
# sources a native tooling/test build still needs.
list(APPEND ESENGINE_SOURCES
src/esengine/renderer/stb_image_impl.cpp
- src/esengine/resource/LoaderJobQueue.cpp
src/esengine/resource/HotReloadManager.cpp
src/esengine/resource/loaders/ShaderLoader.cpp
)
diff --git a/src/esengine/resource/AsyncHandle.hpp b/src/esengine/resource/AsyncHandle.hpp
deleted file mode 100644
index 4713d7011..000000000
--- a/src/esengine/resource/AsyncHandle.hpp
+++ /dev/null
@@ -1,163 +0,0 @@
-/**
- * @file AsyncHandle.hpp
- * @brief Asynchronous resource handle
- * @details Tracks the loading state of an async resource request and provides
- * access to the loaded resource once complete.
- *
- * @author ESEngine Team
- * @date 2026
- *
- * @copyright Copyright (c) 2026 ESEngine Team
- * Licensed under the MIT License.
- */
-#pragma once
-
-// =============================================================================
-// Includes
-// =============================================================================
-
-#include "../core/Types.hpp"
-#include "Handle.hpp"
-
-#include <functional>
-#include <atomic>
-#include <mutex>
-#include <vector>
-
-namespace esengine::resource {
-
-// =============================================================================
-// Load State
-// =============================================================================
-
-enum class LoadState : u8 {
- NotStarted, ///< Loading has not begun
- Loading, ///< Resource is being loaded
- Ready, ///< Resource loaded successfully
- Failed ///< Loading failed
-};
-
-// =============================================================================
-// AsyncHandle Class
-// =============================================================================
-
-/**
- * @brief Handle for tracking async resource loading
- *
- * @details Wraps a Handle<T> with loading state tracking and completion
- * callbacks. Use with ResourceManager::loadAsync<T>().
- *
- * @tparam T The resource type being loaded
- *
- * @code
- * auto asyncTex = rm.loadAsync<Texture>("large_texture.png");
- *
- * asyncTex.onComplete([](TextureHandle h, bool success) {
- * if (success) {
- * // Use the texture
- * }
- * });
- *
- * // Later in update loop
- * if (asyncTex.isReady()) {
- * TextureHandle handle = asyncTex.handle();
- * }
- * @endcode
- */
-template<typename T>
-class AsyncHandle {
-public:
- AsyncHandle() = default;
-
- /**
- * @brief Constructs an AsyncHandle with a job ID
- * @param jobId The job ID from LoaderJobQueue
- */
- explicit AsyncHandle(u64 jobId) : jobId_(jobId), state_(LoadState::Loading) {}
-
- /** @brief Gets the underlying resource handle (valid only when ready) */
- Handle<T> handle() const {
- std::lock_guard<std::mutex> lock(mutex_);
- return handle_;
- }
-
- /** @brief Gets the current loading state */
- LoadState state() const { return state_.load(); }
-
- /** @brief Checks if the resource is ready to use */
- bool isReady() const { return state_.load() == LoadState::Ready; }
-
- /** @brief Checks if the resource is currently loading */
- bool isLoading() const { return state_.load() == LoadState::Loading; }
-
- /** @brief Checks if loading failed */
- bool isFailed() const { return state_.load() == LoadState::Failed; }
-
- /** @brief Gets the job ID for this async operation */
- u64 jobId() const { return jobId_; }
-
- /** @brief Gets the error message if loading failed */
- const std::string& errorMessage() const {
- std::lock_guard<std::mutex> lock(mutex_);
- return errorMessage_;
- }
-
- /**
- * @brief Registers a completion callback
- * @param callback Function called when loading completes (handle, success)
- */
- void onComplete(std::function<void(Handle<T>, bool)> callback) {
- std::lock_guard<std::mutex> lock(mutex_);
- if (state_.load() == LoadState::Ready || state_.load() == LoadState::Failed) {
- callback(handle_, state_.load() == LoadState::Ready);
- } else {
- callbacks_.push_back(std::move(callback));
- }
- }
-
- /**
- * @brief Sets the result (called internally by ResourceManager)
- * @param h The loaded handle
- * @param success Whether loading succeeded
- * @param error Error message if failed
- */
- void setResult(Handle<T> h, bool success, const std::string& error = "") {
- std::vector<std::function<void(Handle<T>, bool)>> callbacksCopy;
- {
- std::lock_guard<std::mutex> lock(mutex_);
- handle_ = h;
- errorMessage_ = error;
- state_.store(success ? LoadState::Ready : LoadState::Failed);
- callbacksCopy = std::move(callbacks_);
- }
-
- for (auto& cb : callbacksCopy) {
- cb(h, success);
- }
- }
-
- /** @brief Explicit bool conversion (true if ready with valid handle) */
- explicit operator bool() const {
- return isReady() && handle().isValid();
- }
-
-private:
- u64 jobId_ = 0;
- std::atomic<LoadState> state_{LoadState::NotStarted};
- mutable std::mutex mutex_;
- Handle<T> handle_;
- std::string errorMessage_;
- std::vector<std::function<void(Handle<T>, bool)>> callbacks_;
-};
-
-// =============================================================================
-// Type Aliases
-// =============================================================================
-
-class Shader;
-class Texture;
-
-using AsyncShaderHandle = AsyncHandle<Shader>;
-using AsyncTextureHandle = AsyncHandle<Texture>;
-
-} // namespace esengine::resource
diff --git a/src/esengine/resource/LoaderJobQueue.cpp b/src/esengine/resource/LoaderJobQueue.cpp
deleted file mode 100644
index 9b3120144..000000000
--- a/src/esengine/resource/LoaderJobQueue.cpp
+++ /dev/null
@@ -1,233 +0,0 @@
-/**
- * @file LoaderJobQueue.cpp
- * @brief Async resource loading job queue implementation
- *
- * @author ESEngine Team
- * @date 2026
- *
- * @copyright Copyright (c) 2026 ESEngine Team
- * Licensed under the MIT License.
- */
-
-#include "LoaderJobQueue.hpp"
-#include "../core/Log.hpp"
-
-namespace esengine::resource {
-
-// =============================================================================
-// Lifecycle
-// =============================================================================
-
-LoaderJobQueue::~LoaderJobQueue() {
- if (initialized_) {
- shutdown();
- }
-}
-
-void LoaderJobQueue::init(u32 numWorkers) {
- if (initialized_) {
- ES_LOG_WARN("LoaderJobQueue already initialized");
- return;
- }
-
- if (numWorkers == 0) {
- numWorkers = std::max(1u, std::thread::hardware_concurrency() - 1);
- }
-
- shutdownRequested_ = false;
-
- for (u32 i = 0; i < numWorkers; ++i) {
- workers_.emplace_back(&LoaderJobQueue::workerThread, this);
- }
-
- initialized_ = true;
- ES_LOG_INFO("LoaderJobQueue initialized with {} worker threads", numWorkers);
-}
-
-void LoaderJobQueue::shutdown() {
- if (!initialized_) {
- return;
- }
-
- ES_LOG_INFO("LoaderJobQueue shutting down...");
-
- {
- std::lock_guard<std::mutex> lock(pendingMutex_);
- shutdownRequested_ = true;
- }
- pendingCondition_.notify_all();
-
- for (auto& worker : workers_) {
- if (worker.joinable()) {
- worker.join();
- }
- }
- workers_.clear();
-
- {
- std::lock_guard<std::mutex> lock(pendingMutex_);
- while (!pendingJobs_.empty()) {
- pendingJobs_.pop();
- }
- }
-
- {
- std::lock_guard<std::mutex> lock(completionMutex_);
- while (!completions_.empty()) {
- completions_.pop();
- }
- }
-
- {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- activeJobIds_.clear();
- }
-
- initialized_ = false;
- ES_LOG_INFO("LoaderJobQueue shutdown complete");
-}
-
-// =============================================================================
-// Job Management
-// =============================================================================
-
-u64 LoaderJobQueue::submit(LoadJob job) {
- if (!initialized_) {
- ES_LOG_ERROR("LoaderJobQueue::submit called before init()");
- return 0;
- }
-
- u64 jobId = nextJobId_++;
- job.jobId = jobId;
-
- {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- activeJobIds_.insert(jobId);
- }
-
- ES_LOG_DEBUG("LoaderJobQueue: Submitted job {} for '{}'", jobId, job.path);
-
- {
- std::lock_guard<std::mutex> lock(pendingMutex_);
- pendingJobs_.push(std::move(job));
- }
- pendingCondition_.notify_one();
-
- return jobId;
-}
-
-bool LoaderJobQueue::cancel(u64 jobId) {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- auto it = activeJobIds_.find(jobId);
- if (it != activeJobIds_.end()) {
- activeJobIds_.erase(it);
- return true;
- }
- return false;
-}
-
-u32 LoaderJobQueue::processCompletions(u32 maxJobs) {
- u32 processed = 0;
-
- while (maxJobs == 0 || processed < maxJobs) {
- std::function<void()> completeFn;
-
- {
- std::lock_guard<std::mutex> lock(completionMutex_);
- if (completions_.empty()) {
- break;
- }
- completeFn = std::move(completions_.front());
- completions_.pop();
- }
-
- if (completeFn) {
- completeFn();
- }
- ++processed;
- }
-
- return processed;
-}
-
-u32 LoaderJobQueue::getPendingCount() const {
- std::lock_guard<std::mutex> lock(pendingMutex_);
- return static_cast<u32>(pendingJobs_.size());
-}
-
-u32 LoaderJobQueue::getCompletionCount() const {
- std::lock_guard<std::mutex> lock(completionMutex_);
- return static_cast<u32>(completions_.size());
-}
-
-bool LoaderJobQueue::isJobActive(u64 jobId) const {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- return activeJobIds_.find(jobId) != activeJobIds_.end();
-}
-
-// =============================================================================
-// Worker Thread
-// =============================================================================
-
-void LoaderJobQueue::workerThread() {
- while (true) {
- LoadJob job;
-
- {
- std::unique_lock<std::mutex> lock(pendingMutex_);
- pendingCondition_.wait(lock, [this] {
- return shutdownRequested_ || !pendingJobs_.empty();
- });
-
- if (shutdownRequested_ && pendingJobs_.empty()) {
- break;
- }
-
- if (pendingJobs_.empty()) {
- continue;
- }
-
- job = pendingJobs_.top();
- pendingJobs_.pop();
- }
-
- bool cancelled = false;
- {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- if (activeJobIds_.find(job.jobId) == activeJobIds_.end()) {
- cancelled = true;
- }
- }
-
- if (cancelled) {
- continue;
- }
-
- ++activeJobs_;
-
- if (job.workFn) {
- try {
- job.workFn();
- } catch (const std::exception& e) {
- ES_LOG_ERROR("LoaderJobQueue: Job {} threw exception: {}", job.jobId, e.what());
- }
- }
-
- bool cancelledDuringWork = false;
- {
- std::lock_guard<std::mutex> lock(activeJobsMutex_);
- if (activeJobIds_.erase(job.jobId) == 0) {
- cancelledDuringWork = true;
- }
- }
-
- if (!cancelledDuringWork && job.completeFn) {
- std::lock_guard<std::mutex> lock(completionMutex_);
- completions_.push(std::move(job.completeFn));
- }
-
- --activeJobs_;
- }
-}
-
-} // namespace esengine::resource
diff --git a/src/esengine/resource/LoaderJobQueue.hpp b/src/esengine/resource/LoaderJobQueue.hpp
deleted file mode 100644
index 19c492906..000000000
--- a/src/esengine/resource/LoaderJobQueue.hpp
+++ /dev/null
@@ -1,179 +0,0 @@
-/**
- * @file LoaderJobQueue.hpp
- * @brief Async resource loading job queue
- * @details Provides a thread pool for background resource loading with
- * main-thread completion callbacks.
- *
- * @author ESEngine Team
- * @date 2026
- *
- * @copyright Copyright (c) 2026 ESEngine Team
- * Licensed under the MIT License.
- */
-#pragma once
-
-// =============================================================================
-// Includes
-// =============================================================================
-
-#include "../core/Types.hpp"
-
-#include <string>
-#include <functional>
-#include <queue>
-#include <mutex>
-#include <condition_variable>
-#include <thread>
-#include <atomic>
-#include <vector>
-#include <unordered_set>
-
-namespace esengine::resource {
-
-// =============================================================================
-// Load Priority
-// =============================================================================
-
-enum class LoadPriority : u8 {
- Low = 0,
- Normal = 1,
- High = 2,
- Immediate = 3
-};
-
-// =============================================================================
-// Load Job
-// =============================================================================
-
-/**
- * @brief A single loading job for the queue
- */
-struct LoadJob {
- u64 jobId = 0; ///< Unique job identifier
- std::string path; ///< Resource path for logging
- LoadPriority priority = LoadPriority::Normal;
- std::function<void()> workFn; ///< Function to execute on worker thread
- std::function<void()> completeFn; ///< Function to execute on main thread
-
- bool operator<(const LoadJob& other) const {
- return static_cast<u8>(priority) < static_cast<u8>(other.priority);
- }
-};
-
-// =============================================================================
-// LoaderJobQueue Class
-// =============================================================================
-
-/**
- * @brief Thread pool for async resource loading
- *
- * @details Manages a pool of worker threads for background loading.
- * Completion callbacks are queued and must be processed on the
- * main thread via processCompletions().
- *
- * @code
- * LoaderJobQueue queue;
- * queue.init(4); // 4 worker threads
- *
- * // Submit a job
- * queue.submit({
- * .jobId = 1,
- * .path = "texture.png",
- * .priority = LoadPriority::Normal,
- * .workFn = [&]() { loadedData = loadFromDisk(); },
- * .completeFn = [&]() { createGPUResource(loadedData); }
- * });
- *
- * // In main loop
- * queue.processCompletions();
- *
- * queue.shutdown();
- * @endcode
- */
-class LoaderJobQueue {
-public:
- LoaderJobQueue() = default;
- ~LoaderJobQueue();
-
- LoaderJobQueue(const LoaderJobQueue&) = delete;
- LoaderJobQueue& operator=(const LoaderJobQueue&) = delete;
-
- /**
- * @brief Initializes the job queue with worker threads
- * @param numWorkers Number of worker threads (0 = hardware concurrency)
- */
- void init(u32 numWorkers = 0);
-
- /**
- * @brief Shuts down all worker threads
- * @details Waits for all pending jobs to complete
- */
- void shutdown();
-
- /**
- * @brief Checks if the queue is initialized
- * @return True if init() has been called
- */
- bool isInitialized() const { return initialized_; }
-
- /**
- * @brief Submits a job for async loading
- * @param job The job to submit
- * @return Job ID for tracking
- */
- u64 submit(LoadJob job);
-
- /**
- * @brief Cancels a pending job
- * @param jobId Job to cancel
- * @return True if job was cancelled (false if already running/completed)
- */
- bool cancel(u64 jobId);
-
- /**
- * @brief Processes completed job callbacks on the main thread
- * @param maxJobs Maximum jobs to process (0 = unlimited)
- * @return Number of completions processed
- */
- u32 processCompletions(u32 maxJobs = 0);
-
- /**
- * @brief Gets the number of pending jobs
- * @return Number of jobs waiting to be processed
- */
- u32 getPendingCount() const;
-
- /**
- * @brief Gets the number of completed jobs waiting for callbacks
- * @return Number of completions waiting
- */
- u32 getCompletionCount() const;
-
- /**
- * @brief Checks if a job is still pending or running
- * @param jobId Job to check
- * @return True if job is still in queue or running
- */
- bool isJobActive(u64 jobId) const;
-
-private:
- void workerThread();
-
- std::vector<std::thread> workers_;
- std::priority_queue<LoadJob> pendingJobs_;
- std::queue<std::function<void()>> completions_;
-
- mutable std::mutex pendingMutex_;
- mutable std::mutex completionMutex_;
- std::condition_variable pendingCondition_;
-
- std::atomic<u64> nextJobId_{1};
- std::atomic<bool> shutdownRequested_{false};
- std::atomic<u32> activeJobs_{0};
- bool initialized_ = false;
-
- std::unordered_set<u64> activeJobIds_;
- mutable std::mutex activeJobsMutex_;
-};
-
-} // namespace esengine::resource