wasix-org / wasix-libc

wasix libc implementation for WebAssembly
https://wasi.dev
Other
111 stars 19 forks source link

nanosleep hates nanoseconds! #39

Open Ale-esse opened 3 months ago

Ale-esse commented 3 months ago

Hello everyone,

I installed WASIX for Rust as described in the documentation. I've written a simple code using nanosleep: this program calls a thread, prints the start time, sleeps four times for 500,000,000 nanoseconds each time (2 seconds in total), prints the end time and ends.

In reality, this program doesn't work properly because the nanosleep doesn't pause the thread for two seconds, but returns immediately.

The code is as follows:

extern crate libc;

use libc::{timespec, nanosleep};
use std::thread;
use chrono::prelude::*;

fn main() {
    let thread = thread::spawn(|| {

           let _start_time = Local::now();
           println!("[THREAD] Start time: {}", _start_time.format("%H:%M:%S"));

            let ts = timespec {
                tv_sec: 0,
                tv_nsec: 500000000,
            };

            unsafe {
              nanosleep(&ts, std::ptr::null_mut());
              nanosleep(&ts, std::ptr::null_mut());
              nanosleep(&ts, std::ptr::null_mut());
              nanosleep(&ts, std::ptr::null_mut());
            }

    });

    thread.join().unwrap();

    let _end_time = Local::now();
    println!("[THREAD] End time: {}", _end_time.format("%H:%M:%S"));

} 

The following screenshot shows what happens at runtime:

esec_quattro_nanosleep

I am available to provide further information if required.