Skip to content

Commit

Permalink
Forbid use of static threads
Browse files Browse the repository at this point in the history
  • Loading branch information
bergel committed Aug 14, 2024
1 parent 3ecb08f commit 94e6fb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}(
Expand Down Expand Up @@ -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
20 changes: 20 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 94e6fb2

Please sign in to comment.