Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 60 additions & 22 deletions src/bin/imgtools/rvio/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
#include <RvApp/FormatIPNode.h>
#include <arg.h>
#include <iostream>
#include <memory>
#include <sched.h>
#include <cstdio>
#include <cstdlib>
#include <stl_ext/thread_group.h>
#include <stl_ext/string_algo.h>
Expand Down Expand Up @@ -384,6 +386,27 @@ void threadedMovieInit()
#endif
}

//
// Runs on the writer thread once it has rendered the last frame. The GL
// context is current on that thread, and making it current on the main
// thread fails (BadAccess on GLX and EGL, silently on WGL).
//

void threadedMovieFinalize()
{
// clean up any frame buffers we allocated writing the movie
MovieRV::uninit();

//
// Delete the pixel buffer objects while the GL context is still
// current. Left to the pools' static destructors, glDeleteBuffers
// runs after the GL library has shut down, which crashes at exit
// with Mesa.
//

TwkGLF::DeletePBOPoolBuffers();
}

TwkMovie::Movie* makeLeaderMovie(MovieWriter::WriteRequest& writeRequest, TwkMovie::Movie* omov, Mu::MuLangContext* context,
Mu::Process* process)
{
Expand Down Expand Up @@ -981,13 +1004,7 @@ int utf8Main(int argc, char* argv[])
setEnvVar("LC_ALL", "C");
TwkFB::ThreadPool::initialize();

//
// XXX dummyDev is leaking here. Best would be to pass it to the App so
// that it could delete it after startup, since it's no longer needed at
// that point.
//

TwkGLF::FBOVideoDevice* dummyDev = new TwkGLF::FBOVideoDevice(0, 10, 10, false);
auto dummyDev = std::make_unique<TwkGLF::FBOVideoDevice>(nullptr, 10, 10, false);
IPCore::ImageRenderer::queryGL();
const char* glVersion = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
IPCore::Shader::Function::useShadingLanguageVersion(glVersion);
Expand Down Expand Up @@ -1662,13 +1679,13 @@ int utf8Main(int argc, char* argv[])
#ifdef WIN32
GC_INIT();
GC_allow_register_threads();
outmov = new ThreadedMovie(inputMovies, outFrames, 8, 0, threadedMovieInit);
outmov = new ThreadedMovie(inputMovies, outFrames, 8, 0, threadedMovieInit, threadedMovieFinalize);
#else
ThreadedMovie::ThreadAPI threadAPI;
threadAPI.create = GC_pthread_create;
threadAPI.join = GC_pthread_join;
threadAPI.detach = GC_pthread_detach;
outmov = new ThreadedMovie(inputMovies, outFrames, 8, &threadAPI, threadedMovieInit);
outmov = new ThreadedMovie(inputMovies, outFrames, 8, &threadAPI, threadedMovieInit, threadedMovieFinalize);
#endif
#endif

Expand Down Expand Up @@ -1770,19 +1787,6 @@ int utf8Main(int argc, char* argv[])

if (!writer->write(outmov, outfile, writeRequest))
exit(-2);

// clean up any frame buffers we allocated writing the movie
//
MovieRV::uninit();

//
// Delete the pixel buffer objects while the GL context is still
// current. Left to the pools' static destructors, glDeleteBuffers
// runs after the GL library has shut down, which crashes at exit
// with Mesa.
//

TwkGLF::DeletePBOPoolBuffers();
}
catch (TwkExc::Exception& exc)
{
Expand All @@ -1809,5 +1813,39 @@ int utf8Main(int argc, char* argv[])
TwkFB::GenericIO::shutdown(); // Shutdown TwkFB::GenericIO plugins

TwkFB::ThreadPool::shutdown();

#ifdef PLATFORM_LINUX
//
// Workaround for the libglvnd <-> NVIDIA driver shutdown race.
//
// Returning normally from main() triggers libc's exit handlers, which
// run _dl_fini. _dl_fini invokes each linked library's destructor,
// including libGLX.so.0's __glXFini, which calls
// __glXMappingTeardown -> dlclose("libGLX_nvidia.so.0").
//
// Note: libglvnd (GL Vendor-Neutral Dispatch library) is the dispatch
// layer that sits between an application's OpenGL/GLX/EGL calls
// and the actual vendor driver on Linux.
//
// The NVIDIA closed-source driver spawns internal helper threads
// (idle/event loops inside libnvidia-glcore.so) that libglvnd has no
// hook to join. The dlclose therefore unmaps libnvidia-glcore.so while
// those threads are still running, and their next instruction fetch
// lands on an unmapped page -> SIGSEGV at process exit.
//
// Calling std::_Exit(0) instead of returning skips atexit/_dl_fini and
// lets the kernel reap memory, threads, and file descriptors directly.
// This is safe here: by this point rvio's own shutdown is complete
// (the output movie is closed, MovieRV/GenericIO/ThreadPool have all
// been torn down), so there is no remaining cleanup that the exit
// handlers would have done for us, except flushing stdio: _Exit
// doesn't, and output redirected to a file or pipe would be lost.
//
std::cout.flush();
std::cerr.flush();
std::fflush(nullptr);
std::_Exit(0);
#endif

return 0;
}
12 changes: 11 additions & 1 deletion src/lib/image/TwkMovie/ThreadedMovie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ namespace TwkMovie
mov->threadMain();
}

ThreadedMovie::ThreadedMovie(const Movies& movies, const Frames& frames, size_t stackMultiplier, ThreadAPI* api, InitializeFunc F)
ThreadedMovie::ThreadedMovie(const Movies& movies, const Frames& frames, size_t stackMultiplier, ThreadAPI* api, InitializeFunc F,
FinalizeFunc finalizeFunction)
: m_movies(movies)
, m_threadGroup(movies.size(), stackMultiplier, api)
, m_frames(frames)
, m_init(true)
, m_currentIndex(0)
, m_requestIndex(0)
, m_initialize(F)
, m_finalize(finalizeFunction)
{
// if (!m_movie->isThreadSafe()) throw runtime_exception();
m_info = movies.front()->info();
Expand Down Expand Up @@ -188,8 +190,16 @@ namespace TwkMovie

m_threadGroup.lock(m_runLock);
td->running = false;

bool allFramesDone = (m_currentIndex >= m_frames.size());

m_threadGroup.unlock(m_runLock);

if (allFramesDone && m_finalize != nullptr)
{
m_finalize();
}

// cout << "thread " << td->id << " no longer running" << endl;
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/image/TwkMovie/TwkMovie/ThreadedMovie.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ namespace TwkMovie
typedef std::vector<Movie*> Movies;
typedef stl_ext::thread_group::thread_api ThreadAPI;
typedef void (*InitializeFunc)();
using FinalizeFunc = void (*)();

ThreadedMovie(const Movies&, const Frames& frames, size_t stackMultiplier = 8, ThreadAPI* api = 0, InitializeFunc = NULL);
ThreadedMovie(const Movies&, const Frames& frames, size_t stackMultiplier = 8, ThreadAPI* api = nullptr, InitializeFunc = nullptr,
FinalizeFunc = nullptr);

virtual ~ThreadedMovie();

Expand Down Expand Up @@ -104,6 +106,7 @@ namespace TwkMovie
int m_requestIndex;
bool m_init;
InitializeFunc m_initialize;
FinalizeFunc m_finalize;
};

} // namespace TwkMovie
Expand Down
Loading