Coder Social home page Coder Social logo

schedlua's Introduction

schedlua is a set of routines that make it relatively easy to create cooperative multi-tasking applications in LuaJIT.

Design criteria

  • Simplicity through minimal coding
  • Complexity by composition rather than complex structures
  • Compactness through reuse
  • minimal number of concepts
  • implied operations where possible

The lua programming language is great for multi-tasking because it already contains the notion of co-routines. This mechanism provides the means to readily compartmentalize operations into easily coded sections. The challenge of this method of multi-tasking is that it is cooperative, and you end up with a management challenge as the number of independently operating tasks increases.

schedlua provides a couple of key features which make multi-task programming with lua a lot more manageable. The first is easy scheduling of tasks. In a cooperative multi-tasking environment, scheduling is nothing more than deciding which task should run next after the currently running task decides to yield. schedlua provides a relatively simple scheduling mechanism which deals with this selection process.

The second feature schedlua provides is a series of well named functions which further enable typical multi-tasking tasks. At the core there are routines that handle signaling (events). A task can emit events, as well as wait on events. Built atop the signaling is a relatively new but useful paradign named predicate flow control. This is basically the async equivalent of if/then blocks. There are alarms, which provide mechanism for sleeping and delaying executing, and last, there is async io operations, particularly as they relate to network programming.

Here is a typical application:

local Kernel = require("schedlua.kernel")


local function task1()
	print("first task, first line")
	yield();
	print("first task, second line")
	halt();
end

local function task2()
	print("second task, only line")
end

local function main()
	local t1 = spawn(task1)
	local t2 = spawn(task2)
end

run(main)

All applications must begin by including the schedlua.kernel module. All applications begin execution by explicitly calling the 'run()' function. The run() function takes an optional function to be executed, so it is convenient the create a single function, which in turn has all the code that you want to execute.

Within this example, there are two calls to the spawn() function. Each call to this function will create a separate cooperative task, which will in turn be added to the scheduler for execution. A call to 'spawn' does not cause the task to execute immediately, but merely to be scheduled for execution.

The runtime will constantly step through the list of tasks ready to be run, executing them in turn until each of them reaches a point where they will yield, and allow for another task to run.

Example Using Time and Predicates

--[[
	An example of how to use time, predicates
	and multiple tasks.
--]]

local Kernel = require("schedlua.kernel")
local StopWatch = require("schedlua.stopwatch")

local sw = StopWatch();


-- A simple conditional which will return true
-- once we pass 12 seconds according to the clock
local function timeExpires()
	return sw:seconds() > 12
end

-- The response to be executed once we reach
-- a time of 12 seconds
local function revertToForm()
	print("Time: ", sw:seconds())
	print("The carriage has reverted to a pumpkin")
	halt();
end


-- The response which will be executed whenever
-- we pass another second
local function printTime()
	print("Time: ", sw:seconds())
end


-- Stitching it all together
local function main()
	periodic(1000, printTime)
	when(timeExpires, revertToForm)
end

run(main)

In this example, the timer function 'periodic' is being used, as well as the predicate function 'when'. The periodic function is simply executing the 'printTime' function once every second (1000 milliseconds). By calling 'periodic', a cooperative task is scheduled, and the program continues on to the next statement, which is the 'when' call. The 'when()' function takes two parameters. The first is a function which always returns a non-false value, or false. When the function returns a non-false value, it will then execute the second parameter, which should be a function. In this case the 'revertToForm()' function.

So, overall, the example will print the current running time, once a second, and after 12 seconds, it will print a message, and halt() the program.

References

schedlua's People

Contributors

wiladams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.