Skip to content

Commit e857fc8

Browse files
authored
Merge pull request #28 from TuringLang/copy-task
Enable stack copying for Julia v1.1 and later
2 parents 2722439 + a6eea95 commit e857fc8

File tree

10 files changed

+39
-14
lines changed

10 files changed

+39
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ jobs:
5454
script:
5555
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
5656
- julia -e 'using Pkg; Pkg.clone(pwd(), "Libtask"); Pkg.build("Libtask"); Pkg.test("Libtask"; coverage=true)'
57-
- julia -e 'using Pkg; cd(Pkg.dir("Libtask")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())'
57+
# - julia -e 'using Pkg; cd(Pkg.dir("Libtask")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())'

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function f_ct()
2020
end
2121
end
2222

23-
t = Task(f_ct)
23+
t = CTask(f_ct)
24+
# or t = Task(f_ct) |> enable_stack_copying
2425

2526
consume(t) == 0
2627
consume(t) == 1
@@ -40,7 +41,9 @@ function f_ct2()
4041
end
4142
end
4243

43-
t = Task(f_ct2)
44+
t = CTask(f_ct2)
45+
# or t = Task(f_ct2) |> enable_stack_copying
46+
4447

4548
consume(t) == 0
4649
consume(t) == 1
@@ -63,7 +66,7 @@ function f_cta()
6366
end
6467
end
6568

66-
t = Task(f_cta)
69+
t = Task(f_cta) |> enable_stack_copying
6770

6871
consume(t) == 0
6972
consume(t) == 1

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.3
1+
0.2.4

deps/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
JULIA_HOME ?= $(PWD)
22
target ?= x86_64-linux-gnu
33
prefix ?= $(PWD)
4-
JULIA_VERSION ?= v1_0
4+
JULIA_VERSION ?= v1_1
55

66
INCLUDES = -I$(JULIA_HOME)/include/julia
77

deps/task.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
#include "julia.h"
77

8+
jl_task_t *jl_enable_stack_copying(jl_task_t *t)
9+
{
10+
#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 1
11+
if (!t->copy_stack) {
12+
jl_ptls_t ptls = jl_get_ptls_states();
13+
t->copy_stack = 1;
14+
t->bufsz = 0;
15+
memcpy(&t->ctx, &ptls->base_ctx, sizeof(t->ctx));
16+
}
17+
#endif
18+
return t;
19+
}
20+
821
jl_task_t *jl_clone_task(jl_task_t *t)
922
{
1023
jl_ptls_t ptls = jl_get_ptls_states();
@@ -40,7 +53,7 @@ jl_task_t *jl_clone_task(jl_task_t *t)
4053
memcpy((void*)newt->ctx, (void*)t->ctx, sizeof(jl_jmp_buf));
4154
#endif
4255

43-
if (t->stkbuf){
56+
if (t->stkbuf) {
4457
#if !(JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 1)
4558
newt->ssize = t->ssize; // size of saved piece
4659
#endif
@@ -51,7 +64,7 @@ jl_task_t *jl_clone_task(jl_task_t *t)
5164
t->bufsz = 0;
5265
newt->bufsz = 0;
5366
newt->stkbuf = t->stkbuf;
54-
}else{
67+
} else {
5568
#if !(JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 1)
5669
newt->ssize = 0;
5770
#endif

src/Libtask.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Libtask
22

3-
export consume, produce, TArray, get, tzeros, tfill, TRef
3+
export enable_stack_copying, CTask, consume, produce, TArray, get, tzeros, tfill, TRef
44

55
include("../deps/deps.jl"); check_deps();
66
include("taskcopy.jl")

src/taskcopy.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ n_copies(t::Task) = begin
1616
end
1717
end
1818

19+
function enable_stack_copying(t::Task)
20+
t.state != :runnable && t.state != :done &&
21+
error("Only runnable or finished tasks' stack can be copied.")
22+
return ccall((:jl_enable_stack_copying, libtask), Any, (Any,), t)::Task
23+
end
24+
25+
CTask(func) = Task(func) |> enable_stack_copying
1926

2027
function Base.copy(t::Task)
2128
t.state != :runnable && t.state != :done &&

test/clonetask.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ function f_ct()
1212
end
1313
end
1414

15-
t = Task(f_ct)
15+
t = Task(f_ct) |> enable_stack_copying
1616

1717
@test consume(t) == 0
1818
@test consume(t) == 1
1919
a = copy(t);
2020
@test consume(a) == 2
2121
@test consume(a) == 3
22+
@test consume(t) == 2
23+
@test consume(t) == 3
2224

2325
# Test case 2: heap allocated objects are shallowly copied.
2426

@@ -30,7 +32,7 @@ function f_ct2()
3032
end
3133
end
3234

33-
t = Task(f_ct2)
35+
t = CTask(f_ct2)
3436

3537
@test consume(t) == 0
3638
@test consume(t) == 1

test/tarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function f_cta()
1313
end
1414
end
1515

16-
t = Task(f_cta)
16+
t = Task(f_cta) |> enable_stack_copying
1717

1818
consume(t); consume(t)
1919
a = copy(t);

test/tref.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function f_cta()
1212
end
1313
end
1414

15-
t = Task(f_cta)
15+
t = Task(f_cta) |> enable_stack_copying
1616

1717
consume(t); consume(t)
1818
a = copy(t);
@@ -32,7 +32,7 @@ function dict_test()
3232
end
3333
end
3434

35-
t = Task(dict_test)
35+
t = Task(dict_test) |> enable_stack_copying
3636

3737
consume(t); consume(t)
3838
a = copy(t);

0 commit comments

Comments
 (0)