I have a polling function that will forever poll and always do pending. Inside the polling function poll_event_loop I want to control timings when the context should be polled again and the function to be called again in x seconds depending on some conditions. I could do this using another thread which calls the waker.wake_by_ref function. But this feels like a cheat. How could I do this without other threads.
poll_fn(|cx| self.poll_event_loop(cx)).await
// function will never be Ready, always pending, polling sould be fast so not just sleeping x seconds inside
fn poll_event_loop(&mut self, cx: &mut Context) -> Poll<anyhow::Result<()>> {
while some_codeandfunc() { /*....*/ }
// guarantee another poll_fn in 1 sec
if condition {
context_callback(cx, 1000);
}
// guarantee another poll_fn in 2 sec
if condition {
context_callback(cx, 2000);
}
Poll::Pending
}
fn context_callback(context: &mut Context, millisec: u64) {
let mut future = Box::pin(tokio::time::sleep(Durationtk::from_millis(millisec)));
//let cb = future.as_mut().poll(context);
future.poll_unpin(context);
}
// ugly way to auto poll the function every x seconds
fn spawn_qeueu_thread(waker: &Waker, rx: &Receiver<String>) -> Option<JoinHandle<()>> {
debug!("doing spawning thread");
//self.thread_spawned = true;
let waker = waker.clone();
let rx2 = rx.clone();
let spawn = tokio::spawn(async move {
loop {
tokio::time::sleep(Durationtk::from_millis(WAKEUPINTERVAL)).await;
debug!("doing other thread wakebyref");
waker.wake_by_ref();
let try_result = rx2.try_recv();
match try_result {
Err(_) => {}
Ok(_msg) => break,
}
}
debug!("ending spawned thread");
});
return Some(spawn);
//self.threadhandle = Some(spawn);
}
After some experimenting, I found a working solution that lets you call the polling function on multiple desired times, working example:
use chrono::Utc;
use futures::FutureExt;
use futures::future::poll_fn;
use tokio::time::Sleep;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use tokio::time::Duration as Durationtk;
pub struct ControllerModule {
vec: Vec<Pin<Box<Sleep>>>,
i: i64,
}
impl ControllerModule {
fn new() -> Self {
let vec = vec![];
let i = 0;
Self { vec, i }
}
async fn start(&mut self) {
poll_fn(|cx| self.poll_event_loop(cx)).await;
print!("worked");
}
fn poll_event_loop(&mut self, context: &mut Context) -> Poll<anyhow::Result<()>> {
self.i += 1;
if self.i % 3 == 0 {
let mut sleep = Box::pin(tokio::time::sleep(Durationtk::from_millis(5000)));
sleep.poll_unpin(context);
self.vec.push(sleep);
} else if self.i % 3 == 1 {
let mut sleep = Box::pin(tokio::time::sleep(Durationtk::from_millis(4000)));
sleep.poll_unpin(context);
self.vec.push(sleep);
} else {
context.waker().wake_by_ref();
}
self.vec.retain(|e| !e.is_elapsed());
Poll::Pending
}
}
#[tokio::main]
async fn main() {
let mut i = ControllerModule::new();
i.start().await
}
Related
I have created a very simplified example of the code I am having an issue with:
use core::time;
use std::thread;
use tokio::sync::{mpsc::Receiver, RwLock};
struct MyStruct {
counter: Arc<RwLock<i32>>,
rx: RwLock<Receiver<i32>>,
}
impl MyStruct {
async fn start_here(&self) { // <--------- Lifetime error here on self
while let Some(message) = self.rx.write().await.recv().await {
tokio::spawn(self.do_some_work_then_update_counter());
}
}
async fn do_some_work_then_update_counter(&self) {
let dur = time::Duration::from_millis(10000);
thread::sleep(dur);
let mut counter = self.counter.write().await;
*counter += 1;
}
}
There is a receiver that is receiving messages from another part of the program, and I want to be able to process each message in its own task to prevent blocking the next message from being processed.
As you can imagine it's a lifetime error since the task could outlast self in this case.
One solution I have done is this:
impl MyStruct {
async fn start_here(&self) {
while let Some(message) = self.rx.write().await.recv().await {
let counter = self.counter.clone();
tokio::spawn(do_some_work_then_update_counter(counter));
}
}
}
async fn do_some_work_then_update_counter(counter: Arc<RwLock<i32>>) {
let dur = time::Duration::from_millis(10000);
thread::sleep(dur);
let mut counter = counter.write().await;
*counter += 1;
}
This just doesn't seem like a good option, I want to keep do_some_work_then_update_counter as an impl of MyStruct instead of a free function since it is modifying data on MyStruct.
I am wondering if there is a better solution to this?
You can if you'll return impl Future directly instead of being async fn:
fn do_some_work_then_update_counter(&self) -> impl Future<Output = ()> {
let counter = Arc::clone(&self.counter);
async move {
let dur = time::Duration::from_millis(10000);
thread::sleep(dur);
let mut counter = counter.write().await;
*counter += 1;
}
}
further along in trying to port my c++ timer queue to rust. Reading a lot of other peoples code, the rust library etc I came up with this scheme
I have an outer struct that clients use. It is simply a wrapper around an inner implementation. So I have
type QIFunc = Box<dyn Fn() -> () + Send>;
pub struct TimerQueueItem {
when: Instant,
name: String,
what: QIFunc,
}
struct _TimerQueue {
running: bool,
stop: bool,
condvar: Condvar,
queue: Vec<TimerQueueItem>,
}
struct TimerQueue {
inner: Arc<Mutex<_TimerQueue>>,
}
There is a dedicated thread that picks things off this queue and runs them. Works fine without the timing, what I need to do is a classic condvar thing
lock cv -> is queue empty -> wait. else pick item off queue (queue is sorted by due time)
look at item -> is it due yet, yes run it, no lock cv till it is due
while sleeping a new thing gets posted, so notify the cv to wake up thread and look at head of queue again.
I want to use the mutex thats in my Arc<Mutex<_TimerQueue>> since thats the logical queue lock. But I cannot work out how to get at it in such a way that it will compile.
I have this
impl TimerQueue {
fn thr(&self) -> () {
let inner = self.inner.clone();
thread::spawn(move || {
loop {
//let tqi:TimerQueueItem;
loop {
let tqimp = &match inner.lock() {
Ok(tqimp) => tqimp,
_ => continue,
};
while !tqimp.stop && tqimp.queue.is_empty() {
tqimp.condvar.wait(xxx);
}
if tqimp.stop {
return;
}
let now = Instant::now();
let tqi = &tqimp.queue[0];
let due = tqi.when;
if due > now {
let wait = due - now;
tqimp.condvar.wait_timeout(xxx, wait);
let tqi = &tqimp.queue[0];
let now = Instant::now();
let due = tqi.when;
if due <= now {
(tqi.what)();
}
} else {
let tqi = &tqimp.queue[0];
(tqi.what)();
}
}
// (tqi.what)();
}
});
}
but cannot work out what I need to put in the cv wait calls (where there is xxx at the moment). I have tried &, * clone,,,,. I am stuck. (The commented out code is me trying to pull out the execution of the 'what' to be outside the lock, stuck on that too but I am sure I can work that one out)
EDIT:
heres the entire source
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
use std::time::Instant;
type QIFunc = Box<dyn Fn() -> () + Send>;
struct TimerQueueItem {
when: Instant,
name: String,
what: QIFunc,
}
struct _TimerQueue {
running: bool,
stop: bool,
condvar: Arc<Condvar>,
queue: Vec<TimerQueueItem>,
}
pub struct TimerQueue {
inner: Arc<Mutex<_TimerQueue>>,
}
impl TimerQueue {
fn thr(&self) -> () {
let inner = self.inner.clone();
thread::spawn(move || {
loop {
//let tqi:TimerQueueItem;
loop {
let mut tqimp = &match inner.lock() {
Ok(tqimp) => tqimp,
_ => continue,
};
let cv = tqimp.condvar.clone();
while !tqimp.stop && tqimp.queue.is_empty() {
cv.wait(xxx);
}
if tqimp.stop {
return;
}
let now = Instant::now();
let tqi = &tqimp.queue[0];
let due = tqi.when;
if due > now {
let wait = due - now;
cv.wait_timeout(xxx, wait);
let tqi = &tqimp.queue[0];
let now = Instant::now();
let due = tqi.when;
if due <= now {
(tqi.what)();
}
} else {
let tqi = &tqimp.queue[0];
(tqi.what)();
}
}
// (tqi.what)();
}
});
}
fn set(&self, f: QIFunc, n: String, when: Instant) {
let qi = TimerQueueItem {
what: f,
name: n,
when: when,
};
let mut inner = self.inner.lock().unwrap();
inner.queue.push(qi);
inner.queue.sort_by_key(|k| k.when);
}
fn new() -> TimerQueue {
let inner = Arc::new(Mutex::new(_TimerQueue {
queue: Vec::new(),
running: false,
stop: false,
condvar: Arc::new(Condvar::new()),
}));
TimerQueue { inner: inner }
}
}
fn main() {
let x = || {
println!("hello");
};
let y = || {
println!("hello2");
};
let tq = TimerQueue::new();
tq.set(Box::new(x), String::from("yo"), Instant::now());
tq.set(Box::new(y), String::from("yo"), Instant::now());
thread::sleep_ms(10000);
}
Normally you should put the MutexGuard that was returned by lock, which in your case is tqimp. But you can't because that would require a mutable borrow (in the parameter) at the same time as another borrow (to access tqimp.condvar). You will need to either store the CondVar outside the queue or to use an Arc:
struct _TimerQueue {
running: bool,
stop: bool,
condvar: Arc<Condvar>,
queue: Vec<TimerQueueItem>,
}
Then when you want to use it:
let mut tqimp = match inner.lock() { // tqimp can't be a reference
Ok(tqimp) => tqimp,
_ => continue,
};
let condvar = tqimp.condvar.clone();
tqimp = condvar.wait (tqimp).unwrap();
Note that condvar.wait consumes tqimp and returns a new MutexGuard (because it releases the mutex until the condition triggers, then locks it again before returning).
struct ThreadHolder{
state: ???
thread: ???
}
impl ThreadHolder {
fn launch(&mut self) {
self.thread = ???
// in thread change self.state
}
}
#[test]
fn test() {
let mut th = ThreadHolder{...};
th.launch();
// thread will be destroy as soon as th go out of scope
}
I think there is something to deal with lifetime, but I don't know how to write it.
What you want is so simple that you don't even need it to be mutable in any way, and then it becomes trivial to share it across threads, unless you want to reset it. You said you need to leave a thread, for one reason or another, therefore I'll assume that you don't care about this.
You instead can poll it every tick (most games run in ticks so I don't think there will be any issue implementing that).
I will provide example that uses sleep, so it's not most accurate thing, it is painfully obvious on the last subsecond duration, but I am not trying to do your work for you anyway, there's enough resources on internet that can help you deal with it.
Here it goes:
use std::{
sync::Arc,
thread::{self, Result},
time::{Duration, Instant},
};
struct Timer {
end: Instant,
}
impl Timer {
fn new(duration: Duration) -> Self {
// this code is valid for now, but might break in the future
// future so distant, that you really don't need to care unless
// you let your players draw for eternity
let end = Instant::now().checked_add(duration).unwrap();
Timer { end }
}
fn left(&self) -> Duration {
self.end.saturating_duration_since(Instant::now())
}
// more usable than above with fractional value being accounted for
fn secs_left(&self) -> u64 {
let span = self.left();
span.as_secs() + if span.subsec_millis() > 0 { 1 } else { 0 }
}
}
fn main() -> Result<()> {
let timer = Timer::new(Duration::from_secs(10));
let timer_main = Arc::new(timer);
let timer = timer_main.clone();
let t = thread::spawn(move || loop {
let seconds_left = timer.secs_left();
println!("[Worker] Seconds left: {}", seconds_left);
if seconds_left == 0 {
break;
}
thread::sleep(Duration::from_secs(1));
});
loop {
let seconds_left = timer_main.secs_left();
println!("[Main] Seconds left: {}", seconds_left);
if seconds_left == 5 {
println!("[Main] 5 seconds left, waiting for worker thread to finish work.");
break;
}
thread::sleep(Duration::from_secs(1));
}
t.join()?;
println!("[Main] worker thread finished work, shutting down!");
Ok(())
}
By the way, this kind of implementation wouldn't be any different in any other language, so please don't blame Rust for it. It's not the easiest language, but it provides more than enough tools to build anything you want from scratch as long as you put effort into it.
Goodluck :)
I think I got it work
use std::sync::{Arc, Mutex};
use std::thread::{sleep, spawn, JoinHandle};
use std::time::Duration;
struct Timer {
pub(crate) time: Arc<Mutex<u32>>,
jh_ticker: Option<JoinHandle<()>>,
}
impl Timer {
fn new<T>(i: T, duration: Duration) -> Self
where
T: Iterator<Item = u32> + Send + 'static,
{
let time = Arc::new(Mutex::new(0));
let arc_time = time.clone();
let jh_ticker = Some(spawn(move || {
for item in i {
let mut mg = arc_time.lock().unwrap();
*mg = item;
drop(mg); // needed, otherwise this thread will always hold lock
sleep(duration);
}
}));
Timer { time, jh_ticker }
}
}
impl Drop for Timer {
fn drop(&mut self) {
self.jh_ticker.take().unwrap().join();
}
}
#[test]
fn test_timer() {
let t = Timer::new(0..=10, Duration::from_secs(1));
let a = t.time.clone();
for _ in 0..100 {
let b = *a.lock().unwrap();
println!("{}", b);
sleep(Duration::from_millis(100));
}
}
I implemented the future and made a request of it, but it blocked my curl and the log shows that poll was only invoked once.
Did I implement anything wrong?
use failure::{format_err, Error};
use futures::{future, Async};
use hyper::rt::Future;
use hyper::service::{service_fn, service_fn_ok};
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use log::{debug, error, info};
use std::{
sync::{Arc, Mutex},
task::Waker,
thread,
};
pub struct TimerFuture {
shared_state: Arc<Mutex<SharedState>>,
}
struct SharedState {
completed: bool,
resp: String,
}
impl Future for TimerFuture {
type Item = Response<Body>;
type Error = hyper::Error;
fn poll(&mut self) -> futures::Poll<Response<Body>, hyper::Error> {
let mut shared_state = self.shared_state.lock().unwrap();
if shared_state.completed {
return Ok(Async::Ready(Response::new(Body::from(
shared_state.resp.clone(),
))));
} else {
return Ok(Async::NotReady);
}
}
}
impl TimerFuture {
pub fn new(instance: String) -> Self {
let shared_state = Arc::new(Mutex::new(SharedState {
completed: false,
resp: String::new(),
}));
let thread_shared_state = shared_state.clone();
thread::spawn(move || {
let res = match request_health(instance) {
Ok(status) => status.clone(),
Err(err) => {
error!("{:?}", err);
format!("{}", err)
}
};
let mut shared_state = thread_shared_state.lock().unwrap();
shared_state.completed = true;
shared_state.resp = res;
});
TimerFuture { shared_state }
}
}
fn request_health(instance_name: String) -> Result<String, Error> {
std::thread::sleep(std::time::Duration::from_secs(1));
Ok("health".to_string())
}
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = hyper::Error> + Send>;
fn serve_health(req: Request<Body>) -> BoxFut {
let mut response = Response::new(Body::empty());
let path = req.uri().path().to_owned();
match (req.method(), path) {
(&Method::GET, path) => {
return Box::new(TimerFuture::new(path.clone()));
}
_ => *response.status_mut() = StatusCode::NOT_FOUND,
}
Box::new(future::ok(response))
}
fn main() {
let endpoint_addr = "0.0.0.0:8080";
match std::thread::spawn(move || {
let addr = endpoint_addr.parse().unwrap();
info!("Server is running on {}", addr);
hyper::rt::run(
Server::bind(&addr)
.serve(move || service_fn(serve_health))
.map_err(|e| eprintln!("server error: {}", e)),
);
})
.join()
{
Ok(e) => e,
Err(e) => println!("{:?}", e),
}
}
After compile and run this code, a server with port 8080 is running. Call the server with curl and it will block:
curl 127.0.0.1:8080/my-health-scope
Did I implement anything wrong?
Yes, you did not read and follow the documentation for the method you are implementing (emphasis mine):
When a future is not ready yet, the Async::NotReady value will be returned. In this situation the future will also register interest of the current task in the value being produced. This is done by calling task::park to retrieve a handle to the current Task. When the future is then ready to make progress (e.g. it should be polled again) the unpark method is called on the Task.
As a minimal, reproducible example, let's use this:
use futures::{future::Future, Async};
use std::{
mem,
sync::{Arc, Mutex},
thread,
time::Duration,
};
pub struct Timer {
data: Arc<Mutex<String>>,
}
impl Timer {
pub fn new(instance: String) -> Self {
let data = Arc::new(Mutex::new(String::new()));
thread::spawn({
let data = data.clone();
move || {
thread::sleep(Duration::from_secs(1));
*data.lock().unwrap() = instance;
}
});
Timer { data }
}
}
impl Future for Timer {
type Item = String;
type Error = ();
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
let mut data = self.data.lock().unwrap();
eprintln!("poll was called");
if data.is_empty() {
Ok(Async::NotReady)
} else {
let data = mem::replace(&mut *data, String::new());
Ok(Async::Ready(data))
}
}
}
fn main() {
let v = Timer::new("Some text".into()).wait();
println!("{:?}", v);
}
It only prints out "poll was called" once.
You can call task::current (previously task::park) in the implementation of Future::poll, save the resulting value, then use the value with Task::notify (previously Task::unpark) whenever the future may be polled again:
use futures::{
future::Future,
task::{self, Task},
Async,
};
use std::{
mem,
sync::{Arc, Mutex},
thread,
time::Duration,
};
pub struct Timer {
data: Arc<Mutex<(String, Option<Task>)>>,
}
impl Timer {
pub fn new(instance: String) -> Self {
let data = Arc::new(Mutex::new((String::new(), None)));
let me = Timer { data };
thread::spawn({
let data = me.data.clone();
move || {
thread::sleep(Duration::from_secs(1));
let mut data = data.lock().unwrap();
data.0 = instance;
if let Some(task) = data.1.take() {
task.notify();
}
}
});
me
}
}
impl Future for Timer {
type Item = String;
type Error = ();
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
let mut data = self.data.lock().unwrap();
eprintln!("poll was called");
if data.0.is_empty() {
let v = task::current();
data.1 = Some(v);
Ok(Async::NotReady)
} else {
let data = mem::replace(&mut data.0, String::new());
Ok(Async::Ready(data))
}
}
}
fn main() {
let v = Timer::new("Some text".into()).wait();
println!("{:?}", v);
}
See also:
Why does Future::select choose the future with a longer sleep period first?
Why is `Future::poll` not called repeatedly after returning `NotReady`?
What is the best approach to encapsulate blocking I/O in future-rs?
I have two parts of code that I want to run in a loop. Sometimes I need to make the loop 'sleep', making each iteration skip the second part. The loop should stop sleeping after a set amount of time (for example using a thread with a call to thread::sleep). How do I accomplish this?
use std::thread;
let mut sleeping = false;
let mut handle = thread::spawn(|| {});
loop {
part_1();
if sleeping {
continue;
}
part_2();
if some_condition {
sleeping = true;
handle = thread::spawn(|| thread::sleep_ms(100));
}
}
In this example, if the condition is met, the part_2 call would be skipped for some amount of iterations. My use case is continuing to run graphical updates in a game, while freezing the game's logic (such as counting down timers).
There is no need for the overhead of threads or even the need to sleep. Simply track the time that you should delay executing code until:
use std::time::{Duration, Instant};
fn part_1() {}
fn part_2() {}
fn some_condition() -> bool {
false
}
fn main() {
let mut sleep_until = None;
loop {
part_1();
if let Some(until) = sleep_until {
if until > Instant::now() {
continue;
}
}
part_2();
if some_condition() {
let now = Instant::now();
let until = now + Duration::from_millis(500);
sleep_until = Some(until);
}
}
}
Although I'd probably avoid the use of continue here, and instead embed the logic within:
use std::time::{Duration, Instant};
fn perform_physics_calculation() {}
fn perform_graphics_render() {}
fn main() {
let mut next_graphics_update = Instant::now();
let graphics_delay = Duration::from_millis(500);
loop {
let now = Instant::now();
perform_physics_calculation();
if next_graphics_update <= now {
perform_graphics_render();
next_graphics_update = now + graphics_delay;
}
}
}
Note in one case I use an Option<Instant> and in the other I just use an Instant; both cases can make sense.
Turn your sleeping variable into a reference-counted atomic boolean so that you can reset it on the sleeping thread.
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;
fn part_1() {}
fn part_2() {}
fn some_condition() -> bool { false }
fn main() {
let sleeping = Arc::new(AtomicBool::new(false));
let mut handle = None;
loop {
part_1();
if sleeping.load(Ordering::Acquire) {
continue;
}
part_2();
if some_condition() {
sleeping.store(true, Ordering::Release);
let sleeping_clone = sleeping.clone();
handle = Some(thread::spawn(move || {
thread::sleep(Duration::from_millis(100));
sleeping_clone.store(false, Ordering::Release);
}));
}
}
}