Skip to content

Jvm SegmentPool and coroutines #476

@ThibaultBee

Description

@ThibaultBee

Hi,

I am just raising a question because I got an issue with ktor.

If my understanding is correct about this:

return (Thread.currentThread().id and mask).toInt()

The SegmentPool contains Buckets that are ordered by bucketId.

 private fun bucketId(mask: Long): Int {
        // Get a value in [0..HASH_BUCKET_COUNT_L2) based on the current thread.
        @Suppress("DEPRECATION") // TODO: switch to threadId after JDK19
        return (Thread.currentThread().id and mask).toInt()
}

And when calling take or recycle, the SegmentPool tries to get or to put a Segment to the buckets of id bucketId.

When using a single thread executor, the bucketId is always going to be the same value. It also works with a limited number of thread.
But in the coroutine context, we don't have any info about the thread. The calling thread might be different every time take or recycle is called. We don't have a clue on how coroutine deal with the thread (only in a SingleExecutor dispatcher - but it is not Kotlin multiplatform friendly).

How the SegmentPool could be sure to reuse a Segment in a coroutine context?
Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions