Is there a sleep() anywhere that doesn't require me to rearchitect my entire program's IO just to debug it?

I was trying to hunt down an obnoxious infinite loop and I needed to slow down the calls. Of course, I bumped into the fact that sleep has been removed from effectively everywhere.

I had to do:

    const linux = std.os.linux;
    var req = linux.timespec{ .sec = 1, .nsec = 0 };
    _ = linux.nanosleep(&req, null);

just so I could run down my error.

Is there an alternative? Or could we at least get something like std.debug.sleep like we have std.debug.print?

Thanks.

1 Like

Not sure if it’s particularly “idiomatic”, but you could always create a standalone sleep function with an adhoc single-threaded Io object (which should be fairly cheap to create):

fn mySleep(ms: i64) void {
    var io: std.Io.Threaded = .init_single_threaded;
    defer io.deinit();
    io.io().sleep(.fromMilliseconds(ms), .boot) catch {};
}

to add to what @floooh mentioned, you can always just create a global IO object that compiles out in release mode and use that

1 Like

Out of curiosity, if it’s an infinite loop, what’s the reason you’re debugging with sleeps over something like a conditional breakpoint? Introducing variable(s) to trigger on if need be.

To add onto what @floooh and @asibahi said, there is already a global instance available std.Io.Threaded.global_single_threaded

I don’t recommend using it directly, instead use std.Options.debug_io, which is the same by default but can be overridden.

There is also std.Options.debug_threaded_io, which is again the same, but can be overridden separately for some reason.

I think the only reason this exists is so std.start can set arg0 without forcing the compilation of std.Io.Threaded.global_single_threaded by letting you override it (can be disabled with null).

3 Likes

Embedded system with no MMU and the program I’m debugging is supposed to be part of that debug flow.

Quoting James Mickens:
“I HAVE NO TOOLS BECAUSE I’VE DESTROYED MY TOOLS WITH MY TOOLS. My only logging option is to hire monks to transcribe the subjective experience of watching my machines die as I weep tears of blood.”

6 Likes