Simulating context switches in JavaScript?

I worked on implementing a rather complex system in JavaScript that should mimic, among other things, multi-threaded processes. In a real multithreaded process (such as a kernel thread), you can switch between threads through context switching. This works because you can save the current program process counter and register a temporary structure, restore the program counter and registers for some other process, and then resume the one you stopped in the previous process.

I am wondering if something like this is possible in JavaScript. Currently, I do not know how to do this, and therefore we are developing a system using joint multitasking. In particular, any “function” that I want to run in a multi-threaded simulator is split into an array of functions. To execute a “function”, I iterate over an array of functions, performing each one in order, while maintaining a “program counter” from which the next function should be executed. This allows me to simulate a context switch by calling one of the functions in the array, waiting for the function to return, and then switching to another array of functions that need to be executed.

My current approach works, but it's hard to write code on this system. Each function should indicate specifically when it can be interrupted, and since the functions in the array are all separate, the logic for transferring data between different parts of the function is complex. I was hoping to get something closer to proactive multitasking working instead.

My question is: Is it possible to run an arbitrary JavaScript function so that it can be paused and resumed by an external source?

+7
source share
2 answers

Check StratifiedJS out

+4
source

First of all, it's important to note that JavaScript is completely single-threaded. Multithreading emulation is not really the way to go. You are much better off relying on eventloops.

As mentioned, web workers can be used, but there is no match between browsers for them, so I will ignore web workers. Also, you cannot manipulate the DOM with web workers.

I would say node.js to reason about why event loops are a good alternative to multithreading. I think it pretty well touches on why this is a good alternative in this video .

Therefore, instead of having an array of functions and iterating over them, you can instead create an event and associate a set of functions with it and trigger the specified event. A very easy implementation of events can be found in backbone.js .

You cannot just pause a stream in JavaScript because there is only one. There is no way to pause or resume a function without a function in which there are dots.

There is only one way to imitate this. Write a JavaScript parser that will break your beautiful JavaScript construct and create a system that allows you to pause and resume JavaScript.

Take for example this function

function(i) { j = i + 1; console.log(j); return foo(j); } 

and converts it to this

 var bar = function(i) { var r = {}; var j = i + 1; var f = function() { console.log(j); var g = function() { return foo(j); }; onNext(g, arguments.callee, this, r); }; onNext(f, arguments.callee, this); return r; } 

you will need to extend the function with .suspend and .resume

 Function.prototype.suspend = function() { this.__suspended = true; } Function.prototype.resume = function() { this.__suspended = false; } function onNext(callback, function, context, returnObj) { if (!function.__suspended) { var cb = function() { Backbone.Events.unbind("run", cb); returnObj.r = callback.call(this); } Backbone.Events.bind("run", cb); } } setInterval(function() { Backbone.Events.trigger("run"); }, 5); 

You will also have to replace all references to var a = b() with

 callFunctionAsync(b, context, args, function(return) { var a = return; ... }); 

I will leave the implantation up to you. At the moment, all functions return an object r and only when rr set to a value does it "return". So just check around the event loop to see if it has been “returned”, checking if rr installed, and if it calls the async callback function.

Hey and see what we have. Emulated threads by running them around an event loop. It is much better to use the event loop initially in your code, and then simulate the threads through it.

Basically, your function runs the next line of its code when you go around the event loop again. And check if any “function” is paused or resumed when you go around the eventloop.

I did not implement the return of functions back to "functions" for short. It should not be too difficult to imitate.

Either use the event loop directly, or use fake threading methods and get a compiler to compile your code so that it doesn't look disgusting when coding it.

If you cause punches with dead locks.

+3
source

All Articles