Smithay / calloop

A callback-based Event Loop
MIT License
176 stars 34 forks source link

Calloop cannot be shared between objects safely #137

Closed harshakhmk closed 1 year ago

harshakhmk commented 1 year ago

Hi, I was experimenting with calloop to run a task periodically, for that I added a method with to eventloop and execute it after sometime, I wrapped the eventloop inside tokio multi-threaded code, but getting an error Rc<calloop::loop_logic::LoopInner<'a, ()>> cannot be shared between threads safely. It would be very helpful, if someone could provide how to make calloop object be shared across threads safely The multi threaded tokio code looks like `tokio::spawn(async move { loop { tokio::time::sleep(idle_duration).await;

    let sem = semaphore.clone().acquire_owned().await;
    tokio::task::spawn_blocking(move || {
        // Eventloop operations
        drop(sem);
    }).await.unwrap();
}

});`

elinorbgr commented 1 year ago

The event-loop of calloop is designed to be a single-threaded event loop, and as such making it threadsafe is currently a non-goal.

Furthermore calloop uses internally its own sleep-wake mechanisms, so mixing it with other runtimes like tokio should be made with extreme care to avoid conflict.

What are you actually trying to achieve?

harshakhmk commented 1 year ago

The event-loop of calloop is designed to be a single-threaded event loop, and as such making it threadsafe is currently a non-goal.

Furthermore calloop uses internally its own sleep-wake mechanisms, so mixing it with other runtimes like tokio should be made with extreme care to avoid conflict.

What are you actually trying to achieve?

I just wanted to add functions with timer to eventloop and execute them periodically for that I have wrapped eventloop code inside tokio loop

elinorbgr commented 1 year ago

But, why are you trying to mix tokio and calloop?

Calloop is an event loop by itself, using it to run periodical callbacks doesn't require tokio at all, and I'm pretty sure tokio can also be used to run periodical callbacks by itself.

harshakhmk commented 1 year ago

I combined tokio & calloop to have periodic function calling, but using TimeoutAction:: ToDuration() can also be used to achieve periodic function calling, without the need of tokio?

elinorbgr commented 1 year ago

Yes, this mechanism is exactly designed to create timers to reprogram themselves automatically, such as invoking the same callback every X seconds.

harshakhmk commented 1 year ago

Yes, this mechanism is exactly designed to create timers to reprogram themselves automatically, such as invoking the same callback every X seconds.

That is what I was looking for, thanks