diff --git a/morph/Visual.h b/morph/Visual.h index 5607ad4c..922133be 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -81,6 +81,9 @@ #include #include +#ifndef OWNED_MODE +#include +#endif // Use Lode Vandevenne's PNG encoder #define LODEPNG_NO_COMPILE_DECODER 1 @@ -261,6 +264,33 @@ namespace morph { // A callback friendly wrapper static void set_context (morph::Visual* _v) { _v->setContext(); }; + //! Lock the context to prevent accessing the OpenGL context from multiple threads + //! then obtain the context. + void lockContext() + { + this->context_.lock(); + this->setContext(); + } + + //! Attempt to lock the context . If the lock is obtained, set the OpenGL context + //! and return true. If the lock is not obtained, return false. + bool tryLockContext() + { + if (this->context_.try_lock()) { + this->setContext(); + return true; + } else { + return false; + } + } + + //! Release the OpenGL context and unlock the context . + void unlockContext() + { + this->releaseContext(); + this->context_mutex.unlock(); + } + //! Release the OpenGL context void releaseContext() { glfwMakeContextCurrent (nullptr); } // A callback friendly wrapper @@ -1070,6 +1100,11 @@ namespace morph { //! The window (and OpenGL context) for this Visual morph::win_t* window = nullptr; +#ifndef OWNED_MODE + //! Context mutex to prevent contexts being acquired in a non-threadsafe manner. + std::mutex context_mutex; +#endif + //! Current window width int window_w = 640; //! Current window height @@ -1256,6 +1291,7 @@ namespace morph { std::cout << "Shift-Down: Halve cyl proj radius\n"; std::cout << "Ctrl-Up: Double cyl proj height\n"; std::cout << "Ctrl-Down: Halve cyl proj height\n"; + std::cout << std::flush; } if (_key == key::l && (mods & keymod::control) && action == keyaction::press) {