Skip to content

Support for multi threading cores#75

Merged
arianrhodsandlot merged 2 commits intoarianrhodsandlot:mainfrom
perrello:pthreads
Jan 18, 2026
Merged

Support for multi threading cores#75
arianrhodsandlot merged 2 commits intoarianrhodsandlot:mainfrom
perrello:pthreads

Conversation

@perrello
Copy link
Contributor

This MR adds the support for multi threaded cores.
It does not change the workflow dramatically but a few thing need to be added by the Nostalgist user to make it work.

Multithreaded cores resolve there own files, so two Emscripten hooks need to be used in the launch configs.

            emscriptenModule: {
                mainScriptUrlOrBlob: `/cores/${core}/threads/${core}.js`,
                locateFile: (path) => {
                    if (path.endsWith('.worker.js')) return `/cores/${core}/threads/${core}.worker.js`;
                    if (path.endsWith('.wasm')) return `/cores/${core}/threads/${core}.wasm`;
                    return path;
                }
            }

Note that this doesnt change the use or possible requirement of resolveCoreWasm and resolveCoreJs in the config.

Other than that the page serving the emulation must be cross origin isolated. This can be done by adding headers to the page like:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

@arianrhodsandlot
Copy link
Owner

Are there any ways to test against this change, like with a precompiled core somewhere?

@perrello
Copy link
Contributor Author

Its hard to test because the environment needs to be cross origin isolated. I did however test it in Foxol with some cores i compiled. What would be a good strategy here?

@arianrhodsandlot
Copy link
Owner

It's ok to not add an automated test case right now. But it's better to have a button in the playground for manually testing, so we can make sure it's actually working.
As I tried, add a playground/vite.config.ts with following content then pnpm dev will launch a vite dev server with desired HTTP headers:

import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    headers: {
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
    },
  },
})

The problem is where would the test threaded cores come from. Are your cores open for test usage here? Or are there any existing cores on GitHub or somewhere else we can use for testing?

@arianrhodsandlot arianrhodsandlot added the enhancement New feature or request label Jan 17, 2026
@perrello
Copy link
Contributor Author

I couldnt get the cross origin isolated vite server to run. Would you mind adding it? I added a zip file containing one of the cores i compiled
mgba-multithread-foxol-1.0.3.zip

@arianrhodsandlot
Copy link
Owner

I extracted your cores to playground/public and add following code along with a vite.config.ts

diff --git a/playground/activate.ts b/playground/activate.ts
index c93c2b4..33e2f71 100644
--- a/playground/activate.ts
+++ b/playground/activate.ts
@@ -27,6 +27,29 @@ const handlers = {
       nostalgist = await Nostalgist.gbc('combatsoccer.gbc')
     },
 
+    async gbaThreaded() {
+      nostalgist = await Nostalgist.launch({
+        core: {
+          js: '/mgba.js',
+          name: 'mgba',
+          wasm: '/mgba.wasm',
+        },
+        emscriptenModule: {
+          locateFile: (path) => {
+            if (path.endsWith('.worker.js')) {
+              return '/mgba.worker.js'
+            }
+            if (path.endsWith('.wasm')) {
+              return '/mgba.wasm'
+            }
+            return path
+          },
+          mainScriptUrlOrBlob: '/mgba.js',
+        },
+        rom: 'eliminator.gba',
+      })
+    },
+
     async launchSize() {
       nostalgist = await Nostalgist.gbc({ rom: 'combatsoccer.gbc', size: { height: 100, width: 100 } })
     },

The game was launched successfully though there are some errors printed. But I believe it's not related to these changes.
image

I misunderstood the changes and now I see it's just giving users more space to tweak the emscripten object. It's essentially not bound to working with threaded cores. Thanks for contributing!

@arianrhodsandlot arianrhodsandlot merged commit 17f5262 into arianrhodsandlot:main Jan 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants