Page 1 of 1

How can I arrange a delay in your code the kernel in C++?

Posted: Wed Jul 24, 2019 10:17 am
by mrjbom
Hi.
At first I thought I could do it with std::chrono, but then I realized that the functions of std::chrono depend on the system clock. And now I can't figure out how to pause while the code is running.
I write the kernel in C++.

Re: How can I arrange a delay in your code the kernel in C++

Posted: Wed Jul 24, 2019 10:25 am
by iansjack
Basically, you use the timer chip.

It's difficult to go into full detail, but that should provide a starting point for further research.

Re: How can I arrange a delay in your code the kernel in C++

Posted: Wed Jul 24, 2019 10:47 am
by Schol-R-LEA
To add to iansjack's answer (and hopefully provide a positive example for your future research), I offer this wiki search on the term 'timer', which brings up a number of relevant pages.

For an x86 PC (which I assume is your target given your earlier posts), the relevant pages in the wiki are Timer Interrupt Sources, Programmable Interval Timer, and APIC timer (and maybe Chip Numbers, Acronyms and Things for general reference, as well as Real Time Clock if you need to time something against external time at some point). For internal timing relative to the number of clock cycles, you would use the APIC timer, while the PIT is what you need for timing something over an absolute interval.

For Raspberry Pi, the relevant pages are BCM System Timer and ARM Local Timer - or will be someday, as right now both of them are only stub entries. For other SBCs, you'd need to see the documentation on the specific system.

You might also want to do a forum search on 'timer' or 'timing'; while you'll probably need to wade through a fair number of unrelated threads, it is likely to have details and advice which haven't made it into the wiki.

Having said all of that, I have the feeling that there is some missing context in your query; knowing your goal is usually relevant to giving a useful answer, and asking meaningful questions is itself a learned skill. What are you trying to pause, and for what reason? I ask specifically because halting the kernel itself for a fixed period is a very different matter from sleeping a kernel process (or a thread within one - every process has at least one thread, but for single-threading the two terms are often treated as synonymous), which may in turn be different from doing the same for a user process, and pausing a driver while waiting for an I/O operation's interrupt is yet different again. All but the first and possibly the last should be part of your scheduler, for which the interval timer is a necessary but not sufficient requirement.

In other words, what are you trying to have the kernel do, and why?