C++ Logo

std-proposals

Advanced search

Re: Remove infinite loop UB

From: connor horman <chorman64_at_[hidden]>
Date: Mon, 11 May 2020 09:25:25 -0400
On Mon, 11 May 2020 at 03:40, David Brown via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 09/05/2020 15:18, Tony V E via Std-Proposals wrote:
> > The basic idea is a program that looks like:
> >
> > int main()
> > {
> > setupSignalHandlers();
> > for(;;)
> > ;
> > }
> >
> > The real work of the program happens in the signal handlers.
> > Or interrupts. Etc.
> >
> > Nothing happens on the main thread. But it needs to be running for the
> > signal handlers to have somewhere to run.
> >
> > We could have a special function for this:
> >
> > for (;;)
> > std::magic_pretend_im_doing_something();
> >
> > Not sure how we specify it though.
> >
>
> In gcc and clang, you can use:
>
> asm volatile ("" ::: );
>
> It is "volatile", but does absolutely nothing.
>
> It is standard, of course. But I can't see it being difficult to specify.
>
I assume you mean non-standard, though I suppose the declaration itself is
standard, just implementation-defined.
I don't necessarily like non-standard stuff, especially for high-level,
abstract stuff like looping. Technically an infinite loop that does nothing
at all is a valid program for a Universal Turing Machine (of which the C++
Abstract Machine should qualify), so this is an entire theoretical
computing arguement, which I am going to choose to omit.

>
> However, what you /really/ want in loops like this is something like:
>
> while (true) sleep();
>
> where "sleep" is highly cpu specific. Some cpu's have a "sleep"
> instruction, others have a "wait for events", "halt", "HCF", etc.
>
I don't believe all CPUs I target have such an instruction. As far as I can
tell, the 6502 itself only has STP (65816 does have WAI, so that is useful
for that case, but I believe it is a New Instruction, so not available to
older 6502 CPUs), the issues of which I have mentioned previously. 6502
would be for NES games, which is a future target of the same project that
is providing the homebrew platform for SNES Games.

>
> This should only be used at the lowest level of the system - in the main
> loop of a bare-metal embedded system, or in the idle task of an OS. Any
> other task in a system with an OS should block waiting on some event, or
> at the very least "yield" its time-slice.
>
> And these sorts of things are all observable events.
>
> I think the only use I have had for a genuinely empty infinite loop is
> in handling restarts in an embedded system, where the processor is
> twiddling its thumbs waiting for the watchdog to cause a restart.
>
Possibly. That would be an example though.

>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-05-11 08:28:40