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

Support ThreadLocal variables #571

Open
bbrockbernd opened this issue Mar 13, 2025 · 0 comments
Open

Support ThreadLocal variables #571

bbrockbernd opened this issue Mar 13, 2025 · 0 comments

Comments

@bbrockbernd
Copy link
Collaborator

The following test succeeds if we get the "threadLocal" from the concurrenthashmap (keyed by thread id), but fails if we get it from actually thread local.

class LincheckTest {
    val threadLocalCounters = ConcurrentHashMap<Long, AtomicInteger>()
    val counter = AtomicInteger(0)

    @Test
    fun modelCheckingTest(): Unit = ModelCheckingOptions()
        .iterations(0) // Change to 300 for exhaustive testing
        .addCustomScenario { 
            parallel { 
                thread { 
                    actor(::iinc) 
                    actor(::iinc)
                } 
                
                thread { 
                    actor(::gget)
                    actor(::gget)
                }
                
                thread {
                    actor(::gget)
                    actor(::gget)
                }
            }
        }
        .invocationsPerIteration(5_000)
        .hangingDetectionThreshold(100)
        .logLevel(LoggingLevel.INFO)
        .check(this::class.java)

    @Operation
    fun iinc(): Int {
        val add = getLocalCounter().getAndIncrement()
        return counter.addAndGet(add)
    }

    @Operation
    fun gget() = counter.get()
    
    // This one works
//    private fun getLocalCounter() = threadLocalCounters.computeIfAbsent(Thread.currentThread().id) { AtomicInteger(1) }
    
    // This one doesnt
    private fun getLocalCounter() = localCounter.get()
    
}

private val localCounter: ThreadLocal<AtomicInteger> = ThreadLocal.withInitial { AtomicInteger(1) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant