LCOV - code coverage report
Current view: top level - core/threads - SPThreadTask.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 5 100.0 %
Date: 2024-05-12 00:16:13 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /**
       2             : Copyright (c) 2016-2019 Roman Katuntsev <sbkarr@stappler.org>
       3             : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
       4             : 
       5             : Permission is hereby granted, free of charge, to any person obtaining a copy
       6             : of this software and associated documentation files (the "Software"), to deal
       7             : in the Software without restriction, including without limitation the rights
       8             : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       9             : copies of the Software, and to permit persons to whom the Software is
      10             : furnished to do so, subject to the following conditions:
      11             : 
      12             : The above copyright notice and this permission notice shall be included in
      13             : all copies or substantial portions of the Software.
      14             : 
      15             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      16             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      17             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      18             : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      19             : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      20             : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      21             : THE SOFTWARE.
      22             : **/
      23             : 
      24             : #ifndef STAPPLER_THREADS_SPTHREADTASK_H_
      25             : #define STAPPLER_THREADS_SPTHREADTASK_H_
      26             : 
      27             : #include "SPMemPriorityQueue.h"
      28             : #include "SPRef.h"
      29             : 
      30             : namespace STAPPLER_VERSIONIZED stappler::thread {
      31             : 
      32             : class Task : public RefBase<memory::StandartInterface> {
      33             : public: /* typedefs */
      34             :         using Ref = RefBase<memory::StandartInterface>;
      35             : 
      36             :         /* Function to be executed in init phase */
      37             :         using PrepareCallback = std::function<bool(const Task &)>;
      38             : 
      39             :         /* Function to be executed in other thread */
      40             :         using ExecuteCallback = std::function<bool(const Task &)>;
      41             : 
      42             :         /* Function to be executed after task is performed */
      43             :         using CompleteCallback = std::function<void(const Task &, bool)>;
      44             : 
      45             :         using PriorityType = ValueWrapper<memory::PriorityQueue<Rc<Task>>::PriorityType, class PriorityTypeFlag>;
      46             : 
      47             :         Task();
      48             :         virtual ~Task();
      49             : 
      50             :         /* creates empty task with only complete function to be used as callback from other thread */
      51             :         bool init(const CompleteCallback &, Ref * = nullptr);
      52             :         bool init(CompleteCallback &&, Ref * = nullptr);
      53             : 
      54             :         /* creates regular async task without initialization phase */
      55             :         bool init(const ExecuteCallback &, const CompleteCallback & = nullptr, Ref * = nullptr);
      56             :         bool init(ExecuteCallback &&, CompleteCallback && = nullptr, Ref * = nullptr);
      57             : 
      58             :         /* creates regular async task with initialization phase */
      59             :         bool init(const PrepareCallback &, const ExecuteCallback &, const CompleteCallback & = nullptr, Ref * = nullptr);
      60             :         bool init(PrepareCallback &&, ExecuteCallback &&, CompleteCallback && = nullptr, Ref * = nullptr);
      61             : 
      62             :         /* adds one more function to be executed before task is added to queue, functions executed as FIFO */
      63             :         void addPrepareCallback(const PrepareCallback &);
      64             :         void addPrepareCallback(PrepareCallback &&);
      65             : 
      66             :         /* adds one more function to be executed in other thread, functions executed as FIFO */
      67             :         void addExecuteCallback(const ExecuteCallback &);
      68             :         void addExecuteCallback(ExecuteCallback &&);
      69             : 
      70             :         /* adds one more function to be executed when task is performed, functions executed as FIFO */
      71             :         void addCompleteCallback(const CompleteCallback &);
      72             :         void addCompleteCallback(CompleteCallback &&);
      73             : 
      74             :         /* mark this task with tag */
      75             :         void setTag(int tag) { _tag = tag; }
      76             : 
      77             :         /* returns tag */
      78             :         int getTag()  const{ return _tag; }
      79             : 
      80             :         /* set default task priority */
      81             :         void setPriority(PriorityType::Type priority) { _priority = PriorityType(priority); }
      82             : 
      83             :         /* get task priority */
      84     1234559 :         PriorityType getPriority() const { return _priority; }
      85             : 
      86     2611341 :         void addRef(Ref *target) { _refs.emplace_back(target); }
      87             : 
      88             :         /* used by task manager to set success state */
      89     1232243 :         void setSuccessful(bool value) { _isSuccessful = value; }
      90             : 
      91             :         /* if task execution was successful */
      92     1226577 :         bool isSuccessful() const { return _isSuccessful; }
      93             : 
      94             :         const std::vector<PrepareCallback> &getPrepareTasks() const { return _prepare; }
      95             :         const std::vector<ExecuteCallback> &getExecuteTasks() const { return _execute; }
      96     1032628 :         const std::vector<CompleteCallback> &getCompleteTasks() const { return _complete; }
      97             : 
      98             : public: /* overloads */
      99             :         virtual bool prepare() const;
     100             : 
     101             :         /** called on worker thread */
     102             :         virtual bool execute();
     103             : 
     104             :         /** called on UI thread when request is completed */
     105             :         virtual void onComplete();
     106             : 
     107             : protected:
     108             :         bool _isSuccessful = true;
     109             :         int _tag = -1;
     110             :         PriorityType _priority = PriorityType();
     111             : 
     112             :         std::vector<Rc<Ref>> _refs;
     113             :         std::vector<PrepareCallback> _prepare;
     114             :         std::vector<ExecuteCallback> _execute;
     115             :         std::vector<CompleteCallback> _complete;
     116             : };
     117             : 
     118             : }
     119             : 
     120             : #endif /* STAPPLER_THREADS_SPTHREADTASK_H_ */

Generated by: LCOV version 1.14