From 94e6fb25c6012c15515c57a719f755721dc42284 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Wed, 14 Aug 2024 11:59:16 +0200 Subject: [PATCH] Forbid use of static threads --- src/linting/extended_checks.jl | 7 +++++++ test/rai_rules_tests.jl | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index 78af44a..1770d36 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -211,6 +211,7 @@ struct StringInterpolation_Extension <: ViolationExtendedRule end struct RelPathAPIUsage_Extension <: ViolationExtendedRule end struct NonFrontShapeAPIUsage_Extension <: ViolationExtendedRule end struct InterpolationInSafeLog_Extension <: RecommendationExtendedRule end +struct UseOfStaticThreads <: ViolationExtendedRule end const all_extended_rule_types = Ref{Any}( @@ -568,3 +569,9 @@ end function check(t::InterpolationInSafeLog_Extension, x::EXPR) generic_check(t, x, "@warnv_safe_to_log hole_variable \"LINT_STRING_WITH_INTERPOLATION\"", "Safe warning log has interpolation.") end + +function check(t::UseOfStaticThreads, x::EXPR) + msg = "Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`. Static threads must not be used as generated tasks will not be able to migrate across threads." + generic_check(t, x, "@threads :static hole_variable_star", msg) + generic_check(t, x, "Threads.@threads :static hole_variable_star", msg) +end \ No newline at end of file diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index 7eb2202..b3735ed 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -1937,3 +1937,23 @@ end @test lint_test(source, "Line 7, column 9: Safe warning log has interpolation.") @test lint_test(source, "Line 10, column 17: Safe warning log has interpolation.") end + +@testset "Use of static threads" begin + source = raw""" + function f() + Threads.@threads :static for _ in 1:10 + println("foo") + end + + @threads :static for _ in 1:10 + println("foo") + end + + Threads.@threads :dynamic for _ in 1:10 + println("foo") + end + end + """ + @test lint_test(source, "Line 2, column 5: Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`.") + @test lint_test(source, "Line 6, column 5: Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`.") +end