summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Common/Assert.hpp6
-rw-r--r--src/Compute/Queue.hpp3
-rw-r--r--src/GFX/Window.cpp7
-rw-r--r--src/Game.cpp2
-rw-r--r--src/Render.cpp3
-rw-r--r--src/ThreadRole.hpp30
-rw-r--r--src/main.cpp3
7 files changed, 50 insertions, 4 deletions
diff --git a/src/Common/Assert.hpp b/src/Common/Assert.hpp
index 9fc84ce..d30e724 100644
--- a/src/Common/Assert.hpp
+++ b/src/Common/Assert.hpp
@@ -1,6 +1,8 @@
 #pragma once
 
 #include <csignal>
+#include <cstdio>
+#include <cstring>
 
 // https://stackoverflow.com/a/26100478/11342122
 // Two levels are needed to make sure that the argument is expanded before stringification
@@ -12,8 +14,8 @@
 #define _ASSERT_STOP (_ASSERT_IS_DEFINED(NDEBUG) ? std::abort() : (void)raise(SIGTRAP))
 
 // Assertion message macros, with optional message
-#define _ASSERT_NOTIFY_NO_MESSAGE(start) std::fprintf(stderr, start ".\n", __FILE__, __LINE__)
-#define _ASSERT_NOTIFY_WITH_MESSAGE(start, message) std::fprintf(stderr, start ": %s\n", __FILE__, __LINE__, message "")
+#define _ASSERT_NOTIFY_NO_MESSAGE(start) fprintf(stderr, start ".\n", __FILE__, __LINE__)
+#define _ASSERT_NOTIFY_WITH_MESSAGE(start, message) fprintf(stderr, start ": %s\n", __FILE__, __LINE__, message "")
 #define _ASSERT_NOTIFY(start, ...) ((strcmp(__VA_ARGS__ "", "") == 0) ? _ASSERT_NOTIFY_NO_MESSAGE(start) : _ASSERT_NOTIFY_WITH_MESSAGE(start, __VA_ARGS__))
 
 // Debuggable assertion macro, with optional message
diff --git a/src/Compute/Queue.hpp b/src/Compute/Queue.hpp
index 799df73..3c4ec08 100644
--- a/src/Compute/Queue.hpp
+++ b/src/Compute/Queue.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "../Common/Sizes.hpp"
+#include "../ThreadRole.hpp"
 #include <functional>
 #include <mutex>
 #include <vector>
@@ -107,6 +108,8 @@ private:
     [[noreturn]] static void run_thread(std::shared_ptr<Control> control) {
         using namespace std::chrono_literals;
 
+        HELLO_I_AM(ThreadRole::Worker);
+
         while (true) {
             Bool nothing_to_do = true;
             Job job;
diff --git a/src/GFX/Window.cpp b/src/GFX/Window.cpp
index c0c5b03..33e76d5 100644
--- a/src/GFX/Window.cpp
+++ b/src/GFX/Window.cpp
@@ -1,12 +1,13 @@
 #include <stdexcept>
 #include "../Common/Sizes.hpp"
-#include "Window.hpp"
-
 #include "../Common/Assert.hpp"
+#include "../ThreadRole.hpp"
+#include "Window.hpp"
 
 namespace MC::GFX {
 
 Window::Window(const Char* title, U32 width, U32 height) {
+    ASSERT_MAIN_THREAD();
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@@ -23,6 +24,7 @@ Window::Window(const Char* title, U32 width, U32 height) {
 }
 
 Window::~Window() {
+    ASSERT_MAIN_THREAD();
     glfwDestroyWindow(m_window);
 }
 
@@ -55,6 +57,7 @@ void Window::start_render() {
 }
 
 void Window::poll_events() {
+    ASSERT_MAIN_THREAD();
     glfwPollEvents();
 }
 
diff --git a/src/Game.cpp b/src/Game.cpp
index 117ba09..231c681 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -12,6 +12,8 @@
 namespace MC {
 
 void Game::run() const {
+    ASSERT_MAIN_THREAD();
+
     World::World world{};
 
     GFX::Camera camera{};
diff --git a/src/Render.cpp b/src/Render.cpp
index 3955f1c..2b16ed0 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -1,6 +1,7 @@
 #include "Render.hpp"
 #include "Assets.hpp"
 #include "Defines.hpp"
+#include "ThreadRole.hpp"
 #include "Time.hpp"
 #include "Common/Assert.hpp"
 #include "GFX/Image/PPMParser.hpp"
@@ -10,6 +11,8 @@
 namespace MC {
 
 void Render::run() {
+    HELLO_I_AM(ThreadRole::Render);
+
     m_window.attach();
     setup_gl();
 
diff --git a/src/ThreadRole.hpp b/src/ThreadRole.hpp
new file mode 100644
index 0000000..4c668e5
--- /dev/null
+++ b/src/ThreadRole.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include "Common/Assert.hpp"
+
+namespace MC {
+
+enum class ThreadRole {
+    None,
+    Logic,
+    Render,
+    Worker
+};
+
+// Hide the global from direct access
+namespace ThreadRoleDetail {
+    thread_local inline ThreadRole g_thread_role = ThreadRole::None;
+}
+
+#define THREAD_ROLE (MC::ThreadRoleDetail::g_thread_role)
+
+#define HELLO_I_AM(role) do { \
+    ASSERT(THREAD_ROLE == MC::ThreadRole::None, "Thread role already set"); \
+    THREAD_ROLE = (role); \
+} while (0)
+
+#define ASSERT_MAIN_THREAD() ASSERT(THREAD_ROLE == MC::ThreadRole::Logic, "Thread is not main")
+#define ASSERT_THREAD_IS(role) ASSERT(THREAD_ROLE == (role), "Thread role mismatch")
+#define ASSERT_THREAD_IS_NOT(role) ASSERT(THREAD_ROLE != (role), "Thread role mismatch")
+
+}
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 9153530..94061f1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,9 +4,12 @@
 
 #include "Game.hpp"
 #include "Render.hpp"
+#include "ThreadRole.hpp"
 #include "World/World.hpp"
 
 int main() {
+    HELLO_I_AM(MC::ThreadRole::Logic);
+
     if (!glfwInit()) {
         std::cout << "Failed to initialize GLFW" << std::endl;
         return 1;