From b4d51c24e9d58251795d153fe1f762a643e39689 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 22 Feb 2022 17:06:25 +0100 Subject: [PATCH] event: Remove FunctionAsyncTask To create a task from a lambda, it is more efficient to use the new AsyncTaskManager::add() short-hand which creates an AsyncTask subclass in-place. --- panda/src/event/CMakeLists.txt | 2 - panda/src/event/asyncTaskManager.h | 1 - panda/src/event/config_event.cxx | 2 - panda/src/event/functionAsyncTask.I | 83 -------------------------- panda/src/event/functionAsyncTask.cxx | 81 ------------------------- panda/src/event/functionAsyncTask.h | 83 -------------------------- panda/src/event/genericAsyncTask.h | 2 - panda/src/event/p3event_composite1.cxx | 1 - panda/src/gobj/texture.cxx | 5 +- 9 files changed, 2 insertions(+), 258 deletions(-) delete mode 100644 panda/src/event/functionAsyncTask.I delete mode 100644 panda/src/event/functionAsyncTask.cxx delete mode 100644 panda/src/event/functionAsyncTask.h diff --git a/panda/src/event/CMakeLists.txt b/panda/src/event/CMakeLists.txt index 47136d1b5e..b0464ff793 100644 --- a/panda/src/event/CMakeLists.txt +++ b/panda/src/event/CMakeLists.txt @@ -9,7 +9,6 @@ set(P3EVENT_HEADERS config_event.h buttonEvent.I buttonEvent.h buttonEventList.I buttonEventList.h - functionAsyncTask.h functionAsyncTask.I genericAsyncTask.h genericAsyncTask.I pointerEvent.I pointerEvent.h pointerEventList.I pointerEventList.h @@ -29,7 +28,6 @@ set(P3EVENT_SOURCES asyncTaskSequence.cxx buttonEvent.cxx buttonEventList.cxx - functionAsyncTask.cxx genericAsyncTask.cxx pointerEvent.cxx pointerEventList.cxx diff --git a/panda/src/event/asyncTaskManager.h b/panda/src/event/asyncTaskManager.h index 00b5329e9e..0028054ef7 100644 --- a/panda/src/event/asyncTaskManager.h +++ b/panda/src/event/asyncTaskManager.h @@ -30,7 +30,6 @@ #include "clockObject.h" #include "ordered_vector.h" #include "indirectCompareNames.h" -#include "functionAsyncTask.h" /** * A class to manage a loose queue of isolated tasks, which can be performed diff --git a/panda/src/event/config_event.cxx b/panda/src/event/config_event.cxx index 93dc8d916b..ccae0d95ae 100644 --- a/panda/src/event/config_event.cxx +++ b/panda/src/event/config_event.cxx @@ -22,7 +22,6 @@ #include "event.h" #include "eventHandler.h" #include "eventParameter.h" -#include "functionAsyncTask.h" #include "genericAsyncTask.h" #include "pointerEventList.h" @@ -50,7 +49,6 @@ ConfigureFn(config_event) { EventHandler::init_type(); EventStoreInt::init_type("EventStoreInt"); EventStoreDouble::init_type("EventStoreDouble"); - FunctionAsyncTask::init_type(); GenericAsyncTask::init_type(); ButtonEventList::register_with_read_factory(); diff --git a/panda/src/event/functionAsyncTask.I b/panda/src/event/functionAsyncTask.I deleted file mode 100644 index aa413dacf0..0000000000 --- a/panda/src/event/functionAsyncTask.I +++ /dev/null @@ -1,83 +0,0 @@ -/** - * PANDA 3D SOFTWARE - * Copyright (c) Carnegie Mellon University. All rights reserved. - * - * All use of this software is subject to the terms of the revised BSD - * license. You should have received a copy of this license along - * with this source code in a file named "LICENSE." - * - * @file functionAsyncTask.I - * @author rdb - * @date 2021-11-29 - */ - -/** - * - */ -INLINE FunctionAsyncTask:: -FunctionAsyncTask(const std::string &name) : - AsyncTask(name) -{ -} - -/** - * - */ -INLINE FunctionAsyncTask:: -FunctionAsyncTask(const std::string &name, FunctionAsyncTask::TaskFunction function) : - AsyncTask(name), - _function(std::move(function)) -{ -} - -/** - * Replaces the function that is called when the task runs. - */ -INLINE void FunctionAsyncTask:: -set_function(TaskFunction function) { - _function = std::move(function); -} - -/** - * Returns the function that is called when the task runs. - */ -INLINE const FunctionAsyncTask::TaskFunction &FunctionAsyncTask:: -get_function() const { - return _function; -} - -/** - * Replaces the function that is called when the task begins. This is an - * optional function. - */ -INLINE void FunctionAsyncTask:: -set_upon_birth(BirthFunction upon_birth) { - _upon_birth = std::move(upon_birth); -} - -/** - * Returns the function that is called when the task begins, or NULL if the - * function is not defined. - */ -INLINE const FunctionAsyncTask::BirthFunction &FunctionAsyncTask:: -get_upon_birth() const { - return _upon_birth; -} - -/** - * Replaces the function that is called when the task ends. This is an - * optional function. - */ -INLINE void FunctionAsyncTask:: -set_upon_death(FunctionAsyncTask::DeathFunction upon_death) { - _upon_death = upon_death; -} - -/** - * Returns the function that is called when the task ends, or NULL if the - * function is not defined. - */ -INLINE const FunctionAsyncTask::DeathFunction &FunctionAsyncTask:: -get_upon_death() const { - return _upon_death; -} diff --git a/panda/src/event/functionAsyncTask.cxx b/panda/src/event/functionAsyncTask.cxx deleted file mode 100644 index 54b591918d..0000000000 --- a/panda/src/event/functionAsyncTask.cxx +++ /dev/null @@ -1,81 +0,0 @@ -/** - * PANDA 3D SOFTWARE - * Copyright (c) Carnegie Mellon University. All rights reserved. - * - * All use of this software is subject to the terms of the revised BSD - * license. You should have received a copy of this license along - * with this source code in a file named "LICENSE." - * - * @file functionAsyncTask.cxx - * @author rdb - * @date 2021-11-29 - */ - -#include "functionAsyncTask.h" -#include "pnotify.h" - -#ifndef CPPPARSER - -TypeHandle FunctionAsyncTask::_type_handle; - -/** - * Override this function to return true if the task can be successfully - * executed, false if it cannot. Mainly intended as a sanity check when - * attempting to add the task to a task manager. - * - * This function is called with the lock held. - */ -bool FunctionAsyncTask:: -is_runnable() { - return !!_function; -} - -/** - * Override this function to do something useful for the task. - * - * This function is called with the lock *not* held. - */ -AsyncTask::DoneStatus FunctionAsyncTask:: -do_task() { - nassertr(_function, DS_interrupt); - return _function(this); -} - -/** - * Override this function to do something useful when the task has been added - * to the active queue. - * - * This function is called with the lock *not* held. - */ -void FunctionAsyncTask:: -upon_birth(AsyncTaskManager *manager) { - AsyncTask::upon_birth(manager); - - if (_upon_birth) { - _upon_birth(this); - } -} - -/** - * Override this function to do something useful when the task has been - * removed from the active queue. The parameter clean_exit is true if the - * task has been removed because it exited normally (returning DS_done), or - * false if it was removed for some other reason (e.g. - * AsyncTaskManager::remove()). By the time this method is called, _manager - * has been cleared, so the parameter manager indicates the original - * AsyncTaskManager that owned this task. - * - * The normal behavior is to throw the done_event only if clean_exit is true. - * - * This function is called with the lock *not* held. - */ -void FunctionAsyncTask:: -upon_death(AsyncTaskManager *manager, bool clean_exit) { - AsyncTask::upon_death(manager, clean_exit); - - if (_upon_death) { - _upon_death(this, clean_exit); - } -} - -#endif // CPPPARSER diff --git a/panda/src/event/functionAsyncTask.h b/panda/src/event/functionAsyncTask.h deleted file mode 100644 index 297d71c691..0000000000 --- a/panda/src/event/functionAsyncTask.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * PANDA 3D SOFTWARE - * Copyright (c) Carnegie Mellon University. All rights reserved. - * - * All use of this software is subject to the terms of the revised BSD - * license. You should have received a copy of this license along - * with this source code in a file named "LICENSE." - * - * @file functionAsyncTask.h - * @author rdb - * @date 2021-11-29 - */ - -#ifndef FUNCTIONASYNCTASK_H -#define FUNCTIONASYNCTASK_H - -#include "pandabase.h" - -#include "asyncTask.h" - -#ifndef CPPPARSER -#include - -/** - * Associates a generic std::function (eg. a lambda) with an AsyncTask object. - * You can use this when you want to create an AsyncTask without having to - * subclass. - * - * @since 1.11.0 - */ -class EXPCL_PANDA_EVENT FunctionAsyncTask final : public AsyncTask { -public: - typedef std::function TaskFunction; - typedef std::function BirthFunction; - typedef std::function DeathFunction; - - INLINE FunctionAsyncTask(const std::string &name = std::string()); - INLINE FunctionAsyncTask(const std::string &name, TaskFunction function); - ALLOC_DELETED_CHAIN(FunctionAsyncTask); - - INLINE void set_function(TaskFunction function); - INLINE const TaskFunction &get_function() const; - - INLINE void set_upon_birth(BirthFunction function); - INLINE const BirthFunction &get_upon_birth() const; - - INLINE void set_upon_death(DeathFunction function); - INLINE const DeathFunction &get_upon_death() const; - -protected: - virtual bool is_runnable() override; - virtual DoneStatus do_task() override; - virtual void upon_birth(AsyncTaskManager *manager) override; - virtual void upon_death(AsyncTaskManager *manager, bool clean_exit) override; - -private: - TaskFunction _function; - BirthFunction _upon_birth; - DeathFunction _upon_death; - -public: - static TypeHandle get_class_type() { - return _type_handle; - } - static void init_type() { - AsyncTask::init_type(); - register_type(_type_handle, "FunctionAsyncTask", - AsyncTask::get_class_type()); - } - virtual TypeHandle get_type() const override { - return get_class_type(); - } - virtual TypeHandle force_init_type() override {init_type(); return get_class_type();} - -private: - static TypeHandle _type_handle; -}; - -#include "functionAsyncTask.I" - -#endif // CPPPARSER - -#endif diff --git a/panda/src/event/genericAsyncTask.h b/panda/src/event/genericAsyncTask.h index ce7f635156..f35d780e3b 100644 --- a/panda/src/event/genericAsyncTask.h +++ b/panda/src/event/genericAsyncTask.h @@ -22,8 +22,6 @@ * Associates a generic C-style function pointer with an AsyncTask object. * You can use this when you want to create an AsyncTask without having to * subclass. - * - * @deprecated See FunctionAsyncTask instead, which is more powerful. */ class EXPCL_PANDA_EVENT GenericAsyncTask : public AsyncTask { public: diff --git a/panda/src/event/p3event_composite1.cxx b/panda/src/event/p3event_composite1.cxx index 34fcb9a0c9..dc72d2b03f 100644 --- a/panda/src/event/p3event_composite1.cxx +++ b/panda/src/event/p3event_composite1.cxx @@ -7,7 +7,6 @@ #include "asyncTaskSequence.cxx" #include "buttonEvent.cxx" #include "buttonEventList.cxx" -#include "functionAsyncTask.cxx" #include "genericAsyncTask.cxx" #include "pointerEvent.cxx" #include "pointerEventList.cxx" diff --git a/panda/src/gobj/texture.cxx b/panda/src/gobj/texture.cxx index 51b7f7b64f..1e53294813 100644 --- a/panda/src/gobj/texture.cxx +++ b/panda/src/gobj/texture.cxx @@ -1065,7 +1065,7 @@ async_ensure_ram_image(bool allow_compression, int priority) { double delay = async_load_delay; // This texture has not yet been queued to be reloaded. Queue it up now. - task = new FunctionAsyncTask(task_name, [=](AsyncTask *task) { + task = task_mgr->add(task_name, [=](AsyncTask *task) { if (delay != 0.0) { Thread::sleep(delay); } @@ -1076,9 +1076,8 @@ async_ensure_ram_image(bool allow_compression, int priority) { } return AsyncTask::DS_done; }); - task->set_task_chain("texture_reload"); task->set_priority(priority); - task_mgr->add(task); + task->set_task_chain("texture_reload"); cdataw->_reload_task = task; return (AsyncFuture *)task; }