From dc3009bf54122f2cb8bef5d446f244a1f68d7453 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 10 Sep 2025 01:04:56 +0100 Subject: [PATCH] create separate mailbox structures for draw and input messages, because our plan is to: 'handle input + drawing on a single thread' --- shell/draw-mailbox.sml | 1 + shell/input-mailbox.sml | 1 + shell/make-mailbox.sml | 29 +++++++++++++++++++++++++++++ shf.mlb | 8 ++++++++ 4 files changed, 39 insertions(+) create mode 100644 shell/draw-mailbox.sml create mode 100644 shell/input-mailbox.sml create mode 100644 shell/make-mailbox.sml diff --git a/shell/draw-mailbox.sml b/shell/draw-mailbox.sml new file mode 100644 index 0000000..92bca3e --- /dev/null +++ b/shell/draw-mailbox.sml @@ -0,0 +1 @@ +structure DrawMailbox = MakeMailbox(DrawMsg) diff --git a/shell/input-mailbox.sml b/shell/input-mailbox.sml new file mode 100644 index 0000000..8907262 --- /dev/null +++ b/shell/input-mailbox.sml @@ -0,0 +1 @@ +structure InputMailbox = MakeMailbox(InputMsg) diff --git a/shell/make-mailbox.sml b/shell/make-mailbox.sml new file mode 100644 index 0000000..ba37eca --- /dev/null +++ b/shell/make-mailbox.sml @@ -0,0 +1,29 @@ +signature MAKE_MAILBOX = +sig + type t +end + +functor MakeMailbox(Fn: MAKE_MAILBOX) = +struct + val messages: Fn.t vector ref = ref #[] + + fun getMessagesAndClear () = + let + val () = MLton.Thread.atomicBegin () + val msgs = !messages + val () = messages := #[] + val () = MLton.Thread.atomicEnd () + in + msgs + end + + fun append newMsg = + let + val () = MLton.Thread.atomicBegin () + val msgs = !messages + val msgs = Vector.concat [msgs, #[newMsg]] + val () = messages := msgs + in + MLton.Thread.atomicEnd () + end +end diff --git a/shf.mlb b/shf.mlb index f3ca75b..0fd73e6 100644 --- a/shf.mlb +++ b/shf.mlb @@ -66,6 +66,14 @@ in ffi/glfw-input.sml end +ann + "allowVectorExps true" +in + shell/make-mailbox.sml +end +shell/input-mailbox.sml +shell/draw-mailbox.sml + shell/exception-logger.sml shell/search-thread.sml shell/update-thread.sml