2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

abstractprocessor.cpp 6.25 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-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

25
#include "abstractprocessor.h"
26
#include "cgt/assert.h"
27
#include "core/properties/abstractproperty.h"
28

29
30
#include <ext/threading.h>

schultezub's avatar
schultezub committed
31
namespace campvis {
schultezub's avatar
schultezub committed
32

schultezub's avatar
schultezub committed
33
    const std::string AbstractProcessor::loggerCat_ = "CAMPVis.core.datastructures.Processor";
schultezub's avatar
schultezub committed
34
35


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

    AbstractProcessor::~AbstractProcessor() {

    }

schultezub's avatar
schultezub committed
50
    void AbstractProcessor::init() {
schultezub's avatar
schultezub committed
51
        initAllProperties();
52
    }
schultezub's avatar
schultezub committed
53

54
    void AbstractProcessor::deinit() {
schultezub's avatar
schultezub committed
55
        deinitAllProperties();
schultezub's avatar
schultezub committed
56
57
    }

58

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

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

        if (summed != VALID)
            invalidate(summed);
78
79
80
81
    }

    bool AbstractProcessor::isLocked() {
        return _locked != 0;
82
83
    }

schultezub's avatar
schultezub committed
84
    void AbstractProcessor::onPropertyChanged(const AbstractProperty* prop) {
85
86
87
88
89
90
91
92
        if (_ignorePropertyChanges == 0) {
            tbb::spin_rw_mutex::scoped_lock lock(_mtxInvalidationMap, false);
            auto it = _invalidationMap.find(prop);
            if (it != _invalidationMap.end())
                invalidate(it->second);
            else
                LDEBUG("Caught an property changed signal that was not registered with an invalidation level. Did you forget to call addProperty()?");
        }
schultezub's avatar
schultezub committed
93
    }
94

95
96
97
98
99
100
101
102
    bool AbstractProcessor::getEnabled() const {
        return _enabled;
    }

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

103
    void AbstractProcessor::invalidate(int level) {
104
105
106
107
108
109
110
111
112
        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);
113
            s_invalidated.emitSignal(this);
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);
122
        s_validated.emitSignal(this);
123
124
    }

125
126
127
128
129
130
131
132
    bool AbstractProcessor::getClockExecutionTime() const {
        return _clockExecutionTime;
    }

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

133

134
    void AbstractProcessor::process(DataContainer& data) {
135
136
137
138
139
140
141
142
143
        if (hasInvalidShader()) {
            updateShader();
            validate(INVALID_SHADER);
        }
        if (hasInvalidProperties()) {
            updateProperties(data);
            validate(INVALID_PROPERTIES);
        }

144
        // use a scoped lock for exception safety
145
        AbstractProcessor::ScopedLock lock(this);
146
        cgtAssert(_locked == true, "Processor not locked, this should not happen!");
147

148
149
150
151
152
153
154
155
        if (hasInvalidShader()) {
            updateShader();
            validate(INVALID_SHADER);
        }
        if (hasInvalidProperties()) {
            updateProperties(data);
            validate(INVALID_PROPERTIES);
        }
156
        if (hasInvalidResult()) {
157
            updateResult(data);
158
159
            validate(INVALID_RESULT);
        }
160
161
162
163
164
165
    }

    void AbstractProcessor::updateShader() {
        LDEBUG("Called non-overriden updateShader() in " << getName() << ". Did you forget to override your method?");
    }

166
    void AbstractProcessor::updateProperties(DataContainer& dc) {
167
        LDEBUG("Called non-overriden updateProperties() in " << getName() << ". Did you forget to override your method?");
168
169
    }

170
171
172
173
174
175
176
177
178
179
180
181
182
    void AbstractProcessor::addProperty(AbstractProperty& prop) {
        this->addProperty(prop, INVALID_RESULT);
    }

    void AbstractProcessor::addProperty(AbstractProperty& prop, int invalidationLevel) {
        HasPropertyCollection::addProperty(prop);
        setPropertyInvalidationLevel(prop, invalidationLevel);
    }

    void AbstractProcessor::setPropertyInvalidationLevel(AbstractProperty& prop, int invalidationLevel) {
        tbb::spin_rw_mutex::scoped_lock lock(_mtxInvalidationMap, true);
        _invalidationMap[&prop] = invalidationLevel;
    }
183
184
185
186
187
188
189
    void AbstractProcessor::ignorePropertyChanges() {
        ++_ignorePropertyChanges;
    }

    void AbstractProcessor::observePropertyChanges() {
        --_ignorePropertyChanges;
    }
schultezub's avatar
schultezub committed
190
}