aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/lane/tasking_basic.lua
blob: 7a2230784f8cea9d5fd4f88734d127599e7f310a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure()
print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
local lanes = require_lanes_result_1

local require_assert_result_1, require_assert_result_2 = require "_assert"
print("require_assert_result:", require_assert_result_1, require_assert_result_2)

local utils = lanes.require "_utils"
local PRINT = utils.MAKE_PRINT()

local lanes_gen = assert(lanes.gen)
local lanes_linda = assert(lanes.linda)

-- ##################################################################################################
-- ##################################################################################################
-- ##################################################################################################

-- we can create a generator where the lane body is a C function
do
    local b, g = pcall(lanes.gen, "*", print)
    assert(b == true and type(g) == "function")
    -- we can start the lane
    local b, h = pcall(g, "hello")
    -- the lane runs normally
    h:join()
    assert(h.status == "done")
end

-- we can create a generator where the lane body is a C function that raises an error
do
    local b, g = pcall(lanes.gen, "*", error)
    assert(b == true and type(g) == "function")
    -- we can start the lane
    local b, h = pcall(g, "this is an error")
    -- this provides the error that occurred in the lane
    local s, e, t = h:join()
    assert(h.status == "error")
    assert(s == nil and e == "this is an error" and t == nil)
end

-- ##################################################################################################
-- ##################################################################################################
-- ##################################################################################################

local function task(a, b, c)
    local new_name = "task("..a..","..b..","..c..")"
    -- test lane naming change
    lane_threadname(new_name)
    assert(lane_threadname() == new_name)
    --error "111"     -- testing error messages
    assert(hey)
    local v=0
    for i=a,b,c do
        v= v+i
    end
    return v, hey
end

local gc_cb = function(name_, status_)
    PRINT("				---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'")
end

-- ##################################################################################################
-- ##################################################################################################
-- ##################################################################################################

PRINT("\n\n", "---=== Tasking (basic) ===---", "\n\n")

local task_launch = lanes_gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task)
    -- base stdlibs, normal priority

-- 'task_launch' is a factory of multithreaded tasks, we can launch several:

local lane1 = task_launch(100,200,3)
assert.fails(function() print(lane1[lane1]) end) -- indexing the lane with anything other than a string or a number should fail
lanes.sleep(0.1) -- give some time so that the lane can set its name
assert(lane1:get_threadname() == "task(100,200,3)", "Lane name is " .. lane1:get_threadname())

local lane2= task_launch(200,300,4)

-- At this stage, states may be "pending", "running" or "done"

local st1,st2= lane1.status, lane2.status
PRINT(st1,st2)
assert(st1=="pending" or st1=="running" or st1=="done")
assert(st2=="pending" or st2=="running" or st2=="done")

-- Accessing results ([1..N]) pends until they are available
--
PRINT("waiting...")
local v1, v1_hey= lane1[1], lane1[2]
local v2, v2_hey= lane2[1], lane2[2]

PRINT(v1, v1_hey)
assert(v1_hey == true)

PRINT(v2, v2_hey)
assert(v2_hey == true)

assert(lane1.status == "done")
assert(lane1.status == "done")