File: ProgressHandler.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (45 lines) | stat: -rw-r--r-- 1,724 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Base/Progress/ProgressHandler.cpp
//! @brief     Implements class ProgressHandler.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Base/Progress/ProgressHandler.h"
#include "Base/Util/Assert.h"
#include <mutex>

void ProgressHandler::subscribe(Callback_t inform)
{
    ASSERT(!m_inform); // no more than one subscriber is allowed
    m_inform = inform;
}

//! Increments number of completed computation steps (ticks).
//! Performs callback (method m_inform) to inform the subscriber about
//! the state of the computation and to obtain as return value a flag
//! that indicates whether to continue the computation.
void ProgressHandler::incrementDone(size_t ticks_done)
{
    static std::mutex single_mutex;
    {
        std::unique_lock<std::mutex> _(single_mutex);

        m_completed_nticks += ticks_done;
        if (m_completed_nticks > m_expected_nticks)
            m_expected_nticks = m_completed_nticks + 1;

        int percentage_done = (int)(100. * m_completed_nticks / m_expected_nticks);
        // fractional part is discarded, which is fine here:
        // the value 100 is only returned if everything is done

        m_continuation_flag = (!m_inform || m_inform(percentage_done)) && m_continuation_flag;
    }
}