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

opengljobprocessor.cpp 9.33 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
//      Chair for Computer Aided Medical Procedures
8
9
//      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
30
31
32
// 
// 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.
// 
// ================================================================================================

#include "opengljobprocessor.h"

#include "tgt/assert.h"
33
34
#include "tgt/logmanager.h"
#include "tgt/openglgarbagecollector.h"
35
#include "tgt/glcontextmanager.h"
36
37
#include "core/tools/job.h"

schultezub's avatar
schultezub committed
38
namespace campvis {
39
40
41

    OpenGLJobProcessor::OpenGLJobProcessor()
    {
42
        _pause = 0;
43
        _currentContext = 0;
44
45
46
    }

    OpenGLJobProcessor::~OpenGLJobProcessor() {
schultezub's avatar
schultezub committed
47
48
49
50
51
52
53
        // delete all per-context job queues and unfinished jobs
        for (tbb::concurrent_vector<tgt::GLCanvas*>::const_iterator it = _contexts.begin(); it != _contexts.end(); ++it) {
            tbb::concurrent_hash_map<tgt::GLCanvas*, PerContextJobQueue*>::const_accessor a;
            if (_contextQueueMap.find(a, *it)) {
                delete a->second;
            }
        }
54

schultezub's avatar
schultezub committed
55
56
        _contextQueueMap.clear();
        _contexts.clear();
57
58
59
60
61
62
63
64
    }

    void OpenGLJobProcessor::stop() {
        _stopExecution = true;
        _evaluationCondition.notify_all();
    }

    void OpenGLJobProcessor::run() {
65
        std::unique_lock<tbb::mutex> lock(tgt::GlContextManager::getRef().getGlMutex());
66
        clock_t lastCleanupTime = clock() * 1000 / CLOCKS_PER_SEC;
67
68

        while (! _stopExecution) {
69
70
            // this is a simple round-robing scheduling between all contexts:
            bool hadWork = false;
schultezub's avatar
schultezub committed
71
            // TODO: consider only non-empty context queues here
72
            clock_t maxTimePerContext = static_cast<clock_t>(30 / _contexts.size());
73
74

            for (size_t i = 0; i < _contexts.size(); ++i) {
75
                clock_t startTimeCurrentContext = clock() * 1000 / CLOCKS_PER_SEC;
76
77
78
79
                tgt::GLCanvas* context = _contexts[i];

                tbb::concurrent_hash_map<tgt::GLCanvas*, PerContextJobQueue*>::const_accessor a;
                if (!_contextQueueMap.find(a, context)) {
80
81
                    //tgtAssert(false, "Should not reach this: Did not find context in contextQueueMap!");
                    continue;
82
83
                }

84
                // avoid expensive context-switches for contexts without pending jobs.
85
86
87
                if (a->second->empty())
                    continue;

88
89
90
                // we will have work, so update the flag
                hadWork = true;

schultezub's avatar
schultezub committed
91
                // perform context switch if necessary
92
                if (_currentContext != context) {
93
94
95
96
                    if (_currentContext != 0) {
                        glFinish();
                        LGL_ERROR;
                    }
97
                    tgt::GlContextManager::getRef().acquireContext(context);
98
99
100
101
102
103
                    _currentContext = context;
                }

                // now comes the per-context scheduling strategy:
                // first: perform as much serial jobs as possible:
                AbstractJob* jobToDo = 0;
104
                while ((clock() * 1000 / CLOCKS_PER_SEC) - startTimeCurrentContext < maxTimePerContext) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
                    // try fetch a job
                    if (! a->second->_serialJobs.try_pop(jobToDo)) {
                        // no job to do, exit the while loop
                        break;
                    }
                    // execute and delete the job
                    jobToDo->execute();
                    delete jobToDo;
                }

                // second: execute one low-prio job if existant
                if (a->second->_lowPriorityJobs.try_pop(jobToDo)) {
                    jobToDo->execute();
                    delete jobToDo;
119
120
                }

121
                // third: execute paint job
122
123
                jobToDo = a->second->_paintJob.fetch_and_store(0);
                if (jobToDo != 0) {
124
125
126
                    jobToDo->execute();
                    delete jobToDo;
                }
127
128
129
130
131
132
133


                // fourth: start the GC if it's time
                if (clock() * 1000 / CLOCKS_PER_SEC - lastCleanupTime > 250) {
                    GLGC.deleteGarbage();
                    lastCleanupTime = clock();
                }
134
            }
135
136
            
            while (_pause > 0) {
137
138
                GLGC.deleteGarbage();
                lastCleanupTime = clock();
139
                tgt::GlContextManager::getRef().releaseCurrentContext();
140
                _evaluationCondition.wait(lock);
141
                tgt::GlContextManager::getRef().acquireContext(_currentContext);
142
143
                hadWork = true;
            }
144

145
            if (! hadWork) {
146
147
148
149
                if (_currentContext != 0) {
                    GLGC.deleteGarbage();
                    lastCleanupTime = clock();
                }
150
                tgt::GlContextManager::getRef().releaseCurrentContext();
151
                _evaluationCondition.wait(lock);
152
                tgt::GlContextManager::getRef().acquireContext(_currentContext);
153
            }
154
155
156
        }

        // release OpenGL context, so that other threads can access it
157
        tgt::GlContextManager::getRef().releaseCurrentContext();
158
159
    }

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    void OpenGLJobProcessor::pause() {
        ++_pause;
    }

    void OpenGLJobProcessor::resume() {
        if (_pause == 0) {
            tgtAssert(false, "Called resume on non-paused job processor!");
            return;
        }

        --_pause;
        if (_pause == 0)
            _evaluationCondition.notify_all();
    }

175
176
177
178
179
    void OpenGLJobProcessor::enqueueJob(tgt::GLCanvas* canvas, AbstractJob* job, JobType priority) {
        tbb::concurrent_hash_map<tgt::GLCanvas*, PerContextJobQueue*>::const_accessor a;
        if (_contextQueueMap.find(a, canvas)) {
            switch (priority) {
            case PaintJob:
180
181
182
183
184
185
                {
                    AbstractJob* oldJob = a->second->_paintJob.fetch_and_store(job);
                    if (oldJob != 0)
                        delete oldJob;
                    break;
                }
186
187
188
189
190
191
192
193
194
195
196
197
            case SerialJob:
                a->second->_serialJobs.push(job);
                break;
            case LowPriorityJob:
                a->second->_lowPriorityJobs.push(job);
                break;
            default:
                tgtAssert(false, "Should not reach this - wrong job type!");
                break;
            } 
        }
        else {
198
            tgtAssert(false, "Specified context not found. Contexts must be registered before they can have jobs.");
199
        }
200
201
202

        _evaluationCondition.notify_all();
    }
203
204

    void OpenGLJobProcessor::registerContext(tgt::GLCanvas* context) {
schultezub's avatar
schultezub committed
205
#ifdef CAMPVIS_DEBUG
206
207
208
209
210
211
212
213
214
215
        tbb::concurrent_hash_map<tgt::GLCanvas*, PerContextJobQueue*>::const_accessor a;
        if (_contextQueueMap.find(a, context))
            tgtAssert(false, "Contexts shall only be registered once!");
#endif

        PerContextJobQueue* newQueue = new PerContextJobQueue;
        _contextQueueMap.insert(std::make_pair(context, newQueue));
        _contexts.push_back(context);
    }

216
    void OpenGLJobProcessor::deregisterContext(tgt::GLCanvas* context) {
217
        tbb::concurrent_hash_map<tgt::GLCanvas*, PerContextJobQueue*>::accessor a;
218
219
220
221
        if (_contextQueueMap.find(a, context)) {
            delete a->second;
            _contextQueueMap.erase(a);
        }
222
223
224

        if (_currentContext == context)
            _currentContext.compare_and_swap(0, context);
225
226
    }

227
228
229
230
231
232
233
234
235
236
237
    tgt::GLCanvas* OpenGLJobProcessor::iKnowWhatImDoingGetArbitraryContext() {
        if (_currentContext != 0)
            return _currentContext;
        else if (!_contexts.empty())
            return _contexts.front();
        else {
            tgtAssert(false, "No Contexts registered!");
            return 0;
        }
    }

238
239
}