Skip to content

Commit

Permalink
Merge pull request #16 from RelationalAI/adding-unlock
Browse files Browse the repository at this point in the history
Adding unlock
  • Loading branch information
bergel authored Feb 27, 2024
2 parents 7b19f31 + dcd1cfd commit 24739b2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct Semaphore_Extension <: ExtendedRule end
struct Destructor_Extension <: ExtendedRule end
struct ReentrantLock_Extension <: ExtendedRule end
struct SpinLock_Extension <: ExtendedRule end
struct Lock_Extension <: ExtendedRule end
struct Unlock_Extension <: ExtendedRule end

const all_extended_rule_types = InteractiveUtils.subtypes(ExtendedRule)

Expand Down Expand Up @@ -96,10 +98,9 @@ function check(::NThreads_Extention, x::EXPR, markers::Dict{Symbol,Symbol})
generic_check(x, "Threads.nthreads()", "Threads.nthreads() should not be used in a constant variable.")
end

check(::CFunction_Extension, x::EXPR) = generic_check(x, "@cfunction(hole_variable, hole_variable_star)", "Macro @cfunction should not be used.")

check(::Semaphore_Extension, x::EXPR) = generic_check(x, "Semaphore(hole_variable)", "Semaphore should be used with extreme caution.")
check(::ReentrantLock_Extension, x::EXPR) = generic_check(x, "ReentrantLock()", "ReentrantLock should be used with extreme caution.")
check(::CFunction_Extension, x::EXPR) = generic_check(x, "@cfunction(hole_variable, hole_variable_star)", "Macro `@cfunction` should not be used.")
check(::Semaphore_Extension, x::EXPR) = generic_check(x, "Semaphore(hole_variable)", "`Semaphore` should be used with extreme caution.")
check(::ReentrantLock_Extension, x::EXPR) = generic_check(x, "ReentrantLock()", "`ReentrantLock` should be used with extreme caution.")

function check(::Destructor_Extension, x::EXPR)
error_msg = "Destructors should be used with extreme caution."
Expand All @@ -112,4 +113,13 @@ function check(::SpinLock_Extension, x::EXPR)
generic_check(x, "SpinLock()", msg)
generic_check(x, "Threads.SpinLock()", msg)
generic_check(x, "Base.Threads.SpinLock()", msg)
end
end

function check(::Lock_Extension, x::EXPR)
msg = "`@lock` should be used with extreme caution."
generic_check(x, "@lock hole_variable hole_variable", msg)
generic_check(x, "Base.@lock hole_variable hole_variable", msg)
end

check(::Unlock_Extension, x::EXPR) = generic_check(x, "unlock(hole_variable)", "`unlock` should be used with extreme caution.")

28 changes: 28 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ end
@test lint_test(source,
"Line 6, column 19: SpinLock should be used with extreme caution.")
end

@testset "unlock" begin
source = """
function clear(fs::SimulatedFs)
if fs.noop_mode
return nothing
end
for partition in fs.partitions
lock = trylock(partition.lock)
lock || error("SimFs partition locked on clear")
for (k, entry) in partition.entries
lock = trylock(entry.lock)
lock || error("SimFs entry locked on clear")
Blobs.free(entry.buf.data)
unlock(entry.lock)
end
empty!(partition.entries)
unlock(partition.lock)
end
@atomic fs.used_bytes = 0
end
"""
@test lint_has_error_test(source)
@test lint_test(source,
"Line 15, column 9: `unlock` should be used with extreme caution.")
end
end

@testset "Comparison" begin
Expand Down Expand Up @@ -372,6 +398,8 @@ end
end
"""
@test t(source, "finalizer(hole_variable) do hole_variable hole_variable_star end")

@test t("unlock(hole_variable)", "unlock(12)")
end

@testset "offset to line" begin
Expand Down

0 comments on commit 24739b2

Please sign in to comment.