abstractprocessor.cpp 5.49 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-2013, 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 Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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 <tbb/compat/thread>
26
#include "tgt/assert.h"
27

schultezub's avatar
schultezub committed
28
#include "abstractprocessor.h"
29
#include "core/properties/abstractproperty.h"
30
31
32
#include "core/tools/job.h"
#include "core/tools/opengljobprocessor.h"
#include "core/tools/simplejobprocessor.h"
schultezub's avatar
schultezub committed
33

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

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


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

    AbstractProcessor::~AbstractProcessor() {

    }

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

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

60

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

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

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

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

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

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

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

98
    void AbstractProcessor::invalidate(int level) {
99
100
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);
            s_invalidated(this);
        }
110
111
112
113
114
115
116
117
118
119
    }

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

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

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

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

    void AbstractProcessor::process(DataContainer& data, bool unlockInExtraThread) {
        // use a scoped lock for exception safety
        AbstractProcessor::ScopedLock lock(this, unlockInExtraThread);

        if (hasInvalidShader())
            updateShader();
        if (hasInvalidProperties())
            updateProperties(data);
        if (hasInvalidResult())
            updateResult(data);
    }

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

146
    void AbstractProcessor::updateProperties(DataContainer& dc) {
147
        LDEBUG("Called non-overriden updateProperties() in " << getName() << ". Did you forget to override your method?");
148
149
150
        validate(INVALID_PROPERTIES);
    }

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// ================================================================================================

    AbstractProcessor::ScopedLock::ScopedLock(AbstractProcessor* p, bool unlockInExtraThread)
        : _p(p)
        , _unlockInExtraThread(unlockInExtraThread) 
    {
        _p->lockProcessor();
    }

    AbstractProcessor::ScopedLock::~ScopedLock() {
        // Unlocking processors might be expensive, since a long chain of invalidations might be started
        // -> do this in another thread...
        if (_unlockInExtraThread)
            SimpleJobProc.enqueueJob(makeJob(_p, &AbstractProcessor::unlockProcessor));
        else
            _p->unlockProcessor();    
    }
168

schultezub's avatar
schultezub committed
169
}