Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Web Worker Status #148

Closed
austinfrey opened this issue Jun 22, 2017 · 41 comments
Closed

Web Worker Status #148

austinfrey opened this issue Jun 22, 2017 · 41 comments

Comments

@austinfrey
Copy link
Contributor

There was some initial discussion awhile ago regarding threading and using web workers to accomplish that task. What's the current status? I have a use case where they would be helpful. There are a number of NodeJS implementations of web workers. Could some of those be adapted? Some clues as to where to start would be appreciated. Thanks!

@RossComputerGuy
Copy link

I found out a way to "emulate" Web Workers using the setInterval function, but I'm still working on the design.

@piranna
Copy link

piranna commented Jun 23, 2017 via email

@RossComputerGuy
Copy link

I'm emulating Web Workers and Multitasking using the setIntervals

@piranna
Copy link

piranna commented Jun 23, 2017 via email

@RossComputerGuy
Copy link

This is the experimental code:

function main() {
    console.log("Hello, World");
}

var running = false;
var pid = setInterval(() => {
    if(running) return;
    running = true;
    main();
    while(running);
    clearInterval(pid);
},1000)

@piranna
Copy link

piranna commented Jun 23, 2017

Can you explain what are you trying to achieve with that? It goes directly to a blocking infinite loop and do nothing more...

@RossComputerGuy
Copy link

It is multitasking. To kill the task, set running equal to false, then the loop ends and kills the task fully with the clearInterval function.

@piranna
Copy link

piranna commented Jun 23, 2017

As I told you, you got an infinite loop, so on a single thread you'll get blocked. Do you have the full source code of the proof of concept somewhere?

@RossComputerGuy
Copy link

No, I only have that piece of code

@iefserge
Copy link
Member

It's going to be pretty hard and time consuming to implement, because requires working with multiple CPUs and/or allowing time sharing multitasking. This will dramatically increase complexity of the codebase.
Runtime runs bare-metal, there is no linux(or other kernel) to provide luxuries like threads and multitasking for us :)

I think being single-threaded is fine for the OS that runs in the cloud on virtual CPUs, i.e you can run a separate runtime.js instance per physical CPU and get "multitasking" that way.

@piranna
Copy link

piranna commented Jun 23, 2017

Runtime runs bare-metal, there is no linux(or other kernel) to provide luxuries like threads and multitasking for us :)

That's why I'm talking about of concepts like setjmp/lngjmp :-)

I think being single-threaded is fine for the OS that runs in the cloud on virtual CPUs, i.e you can run a separate runtime.js instance per physical CPU and get "multitasking" that way.

Single threaded is not ok, but it's fine. What we would need is to have several concurrent tasks, and I think "emulate" threads and processes (I think this is how greenlets works) on a single CPU/thread is good enough for now. Far for ideal, but cooperative multitasking would be a good first aproach before moving to preemptive threads/processes so far nobody does a blocking loop, and we could move later in the future to multi-CPU multitasking.

Yes, I'm still willing to port NodeOS to runtime.js and I needs processes some way or another :-P

@iefserge
Copy link
Member

Fair enough, also it makes sense for WASM, which needs multithreading. Definitely doable but requires some refactoring and code cleanup. I agree, cooperative multitasking is a good start.

As WASM becomes more and more important, I'm thinking we can rename this project to runtime.wasm and start building multi-language cloud platform :)

@piranna
Copy link

piranna commented Jun 23, 2017

also it makes sense for WASM, which needs multithreading

Not yet, but it's planned...

As WASM becomes more and more important, I'm thinking we can rename this project to runtime.wasm and start building multi-language cloud platform :)

Please no, runtime.js is cool as a name :-P A wasm-based and optimized OS similar to Inferno with dis-assembler would be cool thouhgt, but I think it would be a different project... There are already wasm VM written in C, so they could be used as basis, or also integrate llvm to compile to hardware in real time, hum...

@iefserge
Copy link
Member

iefserge commented Jun 23, 2017

Well, V8 is wasm VM too, the engine is the same :)
JS should still be supported of course, just thinking it shouldn't be locked to js-only. I have a couple ideas for the new language that will target WASM and be low level enough for high performance code (very early stages).

@austinfrey
Copy link
Contributor Author

austinfrey commented Jun 23, 2017

the web assembly piece is really interesting, it provides a library OS that is language agnostic, maybe one of the few unikernels that can claim that!

@iefserge are you talking about implementing your own language? and just for my own clarification, is it easier to all provide threading for wasm vs js, or is the complexity the same?

edit: i should add that my need for Web Workers is not super important at the moment, but it would be a great "nice to have" down the road.

@piranna
Copy link

piranna commented Jun 23, 2017

If we change the name, it should be [something].js, of course :-)

@iefserge
Copy link
Member

@aafrey yeah, though language has nothing to do with runtime itself, it would just compile to wasm and run alongside with js. Just playing with things, not sure if I'll get to the point of working language :)
I think complexity is pretty much the same for js and wasm, they run in the same context, just different language/bytecode format for v8. Both are likely to call the same API.

@piranna I like that name, but .wasm will be taken more seriously haha. Language agnostic and web-compatible (to some extent) system can solve this large problem a lot of unikernels have, where you're required to use specific language to do anything with it.

@austinfrey
Copy link
Contributor Author

@iefserge you mentioned in some older issues (#75) that background tasks can be run effectively, provided they are non blocking. What would a good implementation look like for running background tasks on a single thread? can there be true background tasks in a single threaded environment or just asynchronous tasks ?

@facekapow
Copy link
Contributor

facekapow commented Jun 23, 2017

I just want to say that having WASM integration will definitely help runtime.js (mainly because pointers). Maybe we could take something like TurboScript that has a similar syntax to JavaScript and add some runtime.js integration (direct memory access and custom malloc, free, etc.).

@austinfrey
Copy link
Contributor Author

@facekapow
Having WASM integration will definitely help runtime.js (mainly because pointers).
does this mean no copying between WASM and JS, just passing pointers to the same object?

@piranna
Copy link

piranna commented Jun 23, 2017 via email

@RossComputerGuy
Copy link

I've recently been reading about wasm because I don't fully understant what it is. Is wasm just low-level assembly access through JavaScript?

@facekapow
Copy link
Contributor

facekapow commented Jun 23, 2017

@aafrey Well no, I just mean that it could help with managing pointers, since in an operating system you have to deal with memory directly. The fact that JavaScript doesn't have pointers makes this much more difficult.

Btw, that was just a clarification of what I was saying; to answer your question: yeah, I'm pretty sure you can both copy values and pass pointers between WASM and JS.

@RossComputerGuy
Copy link

I found out how to use a custom memory heap in Runtime.js using the __SYSCALL functions

@RossComputerGuy
Copy link

That can help with specific things.

@austinfrey
Copy link
Contributor Author

@piranna that's what I was thinking, so thanks for confirming.
@facekapow thanks for the info
@SpaceboyRoss01 can you explain that further?

@RossComputerGuy
Copy link

I found how to use a custom memory manager in Runtime.js by reading and writing at a specific section of memory to allocate and free memory for specific tasks, like multitasking.

@facekapow
Copy link
Contributor

@SpaceboyRoss01 WASM is cross platform assembly via a JavaScript engine. It has no native system access, it was created to achieve near native speeds for web code. But, since there is a way to interface with WASM through JavaScript, we can provide access to runtime's native functions.

@RossComputerGuy
Copy link

@facekapow That is what I was asking about what WASM is.

@piranna
Copy link

piranna commented Jun 23, 2017

I've recently been reading about wasm because I don't fully understant what it is. Is wasm just low-level assembly access through JavaScript?

It's a bytecode specification, similar to the one of the Java VM or Python. In fact, I have been using its opcodes to define the instructions of a CPU of my own :-)

@aafrey Well no, I just mean that it could help with managing pointers, since in an operating system you have to deal with memory directly. The fact that JavaScript doesn't have pointers makes this much more difficult.

Pointers are not needed to manage memory data, you can use structs and MemoryViews. In fact, that's the way you do it with wasm, and it's pretty easy and secure since you work with arrays instead of pointers operations and you can access only the memory area of the MemoryView.

@facekapow
Copy link
Contributor

@piranna Well yes, in plain JavaScript you can use Uint arrays with ArrayBuffers for specific memory areas backing them to deal with memory. But what I mean is, languages like C and C++, Rust, TurboScript, and others that compile to WebAssembly and use pointers can help make memory easier to work with.

@piranna
Copy link

piranna commented Jun 23, 2017 via email

@austinfrey
Copy link
Contributor Author

after doing some additional reading, it looks like threads are coming to wasm eventually (WebAssembly/design#1073)

would there be any benefit in beginning that process now with runtime? I know a number of libraries handle web workers with a polyfill when they are not supported by a browser. Maybe we could do something similar. build out a worker class, runtime.worker, that would use lightweight threads (setTimeout) initially, but once threads are a reality we have something that already works. so we build the polyfill first lol

@facekapow
Copy link
Contributor

facekapow commented Jun 23, 2017

@piranha Yeah, I just found that out for myself reading through some source code for TurboScript. Damn. It made my hopes of writing device drivers (e.g. USB host controller support) much harder (I know runtime is a server OS, but you can't blame me for wanting to have traditionally native OS things written in web technologies 😁).

@piranna
Copy link

piranna commented Jun 23, 2017 via email

@facekapow
Copy link
Contributor

The problem is that EHCI does everything in memory, no ports. So the convenience of being able to overlay a struct onto an area of memory simplifies it so much. Oh well.

@austinfrey
Copy link
Contributor Author

@facekapow how is wasm enabled? in the runtimejs: {} property in package.json?

@piranna
Copy link

piranna commented Jun 24, 2017 via email

@facekapow
Copy link
Contributor

@aafrey No, in runtime.json put:

{
  "v8": {
    "flags": "--expose_wasm"
  }
}

@piranna Yeah, well I've been having some trouble with code not executing if I try to modify memory via a Uint8Array. Don't worry about it, it's my dumb hopes, I'll fix it somehow.

@piranna
Copy link

piranna commented Jun 24, 2017 via email

@austinfrey
Copy link
Contributor Author

closing since conversation died off. feel free to re-open if this comes up again :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants