Skip to content

Commit bbdff03

Browse files
authored
Merge pull request #1361 from thewtex/pthread-available
pthread available
2 parents 07179af + 0f7079f commit bbdff03

File tree

8 files changed

+70
-3
lines changed

8 files changed

+70
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { demoServer, pipelineBaseUrl, pipelineWorkerUrl } from '../common'
2+
3+
describe('pthreadSupportAvailable', () => {
4+
beforeEach(() => {
5+
cy.visit(demoServer)
6+
cy.window().then(async (win) => {
7+
const itk = win.itk
8+
itk.setPipelineWorkerUrl(pipelineWorkerUrl)
9+
itk.setPipelinesBaseUrl(pipelineBaseUrl)
10+
})
11+
})
12+
13+
it('reports whether pthread support is available', () => {
14+
cy.window().then(async (win) => {
15+
const itk = win.itk
16+
17+
const pthreadSupportAvailable = itk.pthreadSupportAvailable()
18+
expect(pthreadSupportAvailable).to.equal(false)
19+
})
20+
})
21+
})

packages/core/typescript/itk-wasm/src/pipeline/index-common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { default as pthreadSupportAvailable } from './pthread-support-available.js'
2+
13
export type { default as ItkWasmEmscriptenModule } from './itk-wasm-emscripten-module.js'
24

35
export type { default as PipelineInput } from './pipeline-input.js'

packages/core/typescript/itk-wasm/src/pipeline/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// itk-wasm Browser pipeline functions
1+
// ITK-Wasm Browser pipeline functions
22

33
export { default as runPipeline } from './run-pipeline.js'
44
export { default as createWebWorker } from './create-web-worker.js'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function pthreadSupportAvailable(): boolean {
2+
const haveSharedArrayBuffer =
3+
typeof globalThis.SharedArrayBuffer === 'function'
4+
5+
// Emscripten ENVIRONMENT_IS_NODE
6+
const isNode =
7+
typeof process === 'object' &&
8+
typeof process.versions === 'object' &&
9+
typeof process.versions.node === 'string' &&
10+
// @ts-ignore: ts(2339)
11+
process.type !== 'renderer'
12+
13+
const isCrossOriginIsolated =
14+
typeof crossOriginIsolated !== 'undefined'
15+
? crossOriginIsolated
16+
: globalThis.crossOriginIsolated || false
17+
18+
if (isNode) {
19+
return haveSharedArrayBuffer
20+
}
21+
22+
return haveSharedArrayBuffer && isCrossOriginIsolated
23+
}
24+
25+
export default pthreadSupportAvailable
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'ava'
2+
3+
import { pthreadSupportAvailable } from '../../../dist/index-node.js'
4+
5+
test('node pthread support available', (t) => {
6+
t.truthy(pthreadSupportAvailable())
7+
})

packages/core/typescript/itk-wasm/test/pipelines/typescript/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@
3636
"itk-wasm": "workspace:*"
3737
},
3838
"devDependencies": {
39+
"@itk-wasm/demo-app": "workspace:*",
3940
"@itk-wasm/image-io": "workspace:*",
4041
"@itk-wasm/mesh-io": "workspace:*",
4142
"@itk-wasm/transform-io": "workspace:*",
42-
"@itk-wasm/demo-app": "workspace:*",
4343
"@types/node": "^20.2.5",
4444
"esbuild": "^0.25.0",
4545
"shx": "^0.3.4",
4646
"typescript": "^5.3.2",
4747
"vite": "^4.5.11",
48+
"vite-plugin-cross-origin-isolation": "^0.1.6",
4849
"vite-plugin-static-copy": "^0.17.0"
4950
}
50-
}
51+
}

packages/core/typescript/itk-wasm/test/pipelines/typescript/vite.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig } from 'vite'
22
import { viteStaticCopy } from 'vite-plugin-static-copy'
3+
import crossOriginIsolation from 'vite-plugin-cross-origin-isolation'
34
import path from 'path'
45

56
const base = process.env.VITE_BASE_URL || '/'
@@ -21,6 +22,8 @@ export default defineConfig({
2122
exclude: ['itk-wasm', '@itk-wasm/image-io', '@itk-wasm/mesh-io', '@itk-wasm/transform-io', '@thewtex/zstddec']
2223
},
2324
plugins: [
25+
// wasm threading
26+
crossOriginIsolation(),
2427
// put lazy loaded JavaScript and Wasm bundles in dist directory
2528
viteStaticCopy({
2629
targets: [

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)