Bläddra i källkod

AsyncTask::is_runnable

David Rose 17 år sedan
förälder
incheckning
2266110083

+ 15 - 0
panda/src/event/asyncTask.cxx

@@ -349,6 +349,21 @@ unlock_and_do_task() {
   return status;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: AsyncTask::is_runnable
+//       Access: Protected, Virtual
+//  Description: 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 AsyncTask::
+is_runnable() {
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: AsyncTask::do_task
 //       Access: Protected, Virtual

+ 1 - 0
panda/src/event/asyncTask.h

@@ -111,6 +111,7 @@ protected:
   void jump_to_task_chain(AsyncTaskManager *manager);
   DoneStatus unlock_and_do_task();
 
+  virtual bool is_runnable();
   virtual DoneStatus do_task();
   virtual void upon_birth();
   virtual void upon_death(bool clean_exit);

+ 2 - 0
panda/src/event/asyncTaskManager.cxx

@@ -167,6 +167,8 @@ remove_task_chain(const string &name) {
 ////////////////////////////////////////////////////////////////////
 void AsyncTaskManager::
 add(AsyncTask *task) {
+  nassertv(task->is_runnable());
+
   MutexHolder holder(_lock);
 
   if (task_cat.is_debug()) {

+ 16 - 1
panda/src/event/pythonTask.cxx

@@ -79,7 +79,7 @@ set_function(PyObject *function) {
 
   _function = function;
   Py_INCREF(_function);
-  if (!PyCallable_Check(_function)) {
+  if (_function != Py_None && !PyCallable_Check(_function)) {
     nassert_raise("Invalid function passed to PythonTask");
   }
 }
@@ -306,6 +306,21 @@ __getattr__(const string &attr_name) const {
   }
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PythonTask::is_runnable
+//       Access: Protected, Virtual
+//  Description: 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 PythonTask::
+is_runnable() {
+  return _function != Py_None;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PythonTask::do_task
 //       Access: Protected, Virtual

+ 2 - 1
panda/src/event/pythonTask.h

@@ -27,7 +27,7 @@
 ////////////////////////////////////////////////////////////////////
 class EXPCL_PANDA_PIPELINE PythonTask : public AsyncTask {
 PUBLISHED:
-  PythonTask(PyObject *function, const string &name = string());
+  PythonTask(PyObject *function = Py_None, const string &name = string());
   virtual ~PythonTask();
   ALLOC_DELETED_CHAIN(PythonTask);
 
@@ -48,6 +48,7 @@ PUBLISHED:
   PyObject *__getattr__(const string &attr_name) const;
 
 protected:
+  virtual bool is_runnable();
   virtual DoneStatus do_task();
   DoneStatus do_python_task();
   virtual void upon_birth();