Files
filament/libs/utils/include/utils/AsyncJobQueue.h
Mathias Agopian 209d3f7550 Fix a race condition when tearing down FrameInfo (#9413)
* HandleAllocator::deallocate() was unsafe

It needs to know the concrete type to call the proper destructor, so
if it was given a base type handle (e.g. Handle<HwFoo>) it would not
destroy it properly.

* add AsyncJobQueue::cancelAll()

* Fix a race condition when tearing down FrameInfo

It is actually invalid to destroy a Handle<HwFence> while inside
fenceWait().

Updated the HwFence implementations so that they don't pretend they can
handle being destroyed during fenceWait(), they can't.

FrameInfo now cancels all the pending callbacks and waits for the 
currently executing one to terminate, *before* destroying the
handles.

Reenable the gpuFrameComplete metric, as it should be working now.
2025-11-06 16:50:08 -08:00

70 lines
1.9 KiB
C++

/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TNT_ASYNCJOBQUEUE_H
#define TNT_ASYNCJOBQUEUE_H
#include <utils/JobSystem.h>
#include <utils/Invocable.h>
#include <utils/Mutex.h> // NOLINT(*-include-cleaner)
#include <utils/Condition.h> // NOLINT(*-include-cleaner)
#include <thread>
#include <vector>
namespace utils {
/**
* Simple asynchronous job queue. This manages a *single*thread that executes jobs submitted
* to it in order.
*/
class AsyncJobQueue {
public:
using Job = Invocable<void()>;
using Priority = JobSystem::Priority;
// create the job queue with a name and desired priority
AsyncJobQueue(const char* name, Priority priority);
// drainAndExit() must be called first
~AsyncJobQueue() noexcept;
// cancel all pending jobs, but doesn't exist the thread
void cancelAll() noexcept;
// blocks until all jobs are executed and quits the thread
void drainAndExit();
// adds a job to the queue. no-op if drainAndExit() was called.
void push(Job&& job);
bool isValid() const noexcept;
private:
#if !defined(__EMSCRIPTEN__)
using Container = std::vector<Job>;
std::thread mThread;
Mutex mLock; // NOLINT(*-include-cleaner)
Condition mCondition; // NOLINT(*-include-cleaner)
Container mQueue;
bool mExitRequested = false;
#endif
};
} // namespace utils
#endif //TNT_ASYNCJOBQUEUE_H