Skip to content

Commit

Permalink
wilk#42 shared memory support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Botvin committed Oct 25, 2019
1 parent 639b53f commit a00474e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
21 changes: 21 additions & 0 deletions __tests__/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { job, stop, start } from '../src/job'

beforeAll(async () => await start())
afterAll(async () => await stop())

describe('Job Shared Testing', () => {
it('should mutate shared inside job', async () => {
const sab = new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT)
const shared = new Float64Array(sab)
shared[0] = 1

await job(
() => {
shared[0] = 2
},
{ shared }
)

expect(shared[0]).toEqual(2)
})
})
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Worker } from 'worker_threads'

export interface Config<T = {}, U = {}> {
export interface Config<T = {}, U = {}, V = {}> {
ctx?: T
data?: U
shared?: V
}

export interface Task {
Expand Down
2 changes: 1 addition & 1 deletion src/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class WorkerPool {
this.tick()
})

worker.postMessage(workerStr)
worker.postMessage({worker: workerStr, shared: config.shared})
} catch (err) {
this.free(worker)
reject(err)
Expand Down
2 changes: 1 addition & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const workerFile = `
const { parentPort } = require('worker_threads')
parentPort.on('message', async worker => {
parentPort.on('message', async ({worker, shared}) => {
const response = {
error: null,
data: null
Expand Down

0 comments on commit a00474e

Please sign in to comment.