Skip to content

Conversation

@pchintalapudi
Copy link
Member

Threads that wait for the codegen lock should really sleep when it isn't available, since codegen often takes a nontrivial amount of time to complete. Therefore we introduce a sleeping mutex that cooperates with the GC to replace the spin mutex.

Sleeping lock implementation mostly stolen from here: https://www.realworldtech.com/forum/?threadid=189711&curpostid=189755

If we run on an example that forces 5 threads to attempt to acquire the codegen lock at the same time:

Before:

image

After:

image

@vchuravy
Copy link
Member

So one question I have is, does it make sense to use a Julia scheduler integrated lock? These threads could do useful work, instead of being blocked on codegen.

@pchintalapudi
Copy link
Member Author

A scheduler integrated lock would be nice, but I think this sleeping lock is at least a net positive in all aspects compared to the spin lock. Depending on how much of the scheduler is implemented in Julia vs C, it may even be illegal to yield out of codegen to another task (if the scheduler itself needs to take any lock besides the codegen lock, we could end up in a deadlock).

@gbaraldi
Copy link
Member

While talking with @vtjnash he mentioned to me that maybe what we should do here is something like webkits WTF::Lock or rusts https://docs.rs/parking_lot_core/latest/parking_lot_core/ https://webkit.org/blog/6161/locking-in-webkit/.

@pchintalapudi
Copy link
Member Author

Yeah I implemented something similar to WTF::Lock here, except instead of a full-blown hash table I just use an array with hardcoded length, and accept a few extra spurious wakeups and collisions over the same cond/mutex. If we decide that perhaps the codegen lock is really the only kind of lock that should be sleeping, then we can just make jl_sleep_mutex_t contain a uv_mutex_t and not bother with the array either.

@vchuravy vchuravy changed the base branch from master to vc-gb/pc/sleep August 10, 2023 22:09
@adienes adienes added atomics feature Indicates new feature / enhancement requests labels Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

atomics feature Indicates new feature / enhancement requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants