Busy loop: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Created page with "{{Template:Stub}} A busy loop is a loop that continuously polls for an event. An alternative to this inefficient use of resources, for example, is to ask for an interrupt or ...")
 
No edit summary
Line 1: Line 1:
== Definition ==
{{Template:Stub}}

A busy loop is a loop that continuously polls for an event. An alternative to this inefficient use of resources, for example, is to ask for an interrupt or sleep until the event occurs.
A busy loop is a loop that continuously polls for an event. An alternative to this inefficient use of resources, for example, is to ask for an interrupt or sleep until the event occurs.


== Use Cases ==
Busy loops, while expensive, can be used efficiently in some cases. You can implement a mutex like so, using a busy loop:
<source lang=c>
<source lang=c>
int lock(mutex *mutex) {
int lock(mutex *mutex) {
Line 11: Line 12:
}
}
</source>
</source>

Busy loops can also be used to poll hardware, although it is inefficient. This is sometimes the only option for some hardware that does not support interrupts (bless it's heart).

Revision as of 22:37, 30 May 2021

Definition

A busy loop is a loop that continuously polls for an event. An alternative to this inefficient use of resources, for example, is to ask for an interrupt or sleep until the event occurs.

Use Cases

Busy loops, while expensive, can be used efficiently in some cases. You can implement a mutex like so, using a busy loop:

int lock(mutex *mutex) {
    // Wait for the mutex to become free.
    while(mutex->in_use);
    mutex->in_use = 1;
    return 0;
}

Busy loops can also be used to poll hardware, although it is inefficient. This is sometimes the only option for some hardware that does not support interrupts (bless it's heart).