abstractprocessor.cpp 4.59 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
#include <tbb/compat/thread>
31
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
32
#include "abstractprocessor.h"
33
#include "core/properties/abstractproperty.h"
schultezub's avatar
schultezub committed
34

schultezub's avatar
schultezub committed
35
namespace campvis {
schultezub's avatar
schultezub committed
36

schultezub's avatar
schultezub committed
37
    const std::string AbstractProcessor::loggerCat_ = "CAMPVis.core.datastructures.Processor";
schultezub's avatar
schultezub committed
38
39


40
41
42
    AbstractProcessor::AbstractProcessor()
        : HasPropertyCollection()
    {
43
        _enabled = true;
44
        _clockExecutionTime = false;
45
        _locked = 0;
46
        _level = VALID;
schultezub's avatar
schultezub committed
47
48
49
50
51
52
    }

    AbstractProcessor::~AbstractProcessor() {

    }

schultezub's avatar
schultezub committed
53
    void AbstractProcessor::init() {
schultezub's avatar
schultezub committed
54
        initAllProperties();
55
    }
schultezub's avatar
schultezub committed
56

57
    void AbstractProcessor::deinit() {
schultezub's avatar
schultezub committed
58
        deinitAllProperties();
schultezub's avatar
schultezub committed
59
60
    }

61

62
    void AbstractProcessor::lockProcessor() {
63
64
        while (_locked.compare_and_swap(true, false) == true) // TODO: busy waiting us fu**ing ugly...
            std::this_thread::yield();
65
        _locked = true;
schultezub's avatar
schultezub committed
66
        lockAllProperties();
67
68
    }

69
70
    void AbstractProcessor::unlockProcessor() {
        tgtAssert(_locked == true, "Called AbstractProcessor::unlockProcessor() on unlocked processor!");
schultezub's avatar
schultezub committed
71
        unlockAllProperties();
72
73
74
75
76
        int summed = VALID;
        int il;
        while (_queuedInvalidations.try_pop(il)) {
            summed |= il;
        }
77
        _locked = false;
schultezub's avatar
schultezub committed
78
79
80

        if (summed != VALID)
            invalidate(summed);
81
82
83
84
    }

    bool AbstractProcessor::isLocked() {
        return _locked != 0;
85
86
    }

schultezub's avatar
schultezub committed
87
    void AbstractProcessor::onPropertyChanged(const AbstractProperty* prop) {
88
        HasPropertyCollection::onPropertyChanged(prop);
89
        invalidate(prop->getInvalidationLevel());
schultezub's avatar
schultezub committed
90
    }
91

92
93
94
95
96
97
98
99
    bool AbstractProcessor::getEnabled() const {
        return _enabled;
    }

    void AbstractProcessor::setEnabled(bool enabled) {
        _enabled = enabled;
    }

100
    void AbstractProcessor::invalidate(int level) {
101
102
103
104
105
106
107
108
109
110
111
        if (_locked) {
            // TODO: this is not 100% thread-safe - an invalidation might slip through if the processor is unlocked during invalidation
            _queuedInvalidations.push(level);
        }
        else {
            int tmp;
            do {
                tmp = _level;
            } while (_level.compare_and_swap(tmp | level, tmp) != tmp);
            s_invalidated(this);
        }
112
113
114
115
116
117
118
119
120
121
    }

    void AbstractProcessor::validate(int level) {
        int tmp;
        do {
            tmp = _level;
        } while (_level.compare_and_swap(tmp & (~level), tmp) != tmp);
        s_validated(this);
    }

122
123
124
125
126
127
128
129
    bool AbstractProcessor::getClockExecutionTime() const {
        return _clockExecutionTime;
    }

    void AbstractProcessor::setClockExecutionTime(bool value) {
        _clockExecutionTime = value;
    }

130
131
132
133
    void AbstractProcessor::updateProperties(DataContainer& dc) {
        validate(INVALID_PROPERTIES);
    }

134

schultezub's avatar
schultezub committed
135
}