Can I sleep with no_std?

This might be impossible but can I sleep the program for say 1 sec with #![no_std]?

Sleep is a functionality provided by the operating system. The standard library provides abstractions over general operating systems, including the sleep. #[no_std] disables standard library to allow to use Rust on environments even without operating system. For example, within the WASM runs on the web browser you can't sleep the execution synchronously in any way besides the busy loop.

But if you want to run your code in some specific platform, that platform may have its own way to sleep. If this is your case, what platform are you thinking of?

7 Likes

I was trying to make a simple os from GitHub - phil-opp/blog_os at post-01 so that would not give me any specific platform to work with.

You would need platform specific code for each platform you want to support.

So I looked into it and I saw that I can use _rdtsc in core::arch::x86_64 - Rust but is there any way that I can make that into system time?

How far down the rabbit hole do you want to go?

Normally sleeping will be implemented by configuring one of your CPU's timers to go off after a particular amount of time. For example, you might set it to run at 16MHz and count down from 6,000,000, then put the CPU in low power mode until the timer's interrupt is fired to provide a 0.5 second delay.

Phil Oppermann has an excellent blog series on implementing an OS in Rust, and in the Hardware Interrupts chapter it goes through using timers in this way. All the specifics are for a completely different chip, but the general concept is the same and you should be able to find the relevant details for x86 online.

2 Likes

I looked over it and thank you I will try it and see if it works.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.