Skip to content

Commit

Permalink
Merge branch 'connagh-opteran-feature/lockable-contexts'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebjameswml committed Dec 2, 2024
2 parents 43a108d + b974a19 commit cc9258e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions morph/Visual.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
#include <cstddef>

#include <morph/VisualDefaultShaders.h>
#ifndef OWNED_MODE
#include <mutex>
#endif

// Use Lode Vandevenne's PNG encoder
#define LODEPNG_NO_COMPILE_DECODER 1
Expand Down Expand Up @@ -261,6 +264,33 @@ namespace morph {
// A callback friendly wrapper
static void set_context (morph::Visual<glver>* _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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cc9258e

Please sign in to comment.