C++ Logo

std-proposals

Advanced search

Re: Remove infinite loop UB

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sat, 9 May 2020 18:11:34 +0300
On 2020-05-09 17:49, connor horman via Std-Proposals wrote:
>
> On Sat, May 9, 2020 at 10:39 Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
> On 2020-05-09 17:18, connor horman via Std-Proposals wrote:
> >
> > On Sat, May 9, 2020 at 10:14 Andrey Semashev via Std-Proposals
> > <std-proposals_at_[hidden]
> <mailto:std-proposals_at_[hidden]>
> <mailto:std-proposals_at_[hidden]
> <mailto:std-proposals_at_[hidden]>>>
> > wrote:
> >
> > There is sigwait for this kind of use cases. Or any other kind of
> > blocking - sleep, select, condition_variable::wait, etc.
> >
> > Busy waiting as a means of blocking the thread for extended
> periods of
> > time is not something that should be encouraged, IMHO.
> >
> > Ah yes, that works quite well in embedded/freestanding (or os dev,
> > especially) where I don't have:
> > a) posix
> > b) <thread>
> > c) <mutex>
> > d) Stuff along those lines.
>
> I don't think using pure C/C++ for kernel (or bare metal embedded)
> development is practical. Primarily because these languages require
> certain runtime support and don't implement certain low-level
> primitives, like halting the CPU or switching the current execution
> context or setting up MMU for memory paging and memory mapped IO or
> many
> other hardware-specific things. That includes setting up hardware
> interrupt handlers. These primitives are normally written in assembler.
>
> I do agree (my interrupts are set by a linker script because they have
> to be a specific address which overlaps a text segment. However, things
> like looping forever are a high-level concept that should shouldn't need
> assembly for.

In this case, your infinite loop is just a way to implement halting the
CPU. Most CPUs have specific instructions for that to reduce power
consumption. It is not unreasonable to implement it in assembler, even
if your CPU doesn't have such instructions.

Theoretically, C/C++ could have a standard library function halt() that
would implement this, but thing is you would likely not have it in a
bare metal environment - as you said, you don't have a standard library.

Received on 2020-05-09 10:14:38