abstractprocessor.cpp 5.71 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
        _locked = 0;
42
        _level = VALID;
schultezub's avatar
schultezub committed
43
44
45
46
47
48
    }

    AbstractProcessor::~AbstractProcessor() {

    }

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

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

57

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

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

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

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

schultezub's avatar
schultezub committed
83
    void AbstractProcessor::onPropertyChanged(const AbstractProperty* prop) {
84
85
86
87
88
89
        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
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
        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);
110
            s_invalidated.emitSignal(this);
111
        }
112
113
114
115
116
117
118
    }

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

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

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

130

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

141
        // use a scoped lock for exception safety
142
        AbstractProcessor::ScopedLock lock(this);
143
        tgtAssert(_locked == true, "Processor not locked, this should not happen!");
144

145
        if (hasInvalidResult()) {
146
            updateResult(data);
147
148
            validate(INVALID_RESULT);
        }
149
150
151
152
153
154
    }

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

155
    void AbstractProcessor::updateProperties(DataContainer& dc) {
156
        LDEBUG("Called non-overriden updateProperties() in " << getName() << ". Did you forget to override your method?");
157
158
    }

159
160
161
162
163
164
165
166
167
168
169
170
171
    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;
    }
schultezub's avatar
schultezub committed
172
}