Skip to content

Commit 1678544

Browse files
committed
threads: Bump version to 0.2.1 and feat multi_memory
1 parent ed1a045 commit 1678544

File tree

4 files changed

+55
-39
lines changed

4 files changed

+55
-39
lines changed

threads/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oligami/browser_wasi_shim-threads",
3-
"version": "0.1.6",
3+
"version": "0.2.1",
44
"type": "module",
55
"scripts": {
66
"dev": "vite",

threads/src/animals.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ export class WASIFarmAnimal {
122122
throw new Error("what happened?");
123123
}
124124

125-
const view = new Uint8Array(this.get_share_memory().buffer);
126-
view.fill(0);
125+
const memories = this.thread_spawner.get_share_memory();
126+
for (const share_memory in memories) {
127+
const view = new Uint8Array(memories[share_memory].buffer);
128+
view.fill(0);
129+
}
127130

128131
await this.thread_spawner.async_start_on_thread(
129132
this.args,
@@ -151,8 +154,11 @@ export class WASIFarmAnimal {
151154
throw new Error("what happened?");
152155
}
153156

154-
const view = new Uint8Array(this.get_share_memory().buffer);
155-
view.fill(0);
157+
const memories = this.thread_spawner.get_share_memory();
158+
for (const share_memory in memories) {
159+
const view = new Uint8Array(memories[share_memory].buffer);
160+
view.fill(0);
161+
}
156162

157163
console.log("block_start_on_thread: start");
158164

@@ -335,7 +341,9 @@ export class WASIFarmAnimal {
335341
}
336342
}
337343

338-
get_share_memory(): WebAssembly.Memory {
344+
get_share_memory(): {
345+
[key: string]: WebAssembly.Memory;
346+
} {
339347
if (!this.thread_spawner) {
340348
throw new Error("thread_spawner is not defined");
341349
}
@@ -353,7 +361,9 @@ export class WASIFarmAnimal {
353361
thread_spawn_wasm?: WebAssembly.Module;
354362
hand_override_fd_map?: Array<[number, number]>;
355363
worker_background_worker_url?: string;
356-
share_memory?: WebAssembly.Memory;
364+
share_memory?: {
365+
[key: string]: WebAssembly.Memory;
366+
};
357367
} = {},
358368
override_fd_maps?: Array<number[]>,
359369
thread_spawner?: ThreadSpawner,

threads/src/park.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export abstract class WASIFarmPark {
3535

3636
private get_new_fd_lock = new Array<() => Promise<void>>();
3737

38-
// fdに対して、現在そのfdにidがアクセス可能かを示す。
38+
// Indicates whether the given id currently has access to the fd.
3939
protected fds_map: Array<number[]>;
4040

4141
// If the reassigned value is accessed after being closed,

threads/src/shared_array_buffer/thread_spawn.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ import {
2424
import { WorkerBackgroundRefObjectConstructor } from "./worker_background/worker_export.ts";
2525

2626
type ThreadSpawnerObject = {
27-
share_memory: WebAssembly.Memory;
27+
share_memory: {
28+
[key: string]: WebAssembly.Memory;
29+
};
2830
wasi_farm_refs_object: Array<WASIFarmRefObject>;
2931
worker_url: string;
3032
worker_background_ref_object: WorkerBackgroundRefObject;
3133
// inst_default_buffer_kept: WebAssembly.Memory;
3234
};
3335

3436
export class ThreadSpawner {
35-
private share_memory: WebAssembly.Memory;
37+
private share_memory: {
38+
[key: string]: WebAssembly.Memory;
39+
};
3640
private wasi_farm_refs_object: Array<WASIFarmRefObject>;
3741
private worker_url: string;
3842
private worker_background_ref: WorkerBackgroundRef;
@@ -48,7 +52,9 @@ export class ThreadSpawner {
4852
constructor(
4953
worker_url: string,
5054
wasi_farm_refs_object: Array<WASIFarmRefObject>,
51-
share_memory?: WebAssembly.Memory,
55+
share_memory?: {
56+
[key: string]: WebAssembly.Memory;
57+
},
5258
// 16MB for the time being.
5359
// https://users.rust-lang.org/t/what-is-the-size-limit-of-threads-stack-in-rust/11867/3
5460
MIN_STACK = 16777216,
@@ -60,31 +66,27 @@ export class ThreadSpawner {
6066
this.worker_url = worker_url;
6167
this.wasi_farm_refs_object = wasi_farm_refs_object;
6268

63-
const min_initial_size = 1048576 / 65536; // Rust's default stack size is 1MB.
64-
const initial_size = MIN_STACK / 65536;
65-
if (initial_size < min_initial_size) {
66-
throw new Error(
67-
`The stack size must be at least ${min_initial_size} bytes.`,
68-
);
69-
}
70-
const max_memory = 1073741824 / 65536; // Rust's default maximum memory size is 1GB.
71-
72-
// this.inst_default_buffer_kept =
73-
// inst_default_buffer_kept ||
74-
// new WebAssembly.Memory({
75-
// initial: 1,
76-
// maximum: max_memory,
77-
// shared: true,
78-
// });
79-
80-
this.share_memory =
81-
share_memory ||
69+
if (share_memory === undefined) {
70+
const min_initial_size = 1048576 / 65536; // Rust's default stack size is 1MB.
71+
const initial_size = MIN_STACK / 65536;
72+
if (initial_size < min_initial_size) {
73+
throw new Error(
74+
`The stack size must be at least ${min_initial_size} bytes.`,
75+
);
76+
}
77+
const max_memory = 1073741824 / 65536; // Rust's default maximum memory size is 1GB.
78+
8279
// WebAssembly.Memory's 1 page is 65536 bytes.
83-
new WebAssembly.Memory({
84-
initial: initial_size,
85-
maximum: max_memory,
86-
shared: true,
87-
});
80+
share_memory = {
81+
memory: new WebAssembly.Memory({
82+
initial: initial_size,
83+
maximum: max_memory,
84+
shared: true,
85+
}),
86+
};
87+
}
88+
89+
this.share_memory = share_memory;
8890

8991
if (worker_background_ref_object === undefined) {
9092
let worker_background_worker_url__: string;
@@ -246,7 +248,9 @@ export class ThreadSpawner {
246248
return thread_spawner;
247249
}
248250

249-
get_share_memory(): WebAssembly.Memory {
251+
get_share_memory(): {
252+
[key: string]: WebAssembly.Memory;
253+
} {
250254
return this.share_memory;
251255
}
252256

@@ -299,7 +303,9 @@ export const thread_spawn_on_worker = async (
299303
instantiate: (
300304
thread_spawn_wasm: WebAssembly.Module,
301305
imports: {
302-
env: { memory: WebAssembly.Memory };
306+
env: {
307+
[key: string]: WebAssembly.Memory;
308+
};
303309
wasi: {
304310
"thread-spawn": (start_arg: number) => number;
305311
};
@@ -359,7 +365,7 @@ export const thread_spawn_on_worker = async (
359365

360366
const inst = await instantiate(thread_spawn_wasm, {
361367
env: {
362-
memory: wasi.get_share_memory(),
368+
...wasi.get_share_memory(),
363369
},
364370
wasi: wasi.wasiThreadImport,
365371
wasi_snapshot_preview1: wasi.wasiImport,
@@ -409,7 +415,7 @@ export const thread_spawn_on_worker = async (
409415

410416
const inst = await instantiate(thread_spawn_wasm, {
411417
env: {
412-
memory: wasi.get_share_memory(),
418+
...wasi.get_share_memory(),
413419
},
414420
wasi: wasi.wasiThreadImport,
415421
wasi_snapshot_preview1: wasi.wasiImport,

0 commit comments

Comments
 (0)