|
1 |
| -# This file is a part of Julia. License is MIT: https://julialang.org/license |
2 |
| - |
3 |
| -let cmd = `$(Base.julia_cmd()) --depwarn=error --startup-file=no threads_exec.jl` |
4 |
| - for test_nthreads in (1, 2, 4, 4) # run once to try single-threaded mode, then try a couple times to trigger bad races |
5 |
| - run(pipeline(setenv(cmd, "JULIA_NUM_THREADS" => test_nthreads), stdout = stdout, stderr = stderr)) |
6 |
| - end |
7 |
| -end |
8 |
| - |
9 |
| -# issue #34415 - make sure external affinity settings work |
10 |
| -if Sys.islinux() && Sys.CPU_THREADS > 1 && Sys.which("taskset") !== nothing |
11 |
| - run_with_affinity(spec) = readchomp(`taskset -c $spec $(Base.julia_cmd()) -e "run(\`taskset -p \$(getpid())\`)"`) |
12 |
| - @test endswith(run_with_affinity("1"), "2") |
13 |
| - @test endswith(run_with_affinity("0,1"), "3") |
14 |
| -end |
15 |
| - |
16 |
| -# issue #34769 |
17 |
| -function idle_callback(handle) |
18 |
| - idle = @Base.handle_as handle UvTestIdle |
19 |
| - if idle.active |
20 |
| - idle.count += 1 |
21 |
| - if idle.count == 1 |
22 |
| - # We want to hit the case where we're allowing |
23 |
| - # the thread to go to sleep, which only happens |
24 |
| - # after some default amount of time (DEFAULT_THREAD_SLEEP_THRESHOLD) |
25 |
| - # so spend that amount of time here. |
26 |
| - Libc.systemsleep(0.004) |
27 |
| - elseif idle.count >= 10 |
28 |
| - lock(idle.cond) |
29 |
| - try |
30 |
| - notify(idle.cond, true) |
31 |
| - finally |
32 |
| - unlock(idle.cond) |
33 |
| - end |
34 |
| - idle.active = false |
35 |
| - end |
36 |
| - end |
37 |
| - nothing |
38 |
| -end |
39 |
| - |
40 |
| -mutable struct UvTestIdle |
41 |
| - handle::Ptr{Cvoid} |
42 |
| - cond::Base.ThreadSynchronizer |
43 |
| - isopen::Bool |
44 |
| - active::Bool |
45 |
| - count::Int |
46 |
| - |
47 |
| - function UvTestIdle() |
48 |
| - this = new(Libc.malloc(Base._sizeof_uv_idle), Base.ThreadSynchronizer(), true, false, 0) |
49 |
| - Base.iolock_begin() |
50 |
| - Base.associate_julia_struct(this.handle, this) |
51 |
| - err = ccall(:uv_idle_init, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), |
52 |
| - Base.eventloop(), this.handle) |
53 |
| - if err != 0 |
54 |
| - Libc.free(this.handle) |
55 |
| - this.handle = C_NULL |
56 |
| - throw(_UVError("uv_idle_init", err)) |
57 |
| - end |
58 |
| - err = ccall(:uv_idle_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), |
59 |
| - this.handle, @cfunction(idle_callback, Cvoid, (Ptr{Cvoid},))) |
60 |
| - if err != 0 |
61 |
| - Libc.free(this.handle) |
62 |
| - this.handle = C_NULL |
63 |
| - throw(_UVError("uv_idle_start", err)) |
64 |
| - end |
65 |
| - finalizer(Base.uvfinalize, this) |
66 |
| - Base.iolock_end() |
67 |
| - return this |
68 |
| - end |
69 |
| -end |
70 |
| -Base.unsafe_convert(::Type{Ptr{Cvoid}}, idle::UvTestIdle) = idle.handle |
71 |
| - |
72 |
| -function Base.uvfinalize(t::UvTestIdle) |
73 |
| - Base.iolock_begin() |
74 |
| - Base.lock(t.cond) |
75 |
| - try |
76 |
| - if t.handle != C_NULL |
77 |
| - Base.disassociate_julia_struct(t.handle) # not going to call the usual close hooks |
78 |
| - if t.isopen |
79 |
| - t.isopen = false |
80 |
| - ccall(:jl_close_uv, Cvoid, (Ptr{Cvoid},), t) |
81 |
| - end |
82 |
| - t.handle = C_NULL |
83 |
| - notify(t.cond, false) |
84 |
| - end |
85 |
| - finally |
86 |
| - unlock(t.cond) |
87 |
| - end |
88 |
| - Base.iolock_end() |
89 |
| - nothing |
90 |
| -end |
91 |
| - |
92 |
| -function Base.wait(idle::UvTestIdle) |
93 |
| - Base.iolock_begin() |
94 |
| - Base.preserve_handle(idle) |
95 |
| - Base.lock(idle.cond) |
96 |
| - try |
97 |
| - idle.active = true |
98 |
| - wait(idle.cond) |
99 |
| - finally |
100 |
| - Base.unlock(idle.cond) |
101 |
| - Base.unpreserve_handle(idle) |
102 |
| - Base.iolock_end() |
103 |
| - end |
104 |
| -end |
105 |
| - |
106 |
| -# Spawn another process as a watchdog. If this test fails, it'll unrecoverably |
107 |
| -# hang in the event loop. Another process needs to kill it |
108 |
| -cmd = """ |
109 |
| - @async (Base.wait_readnb(stdin, 1); exit()) |
110 |
| - sleep(100) |
111 |
| - isopen(stdin) || exit() |
112 |
| - println(stderr, "ERROR: Killing threads test due to watchdog expiry") |
113 |
| - ccall(:uv_kill, Cint, (Cint, Cint), $(getpid()), Base.SIGTERM) |
114 |
| -""" |
115 |
| -proc = open(pipeline(`$(Base.julia_cmd()) -e $cmd`; stderr=stderr); write=true) |
116 |
| - |
117 |
| -let idle=UvTestIdle() |
118 |
| - wait(idle) |
119 |
| -end |
120 |
| - |
121 |
| -using Base.Threads |
122 |
| -@threads for i = 1:1 |
123 |
| - let idle=UvTestIdle() |
124 |
| - wait(idle) |
125 |
| - end |
126 |
| -end |
127 |
| - |
128 |
| -@test process_running(proc) |
129 |
| - |
130 |
| -# We don't need the watchdog anymore |
131 |
| -close(proc.in) |
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +let cmd = `$(Base.julia_cmd()) --depwarn=error --startup-file=no threads_exec.jl` |
| 4 | + for test_nthreads in (1, 2, 4, 4) # run once to try single-threaded mode, then try a couple times to trigger bad races |
| 5 | + run(pipeline(setenv(cmd, "JULIA_NUM_THREADS" => test_nthreads), stdout = stdout, stderr = stderr)) |
| 6 | + end |
| 7 | +end |
| 8 | + |
| 9 | +# issue #34415 - make sure external affinity settings work |
| 10 | +if Sys.islinux() && Sys.CPU_THREADS > 1 && Sys.which("taskset") !== nothing |
| 11 | + run_with_affinity(spec) = readchomp(`taskset -c $spec $(Base.julia_cmd()) -e "run(\`taskset -p \$(getpid())\`)"`) |
| 12 | + @test endswith(run_with_affinity("1"), "2") |
| 13 | + @test endswith(run_with_affinity("0,1"), "3") |
| 14 | +end |
| 15 | + |
| 16 | +# issue #34769 |
| 17 | +function idle_callback(handle) |
| 18 | + idle = @Base.handle_as handle UvTestIdle |
| 19 | + if idle.active |
| 20 | + idle.count += 1 |
| 21 | + if idle.count == 1 |
| 22 | + # We want to hit the case where we're allowing |
| 23 | + # the thread to go to sleep, which only happens |
| 24 | + # after some default amount of time (DEFAULT_THREAD_SLEEP_THRESHOLD) |
| 25 | + # so spend that amount of time here. |
| 26 | + Libc.systemsleep(0.004) |
| 27 | + elseif idle.count >= 10 |
| 28 | + lock(idle.cond) |
| 29 | + try |
| 30 | + notify(idle.cond, true) |
| 31 | + finally |
| 32 | + unlock(idle.cond) |
| 33 | + end |
| 34 | + idle.active = false |
| 35 | + end |
| 36 | + end |
| 37 | + nothing |
| 38 | +end |
| 39 | + |
| 40 | +mutable struct UvTestIdle |
| 41 | + handle::Ptr{Cvoid} |
| 42 | + cond::Base.ThreadSynchronizer |
| 43 | + isopen::Bool |
| 44 | + active::Bool |
| 45 | + count::Int |
| 46 | + |
| 47 | + function UvTestIdle() |
| 48 | + this = new(Libc.malloc(Base._sizeof_uv_idle), Base.ThreadSynchronizer(), true, false, 0) |
| 49 | + Base.iolock_begin() |
| 50 | + Base.associate_julia_struct(this.handle, this) |
| 51 | + err = ccall(:uv_idle_init, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), |
| 52 | + Base.eventloop(), this.handle) |
| 53 | + if err != 0 |
| 54 | + Libc.free(this.handle) |
| 55 | + this.handle = C_NULL |
| 56 | + throw(_UVError("uv_idle_init", err)) |
| 57 | + end |
| 58 | + err = ccall(:uv_idle_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), |
| 59 | + this.handle, @cfunction(idle_callback, Cvoid, (Ptr{Cvoid},))) |
| 60 | + if err != 0 |
| 61 | + Libc.free(this.handle) |
| 62 | + this.handle = C_NULL |
| 63 | + throw(_UVError("uv_idle_start", err)) |
| 64 | + end |
| 65 | + finalizer(Base.uvfinalize, this) |
| 66 | + Base.iolock_end() |
| 67 | + return this |
| 68 | + end |
| 69 | +end |
| 70 | +Base.unsafe_convert(::Type{Ptr{Cvoid}}, idle::UvTestIdle) = idle.handle |
| 71 | + |
| 72 | +function Base.uvfinalize(t::UvTestIdle) |
| 73 | + Base.iolock_begin() |
| 74 | + Base.lock(t.cond) |
| 75 | + try |
| 76 | + if t.handle != C_NULL |
| 77 | + Base.disassociate_julia_struct(t.handle) # not going to call the usual close hooks |
| 78 | + if t.isopen |
| 79 | + t.isopen = false |
| 80 | + ccall(:jl_close_uv, Cvoid, (Ptr{Cvoid},), t) |
| 81 | + end |
| 82 | + t.handle = C_NULL |
| 83 | + notify(t.cond, false) |
| 84 | + end |
| 85 | + finally |
| 86 | + unlock(t.cond) |
| 87 | + end |
| 88 | + Base.iolock_end() |
| 89 | + nothing |
| 90 | +end |
| 91 | + |
| 92 | +function Base.wait(idle::UvTestIdle) |
| 93 | + Base.iolock_begin() |
| 94 | + Base.preserve_handle(idle) |
| 95 | + Base.lock(idle.cond) |
| 96 | + try |
| 97 | + idle.active = true |
| 98 | + wait(idle.cond) |
| 99 | + finally |
| 100 | + Base.unlock(idle.cond) |
| 101 | + Base.unpreserve_handle(idle) |
| 102 | + Base.iolock_end() |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +# Spawn another process as a watchdog. If this test fails, it'll unrecoverably |
| 107 | +# hang in the event loop. Another process needs to kill it |
| 108 | +cmd = """ |
| 109 | + @async (Base.wait_readnb(stdin, 1); exit()) |
| 110 | + sleep(100) |
| 111 | + isopen(stdin) || exit() |
| 112 | + println(stderr, "ERROR: Killing threads test due to watchdog expiry") |
| 113 | + ccall(:uv_kill, Cint, (Cint, Cint), $(getpid()), Base.SIGTERM) |
| 114 | +""" |
| 115 | +proc = open(pipeline(`$(Base.julia_cmd()) -e $cmd`; stderr=stderr); write=true) |
| 116 | + |
| 117 | +let idle=UvTestIdle() |
| 118 | + wait(idle) |
| 119 | +end |
| 120 | + |
| 121 | +using Base.Threads |
| 122 | +@threads for i = 1:1 |
| 123 | + let idle=UvTestIdle() |
| 124 | + wait(idle) |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +@test process_running(proc) |
| 129 | + |
| 130 | +# We don't need the watchdog anymore |
| 131 | +close(proc.in) |
0 commit comments