I'm hitting a roadblock with Tokio's RwLock
due to how it sets the semaphore permit to 32 by default.
Here is an example. Rust Playground
use std::sync::Arc;
use tokio::spawn;
use tokio::sync::RwLock;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let lock = Arc::new(RwLock::new(0));
for _ in 0..60 {
spawn({
let lock = lock.clone();
async move {
let guard = lock.read().await;
dbg!(&lock);
sleep(Duration::from_secs(60 * 60 * 24)).await;
}
});
}
sleep(Duration::from_millis(500)).await;
dbg!(&lock);
lock.read().await; // Blocks for a long time here
}
I'm only reading the RwLock
, so all reads should not block when I try to lock it. Granted, the docs for RwLock
says:
This type of lock allows a number of readers
I just need it to set the permit number to 128 or higher for my project. I'm making a Scratch virtual machine (Scratch the PL) and for big Scratch files they can have more than 32 concurrently executing sprites. I have them all indexed on a RwLock<HashMap<SpriteID, Sprite>>
. When too many sprites are executing, other sprites or the redraw function start blocking for a long time, even if none of them want write access.
Is there an alternative async reader-writer lock that can be set to initialize with 128 permits or more?