Threading

All vexiscript threads execute synchronously. This is to say that at any one time only ever at most one vexiscript is running. This is a powerful limitation as it avoids the possibility of concurrent access to javascript objects.

The threading is co-operatively multi-tasked. Long running threads should yield at regular intervals to prevent unresponsiveness. This will either happen automatically when performing a background process (such as an RPC call) where a wait can be expected for a response, or can be done by explicitly calling vexi.thread.yield().

There are two types of thread in vexiscript; the ‘foreground’ thread — which executes layout and event traps — and ‘background’ threads.

The Foreground Thread

The foreground thread is implicit. Code in the foreground thread is typically invoked by layout-related traps or input events.

A limitation of the foreground thread is that no blocking operations may be performed and subsequently calls to vexi.thread.sleep() and vexi.thread.yield() will cause an exception.

The foreground thread is executed inbetween every execution of a background thread, as part of the core layout cycle (known as reflow).

Background Threads

A background thread is an explicitly created thread that may perform blocking operations such as RPC calls, or call vexi.thread.sleep() or vexi.thread.yield() (in constrast with the foreground thread).

To create a background thread simply put a function to the vexi.thread property:

vexi.thread = function() {
    // code here runs in a background thread
}

This places the function in the thread queue, and it will be automatically invoked. To remove a function from the thread queue, complete the function (i.e. return).

Background threads are round-robin scheduled (i.e in a FIFO queue) and when they yield they will start again at the back of the queue.

 


Copyright © 2011 The Vexi Project (vexi.sourceforge.net)