Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing by threadid() should be avoided #2

Merged
merged 5 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lang/correctness/index_by_threadid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Base.Threads: threadid, @threads

states = zeros(10)
Threads.@threads for i = 1:10
# ruleid: index-by-threadid
tid = Threads.threadid()
old_val = states[tid]
new_val = old_val + i
states[tid] = new_val
end

@threads for i = 1:10
# ruleid: index-by-threadid
tid = threadid()
val = states[tid]
states[tid] = val + 1
end

@threads :foo for i = 1:10
# ruleid: index-by-threadid
val = states[Threads.threadid()]
end

Threads.@threads :static for i = 1:10
# ok: index-by-threadid
val = states[Threads.threadid()]
end
31 changes: 31 additions & 0 deletions lang/correctness/index_by_threadid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
rules:
- id: index-by-threadid
patterns:
- pattern-inside: |
@threads ... for $X = ...
...
end
- pattern-not-inside: |
@threads :static for $X = ...
...
end
- pattern-either:
- pattern: $S[Threads.threadid()]
- pattern: $S[threadid()]
- pattern: |
$TID = Threads.threadid()
...
$Y = $S[$TID]
- pattern: |
$TID = threadid()
...
$Y = $S[$TID]
message: Indexing by `threadid()` may cause race conditions and should be avoided.
metadata:
confidence: LOW
references:
- https://www.julialang.org/blog/2023/07/PSA-dont-use-threadid/
license: LGPL
languages:
- julia
severity: WARNING
Loading