Beatmup
multitask.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "../exception.h"
20 #include "multitask.h"
21 #include "../debug.h"
22 
23 using namespace Beatmup;
24 
25 namespace Internal {
26  /**
27  TaskHolder implementation for Multitask pipeline.
28  Used only internally.
29  */
31  public:
33 
35  {
37  }
38  };
39 }
40 
41 
44 }
45 
47  // skip tasks that are "ignored"
48  {
49  std::lock_guard<std::mutex> lock(policyAccess);
50  while (!router.allTasksDone()) {
51  RepetitionPolicy policy = static_cast<const Internal::MultitaskTaskHolder&>(router.getCurrentTask()).repetitionPolicy;
53  break;
54  router.goToNextTask();
55  }
56  }
57 
58  // then go
59  while (!router.allTasksDone() && !router.allTasksAborted()) {
61  if (task.repetitionPolicy != RepetitionPolicy::IGNORE_ALWAYS)
62  router.runTask();
63 
64  // switch repetition policy if needed
65  policyAccess.lock();
66  if (task.repetitionPolicy == RepetitionPolicy::REPEAT_UPDATE)
67  task.repetitionPolicy = RepetitionPolicy::IGNORE_IF_UPTODATE;
68  policyAccess.unlock();
69 
70  router.goToNextTask();
71  }
72 
73  return true;
74 }
75 
77 
79 {
81  std::lock_guard<std::mutex> lock(policyAccess);
82  return ((const Internal::MultitaskTaskHolder&)task).repetitionPolicy;
83 }
84 
87  std::lock_guard<std::mutex> lock(policyAccess);
88  static_cast<Internal::MultitaskTaskHolder&>(taskHolder).repetitionPolicy = policy;
89 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
A task within a pipeline.
int getTaskIndex(const TaskHolder &)
Retrieves task index if it is in the pipeline; returns -1 otherwise.
bool route(TaskRouter &router)
Definition: multitask.cpp:46
std::mutex policyAccess
access control to modify repetition policies
Definition: multitask.h:50
TaskHolder * createTaskHolder(AbstractTask &task)
Definition: multitask.cpp:42
RepetitionPolicy getRepetitionPolicy(const TaskHolder &)
Definition: multitask.cpp:78
void setRepetitionPolicy(TaskHolder &taskHolder, RepetitionPolicy policy)
Sets repetition policy of a task.
Definition: multitask.cpp:85
RepetitionPolicy
Determines when a specific task in the sequence is run when the whole sequence is invoked.
Definition: multitask.h:60
@ IGNORE_IF_UPTODATE
do not execute the task if no preceding tasks are run
@ REPEAT_ALWAYS
execute the task unconditionally on each run
@ IGNORE_ALWAYS
do not execute the task
@ REPEAT_UPDATE
execute the task one time then switch to IGNORE_IF_UPTODATE
Interface managing the execution of a sequence of tasks.
virtual void goToNextTask()=0
Goes to the next task in the list.
virtual void runTask()=0
Executes the pointed task.
virtual CustomPipeline::TaskHolder & getCurrentTask()=0
Returns currently pointed task.
virtual bool allTasksDone() const =0
Returns true if all tasks are done.
virtual bool allTasksAborted() const =0
Returns true if the current session is aborted.
TaskHolder implementation for Multitask pipeline.
Definition: multitask.cpp:30
MultitaskTaskHolder(AbstractTask &task)
Definition: multitask.cpp:34
Multitask::RepetitionPolicy repetitionPolicy
Definition: multitask.cpp:32
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
Beatmup internal declarations.
Definition: basic_types.h:78
return pipeline getTaskIndex * taskHolder
jlong jlong jint policy
Beatmup::NNets::InferenceTask * task