Skip to content

Commit d01f8bf

Browse files
committed
Disable multithreading in Windows due instability
1 parent b88e4af commit d01f8bf

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/JNI.jl

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export JNI_VERSION_1_1, JNI_VERSION_1_2, JNI_VERSION_1_4, JNI_VERSION_1_6, JNI_V
1616
export JNI_OK, JNI_ERR, JNI_EDETACHED, JNI_EVERSION, JNI_ENOMEM, JNI_EEXIST, JNI_EINV
1717
#export jnifunc
1818

19+
include("Threads.jl")
1920
include("jnienv.jl")
2021

2122
const jniref = Ref(JNINativeInterface())

src/JavaCall.jl

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ JULIA_COPY_STACKS = false
3333

3434
include("JNI.jl")
3535
using .JNI
36+
import .JNI.Threads
3637
include("jvm.jl")
3738
include("core.jl")
3839
include("convert.jl")

src/Threads.jl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
JavaCall.JNI.Threads is a standin module for Base.Threads
3+
4+
On Windows, it disables multithreading for JavaCall since JavaCall becomes
5+
unstable in Windows.
6+
7+
On other operating systems, it just uses Base.Threads.
8+
"""
9+
module Threads
10+
@static if Sys.iswindows()
11+
threadid() = 1
12+
nthreads() = 1
13+
resize_nthreads!(x) = x
14+
macro threads(expr)
15+
esc(expr)
16+
end
17+
else
18+
import Base.Threads: resize_nthreads!, @threads, threadid, nthreads
19+
end
20+
end

src/jvm.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,16 @@ const ROOT_TASK_ERROR = JavaCallError(
226226
const JULIA_COPY_STACKS_ON_WINDOWS_ERROR = JavaCallError(
227227
"JULIA_COPY_STACKS should not be set on Windows.")
228228

229+
const THREADID_NOT_ONE_WINDOWS_ERROR = JavaCallError(
230+
"JavaCall must be used on Thread 1 only in Windows. Multithreading JavaCall is not supported on Windows."
231+
)
232+
229233
# JavaCall must run on the root Task or JULIA_COPY_STACKS is enabled
230234
isroottask() = Base.roottask === Base.current_task()
231235
@static if Sys.iswindows()
232-
isgoodenv() = ! JULIA_COPY_STACKS
233-
assertroottask_or_goodenv() = isgoodenv() ? true : throw(JULIA_COPY_STACKS_ON_WINDOWS_ERROR)
236+
isgoodenv() = ( ! JULIA_COPY_STACKS ) && Base.Threads.threadid() == 1
237+
assertroottask_or_goodenv() = isgoodenv() ? true : Base.Threads.threadid() == 1 ?
238+
throw(JULIA_COPY_STACKS_ON_WINDOWS_ERROR) : throw(THREADID_NOT_ONE_WINDOWS_ERROR)
234239
else
235240
isgoodenv() = JULIA_COPY_STACKS || isroottask()
236241
assertroottask_or_goodenv() = isgoodenv() ? true : throw(ROOT_TASK_ERROR)

0 commit comments

Comments
 (0)