| 1 |
const std = @import("std"); |
| 2 |
const builtin = @import("builtin"); |
| 3 |
const c = @cImport(@cInclude("time.h")); |
| 4 |
const errno = @cImport(@cInclude("errno.h")); |
| 5 |
|
| 6 |
extern "c" fn printf(format: [*c]const c_char, ...) c_int; |
| 7 |
|
| 8 |
// Win32 Sleep takes milliseconds. Declared here (not pulled from std.os.windows) |
| 9 |
// because std uses inline syscalls / per-arch glue we don't want in transpiled C. |
| 10 |
extern "kernel32" fn Sleep(dwMilliseconds: u32) callconv(.winapi) void; |
| 11 |
|
| 12 |
// Note: Using printf to avoid the extra code from std.log/std.debug. Those won't |
| 13 |
// compile because they are internally using syscalls (for Mutexes) which aren't cross-platform. |
| 14 |
// |
| 15 |
// This wrapper converts Zig string literals to null-terminated c_char arrays and handles |
| 16 |
// variadic argument forwarding to the C printf function. |
| 17 |
pub fn print(comptime fmt: []const u8, args: anytype) void { |
| 18 |
// Create a comptime null-terminated c_char array from the format string |
| 19 |
const fmt_z = comptime blk: { |
| 20 |
var buf: [fmt.len:0]c_char = undefined; |
| 21 |
for (fmt, 0..) |byte, i| { |
| 22 |
buf[i] = byte; |
| 23 |
} |
| 24 |
buf[fmt.len] = 0; |
| 25 |
break :blk buf; |
| 26 |
}; |
| 27 |
|
| 28 |
_ = @call(.auto, printf, .{&fmt_z} ++ args); |
| 29 |
} |
| 30 |
|
| 31 |
// Sleep for at least the given number of nanoseconds. |
| 32 |
// |
| 33 |
// Avoids std.Thread.sleep / std.time.sleep because those use inline syscalls on |
| 34 |
// Linux that aren't portable across architectures. On POSIX we go straight to |
| 35 |
// nanosleep; on Windows we fall back to Sleep (millisecond resolution). |
| 36 |
pub fn sleep(nanoseconds: u64) void { |
| 37 |
if (builtin.os.tag == .windows) { |
| 38 |
const ms_u64 = nanoseconds / std.time.ns_per_ms; |
| 39 |
const ms: u32 = @intCast(@min(ms_u64, std.math.maxInt(u32))); |
| 40 |
Sleep(ms); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
const s = nanoseconds / std.time.ns_per_s; |
| 45 |
const ns = nanoseconds % std.time.ns_per_s; |
| 46 |
|
| 47 |
var req: c.struct_timespec = .{ |
| 48 |
.tv_sec = std.math.cast(c.time_t, s) orelse std.math.maxInt(c.time_t), |
| 49 |
.tv_nsec = std.math.cast(c_long, ns) orelse std.math.maxInt(c_long), |
| 50 |
}; |
| 51 |
var rem: c.struct_timespec = undefined; |
| 52 |
|
| 53 |
while (true) { |
| 54 |
const ret = c.nanosleep(&req, &rem); |
| 55 |
|
| 56 |
if (ret == errno.EINTR) { |
| 57 |
req = rem; |
| 58 |
continue; |
| 59 |
} else { |
| 60 |
return; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
test "sleep for at least 1 second" { |
| 66 |
const start = try std.time.Instant.now(); |
| 67 |
sleep(1 * std.time.ns_per_s); |
| 68 |
const end = try std.time.Instant.now(); |
| 69 |
|
| 70 |
const elapsed_ns: u64 = end.since(start); |
| 71 |
const elapsed_s = elapsed_ns / std.time.ns_per_s; |
| 72 |
|
| 73 |
std.debug.assert(elapsed_s >= 1); |
| 74 |
std.debug.assert(elapsed_s < 2); |
| 75 |
} |
| 76 |
|
| 77 |
test "print function works without crashing" { |
| 78 |
// Test with no arguments |
| 79 |
print("Hello, World!\n", .{}); |
| 80 |
|
| 81 |
// Test with string argument |
| 82 |
print("Hello, %s!\n", .{"Zig"}); |
| 83 |
|
| 84 |
// Test with multiple arguments |
| 85 |
print("Number: %d, String: %s\n", .{ @as(c_int, 42), "test" }); |
| 86 |
|
| 87 |
// Test with format specifiers that need proper types |
| 88 |
print("Precision test: %.*s\n", .{ @as(c_int, 5), "HelloWorld" }); |
| 89 |
} |