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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
mrjbom
Member
Member
Posts: 300
Joined: Sun Jul 21, 2019 7:34 am

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

Post 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++.
User avatar
iansjack
Member
Member
Posts: 4604
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA
Contact:

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

Post 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?
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Post Reply