From 28ee884888bce35f7ac71e3cee961e83b17c5829 Mon Sep 17 00:00:00 2001 From: Connagh Jacobi Date: Mon, 2 Dec 2024 11:23:50 +0000 Subject: [PATCH 1/9] add a lockable context --- morph/Visual.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/morph/Visual.h b/morph/Visual.h index 0c1e6d36..0ea5e57b 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 @@ -255,6 +258,30 @@ namespace morph { // A callback friendly wrapper static void set_context (morph::Visual* _v) { _v->setContext(); }; + //! Lock the context mutext to prevent locking across multiple. Then sets the context. + void lockContext() { + context_mutex.lock(); + setContext(); + } + + //! Attempt to lock the context mutext returns if locking was successful. + //! If success sets the context. + bool tryLockContext() { + if(context_mutex.try_lock()){ + set_context(); + return true; + } else { + return false; + } + + } + + //! releases the context and unlocks the context mutex. + void unlockContext() { + releaseContext(); + context_mutex.unlock(); + } + //! Release the OpenGL context void releaseContext() { glfwMakeContextCurrent (nullptr); } #endif @@ -1030,6 +1057,11 @@ namespace morph { //! The window (and OpenGL context) for this Visual morph::win_t* window = nullptr; +#ifndef OWNED_MODE + //! Context mutext to prevent contexts being aquired in a none threadsafe manner. + std::mutex context_mutex; +#endif + //! Current window width int window_w = 640; //! Current window height @@ -1216,6 +1248,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) { From b37cf3e03d3c9eb79e153281bb44c7eb6873738c Mon Sep 17 00:00:00 2001 From: Connagh Jacobi Date: Mon, 2 Dec 2024 14:33:48 +0000 Subject: [PATCH 2/9] fix style and set_context top setContext --- morph/Visual.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index 0ea5e57b..2c208494 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -267,19 +267,18 @@ namespace morph { //! Attempt to lock the context mutext returns if locking was successful. //! If success sets the context. bool tryLockContext() { - if(context_mutex.try_lock()){ - set_context(); - return true; - } else { - return false; - } - + if(context_mutex.try_lock()){ + setContext(); + return true; + } else { + return false; + } } //! releases the context and unlocks the context mutex. void unlockContext() { - releaseContext(); - context_mutex.unlock(); + releaseContext(); + context_mutex.unlock(); } //! Release the OpenGL context From cf41546fb83f885f4f0c447bed5181c9e6c10c7a Mon Sep 17 00:00:00 2001 From: Connagh Jacobi Date: Mon, 2 Dec 2024 14:34:36 +0000 Subject: [PATCH 3/9] add use of this for clarity --- morph/Visual.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index 2c208494..203e0b6e 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -260,14 +260,14 @@ namespace morph { //! Lock the context mutext to prevent locking across multiple. Then sets the context. void lockContext() { - context_mutex.lock(); + this->context_mutex.lock(); setContext(); } //! Attempt to lock the context mutext returns if locking was successful. //! If success sets the context. bool tryLockContext() { - if(context_mutex.try_lock()){ + if(this->context_mutex.try_lock()){ setContext(); return true; } else { @@ -278,7 +278,7 @@ namespace morph { //! releases the context and unlocks the context mutex. void unlockContext() { releaseContext(); - context_mutex.unlock(); + this->context_mutex.unlock(); } //! Release the OpenGL context From 546082f2709c3345435c4678154e6d81cf632a43 Mon Sep 17 00:00:00 2001 From: Connagh Jacobi Date: Mon, 2 Dec 2024 14:35:18 +0000 Subject: [PATCH 4/9] add to function calls --- morph/Visual.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index 203e0b6e..f255719c 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -261,14 +261,14 @@ namespace morph { //! Lock the context mutext to prevent locking across multiple. Then sets the context. void lockContext() { this->context_mutex.lock(); - setContext(); + this->setContext(); } //! Attempt to lock the context mutext returns if locking was successful. //! If success sets the context. bool tryLockContext() { if(this->context_mutex.try_lock()){ - setContext(); + this->setContext(); return true; } else { return false; @@ -277,7 +277,7 @@ namespace morph { //! releases the context and unlocks the context mutex. void unlockContext() { - releaseContext(); + this->releaseContext(); this->context_mutex.unlock(); } From e30727c6000aed90fea7b8e1cc5d42aa6572ee8d Mon Sep 17 00:00:00 2001 From: Connagh Jacobi Date: Mon, 2 Dec 2024 14:37:28 +0000 Subject: [PATCH 5/9] more styling --- morph/Visual.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index f255719c..c059a430 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -259,14 +259,16 @@ namespace morph { static void set_context (morph::Visual* _v) { _v->setContext(); }; //! Lock the context mutext to prevent locking across multiple. Then sets the context. - void lockContext() { + void lockContext() + { this->context_mutex.lock(); this->setContext(); } //! Attempt to lock the context mutext returns if locking was successful. //! If success sets the context. - bool tryLockContext() { + bool tryLockContext() + { if(this->context_mutex.try_lock()){ this->setContext(); return true; @@ -276,7 +278,8 @@ namespace morph { } //! releases the context and unlocks the context mutex. - void unlockContext() { + void unlockContext() + { this->releaseContext(); this->context_mutex.unlock(); } From 0ad167a14462698a8f2fdd56a2900c192b531a56 Mon Sep 17 00:00:00 2001 From: Seb James Date: Mon, 2 Dec 2024 16:15:42 +0000 Subject: [PATCH 6/9] Remove extraneous trailing whitespace --- morph/Visual.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index c059a430..b02671d8 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -83,7 +83,7 @@ #include #ifndef OWNED_MODE #include -#endif +#endif // Use Lode Vandevenne's PNG encoder #define LODEPNG_NO_COMPILE_DECODER 1 @@ -259,29 +259,29 @@ namespace morph { static void set_context (morph::Visual* _v) { _v->setContext(); }; //! Lock the context mutext to prevent locking across multiple. Then sets the context. - void lockContext() - { - this->context_mutex.lock(); + void lockContext() + { + this->context_mutex.lock(); this->setContext(); } - + //! Attempt to lock the context mutext returns if locking was successful. //! If success sets the context. bool tryLockContext() - { - if(this->context_mutex.try_lock()){ + { + if (this->context_mutex.try_lock()) { this->setContext(); return true; } else { return false; } - } + } //! releases the context and unlocks the context mutex. void unlockContext() { this->releaseContext(); - this->context_mutex.unlock(); + this->context_mutex.unlock(); } //! Release the OpenGL context From 5bea4aecb4f8492f284fa7ac1753f92d866a2215 Mon Sep 17 00:00:00 2001 From: Seb James Date: Mon, 2 Dec 2024 16:25:56 +0000 Subject: [PATCH 7/9] Docs strings --- morph/Visual.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index b02671d8..319d2739 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -258,15 +258,16 @@ namespace morph { // A callback friendly wrapper static void set_context (morph::Visual* _v) { _v->setContext(); }; - //! Lock the context mutext to prevent locking across multiple. Then sets the context. + //! Lock the context mutex to prevent accessing the OpenGL context from multiple threads + //! then obtain the context. void lockContext() { this->context_mutex.lock(); this->setContext(); } - //! Attempt to lock the context mutext returns if locking was successful. - //! If success sets the context. + //! Attempt to lock the context mutex. 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_mutex.try_lock()) { @@ -277,7 +278,7 @@ namespace morph { } } - //! releases the context and unlocks the context mutex. + //! Release the OpenGL context and unlock the context mutex. void unlockContext() { this->releaseContext(); From c8a89b8dcfbc508449482f48632fe7a41caeb5b2 Mon Sep 17 00:00:00 2001 From: Seb James Date: Mon, 2 Dec 2024 16:29:15 +0000 Subject: [PATCH 8/9] Tiny typo --- morph/Visual.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/morph/Visual.h b/morph/Visual.h index 319d2739..1e06bad9 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -258,19 +258,19 @@ namespace morph { // A callback friendly wrapper static void set_context (morph::Visual* _v) { _v->setContext(); }; - //! Lock the context mutex to prevent accessing the OpenGL context from multiple threads + //! Lock the context to prevent accessing the OpenGL context from multiple threads //! then obtain the context. void lockContext() { - this->context_mutex.lock(); + this->context_.lock(); this->setContext(); } - //! Attempt to lock the context mutex. If the lock is obtained, set the OpenGL context + //! 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_mutex.try_lock()) { + if (this->context_.try_lock()) { this->setContext(); return true; } else { @@ -278,7 +278,7 @@ namespace morph { } } - //! Release the OpenGL context and unlock the context mutex. + //! Release the OpenGL context and unlock the context . void unlockContext() { this->releaseContext(); @@ -1061,7 +1061,7 @@ namespace morph { morph::win_t* window = nullptr; #ifndef OWNED_MODE - //! Context mutext to prevent contexts being aquired in a none threadsafe manner. + //! Context mutex to prevent contexts being aquired in a none threadsafe manner. std::mutex context_mutex; #endif From 932b51985d87a916880376600a57b5b2264cb16d Mon Sep 17 00:00:00 2001 From: Seb James Date: Mon, 2 Dec 2024 16:30:52 +0000 Subject: [PATCH 9/9] Another tiny typo --- morph/Visual.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/morph/Visual.h b/morph/Visual.h index 1e06bad9..eac3784d 100644 --- a/morph/Visual.h +++ b/morph/Visual.h @@ -1061,7 +1061,7 @@ namespace morph { morph::win_t* window = nullptr; #ifndef OWNED_MODE - //! Context mutex to prevent contexts being aquired in a none threadsafe manner. + //! Context mutex to prevent contexts being acquired in a non-threadsafe manner. std::mutex context_mutex; #endif