From e33bd8799094e989ba16fdba5b528d9595eab8a8 Mon Sep 17 00:00:00 2001 From: Iulia Dumitru Date: Thu, 6 Jul 2023 17:41:21 +0300 Subject: [PATCH] Add rule for indexing by `threadid()` --- threads/index_by_threadid.jl | 28 ++++++++++++++++++++++++++++ threads/index_by_threadid.yml | 21 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 threads/index_by_threadid.jl create mode 100644 threads/index_by_threadid.yml diff --git a/threads/index_by_threadid.jl b/threads/index_by_threadid.jl new file mode 100644 index 0000000..8105054 --- /dev/null +++ b/threads/index_by_threadid.jl @@ -0,0 +1,28 @@ +using Base.Threads + +states = zeros(10) +Threads.@spawn 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 + +@spawn begin + # ruleid: index-by-threadid + tid = threadid() + val = states[tid] + states[tid] = val + 1 +end + +Threads.@spawn for i = 1:10 + # ruleid: index-by-threadid + val = states[threadid()] +end + + +Threads.@spawn for i = 1:10 + # ruleid: index-by-threadid + val = states[Threads.threadid()] +end diff --git a/threads/index_by_threadid.yml b/threads/index_by_threadid.yml new file mode 100644 index 0000000..4210ca1 --- /dev/null +++ b/threads/index_by_threadid.yml @@ -0,0 +1,21 @@ +rules: + - id: index-by-threadid + pattern-either: + - pattern: | + $TID = Threads.threadid() + ... + $X = $S[$TID] + - pattern: | + $TID = threadid() + ... + $X = $S[$TID] + - pattern: $S[threadid()] + - pattern: $S[Threads.threadid()] + message: Indexing by `threadid()` may cause race conditions and should be avoided. + metadata: + references: + - https://julialang.netlify.app/previews/pr1914/blog/2023/06/psa-dont-use-threadid/ + license: LGPL + languages: + - julia + severity: WARNING