LCOV - code coverage report
Current view: top level - core/threads - SPThreadTask.cc (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 88 33.0 %
Date: 2024-05-12 00:16:13 Functions: 8 18 44.4 %

          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             : #include "SPThreadTask.h"
      25             : 
      26             : namespace STAPPLER_VERSIONIZED stappler::thread {
      27             : 
      28     1305723 : Task::Task() { }
      29     2610525 : Task::~Task() { }
      30             : 
      31             : /* creates empty task with only complete function to be used as callback from other thread */
      32           0 : bool Task::init(const CompleteCallback &c, Ref *t) {
      33           0 :         addRef(t);
      34           0 :         if (c) {
      35           0 :                 _complete.push_back(c);
      36             :         }
      37           0 :         return true;
      38             : }
      39             : 
      40       71188 : bool Task::init(CompleteCallback &&c, Ref *t) {
      41       71188 :         addRef(t);
      42       71188 :         if (c) {
      43       71188 :                 _complete.emplace_back(move(c));
      44             :         }
      45       71188 :         return true;
      46             : }
      47             : 
      48             : /* creates regular async task without initialization phase */
      49           0 : bool Task::init(const ExecuteCallback &e, const CompleteCallback &c, Ref *t) {
      50           0 :         addRef(t);
      51           0 :         if (e) {
      52           0 :                 _execute.push_back(e);
      53             :         }
      54           0 :         if (c) {
      55           0 :                 _complete.push_back(c);
      56             :         }
      57           0 :         return true;
      58             : }
      59             : 
      60     1234564 : bool Task::init(ExecuteCallback &&e, CompleteCallback &&c, Ref *t) {
      61     1234564 :         addRef(t);
      62     1234552 :         if (e) {
      63     1234552 :                 _execute.emplace_back(move(e));
      64             :         }
      65     1234553 :         if (c) {
      66     1155654 :                 _complete.emplace_back(move(c));
      67             :         }
      68     1234557 :         return true;
      69             : }
      70             : 
      71             : /* creates regular async task with initialization phase */
      72           0 : bool Task::init(const PrepareCallback &p, const ExecuteCallback &e, const CompleteCallback &c, Ref *t) {
      73           0 :         addRef(t);
      74           0 :         if (p) {
      75           0 :                 _prepare.push_back(p);
      76             :         }
      77           0 :         if (e) {
      78           0 :                 _execute.push_back(e);
      79             :         }
      80           0 :         if (c) {
      81           0 :                 _complete.push_back(c);
      82             :         }
      83           0 :         return true;
      84             : }
      85             : 
      86           0 : bool Task::init(PrepareCallback &&p, ExecuteCallback &&e, CompleteCallback &&c, Ref *t) {
      87           0 :         addRef(t);
      88           0 :         if (p) {
      89           0 :                 _prepare.emplace_back(move(p));
      90             :         }
      91           0 :         if (e) {
      92           0 :                 _execute.emplace_back(move(e));
      93             :         }
      94           0 :         if (c) {
      95           0 :                 _complete.emplace_back(move(c));
      96             :         }
      97           0 :         return true;
      98             : }
      99             : 
     100             : /* adds one more function to be executed before task is added to queue, functions executed as FIFO */
     101           0 : void Task::addPrepareCallback(const PrepareCallback &cb) {
     102           0 :         if (cb) {
     103           0 :                 _prepare.push_back(cb);
     104             :         }
     105           0 : }
     106             : 
     107           0 : void Task::addPrepareCallback(PrepareCallback &&cb) {
     108           0 :         if (cb) {
     109           0 :                 _prepare.emplace_back(move(cb));
     110             :         }
     111           0 : }
     112             : 
     113             : /* adds one more function to be executed in other thread, functions executed as FIFO */
     114           0 : void Task::addExecuteCallback(const ExecuteCallback &cb) {
     115           0 :         if (cb) {
     116           0 :                 _execute.push_back(cb);
     117             :         }
     118           0 : }
     119             : 
     120           0 : void Task::addExecuteCallback(ExecuteCallback &&cb) {
     121           0 :         if (cb) {
     122           0 :                 _execute.emplace_back(move(cb));
     123             :         }
     124           0 : }
     125             : 
     126             : /* adds one more function to be executed when task is performed, functions executed as FIFO */
     127           0 : void Task::addCompleteCallback(const CompleteCallback &cb) {
     128           0 :         if (cb) {
     129           0 :                 _complete.push_back(cb);
     130             :         }
     131           0 : }
     132             : 
     133           0 : void Task::addCompleteCallback(CompleteCallback &&cb) {
     134           0 :         if (cb) {
     135           0 :                 _complete.emplace_back(move(cb));
     136             :         }
     137           0 : }
     138             : 
     139     1234555 : bool Task::prepare() const {
     140     1234555 :         if (!_prepare.empty()) {
     141           0 :                 for (auto i : _prepare) {
     142           0 :                         if (i && !i(*this)) {
     143           0 :                                 return false;
     144             :                         }
     145           0 :                 }
     146             :         }
     147     1234554 :         return true;
     148             : }
     149             : 
     150             : /** called on worker thread */
     151     1233829 : bool Task::execute() {
     152     1233829 :         if (!_execute.empty()) {
     153     2466322 :                 for (auto i : _execute) {
     154     1231803 :                         if (i && !i(*this)) {
     155           0 :                                 return false;
     156             :                         }
     157     1232861 :                 }
     158             :         }
     159     1232854 :         return true;
     160             : }
     161             : 
     162             : /** called on UI thread when request is completed */
     163     1226534 : void Task::onComplete() {
     164     1226534 :         if (!_complete.empty()) {
     165     2453167 :                 for (auto i : _complete) {
     166     1226588 :                         i(*this, isSuccessful());
     167     1226759 :                 }
     168             :         }
     169     1226691 : }
     170             : 
     171             : }

Generated by: LCOV version 1.14