Commit 67fb4d1359
Changed files (5)
std
std/atomic/queue.zig
@@ -196,7 +196,7 @@ fn startPuts(ctx: *Context) u8 {
var put_count: usize = puts_per_thread;
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ std.os.time.sleep(1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Queue(i32).Node{
.prev = undefined,
@@ -214,7 +214,7 @@ fn startGets(ctx: *Context) u8 {
const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
while (ctx.queue.get()) |node| {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ std.os.time.sleep(1); // let the os scheduler be our fuzz
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
}
std/atomic/stack.zig
@@ -123,7 +123,7 @@ fn startPuts(ctx: *Context) u8 {
var put_count: usize = puts_per_thread;
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ std.os.time.sleep(1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Stack(i32).Node{
.next = undefined,
@@ -140,7 +140,7 @@ fn startGets(ctx: *Context) u8 {
const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
while (ctx.stack.pop()) |node| {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ std.os.time.sleep(1); // let the os scheduler be our fuzz
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
}
std/event/group.zig
@@ -151,7 +151,7 @@ async fn testGroup(loop: *Loop) void {
}
async fn sleepALittle(count: *usize) void {
- std.os.time.sleep(0, 1000000);
+ std.os.time.sleep(1 * std.os.time.millisecond);
_ = @atomicRmw(usize, count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
}
std/event/rwlock.zig
@@ -267,7 +267,7 @@ async fn writeRunner(lock: *RwLock) void {
var i: usize = 0;
while (i < shared_test_data.len) : (i += 1) {
- std.os.time.sleep(0, 100000);
+ std.os.time.sleep(100 * std.os.time.microsecond);
const lock_promise = async lock.acquireWrite() catch @panic("out of memory");
const handle = await lock_promise;
defer handle.release();
@@ -282,7 +282,7 @@ async fn writeRunner(lock: *RwLock) void {
async fn readRunner(lock: *RwLock) void {
suspend; // resumed by onNextTick
- std.os.time.sleep(0, 1);
+ std.os.time.sleep(1);
var i: usize = 0;
while (i < shared_test_data.len) : (i += 1) {
std/os/time.zig
@@ -11,14 +11,16 @@ const posix = std.os.posix;
pub const epoch = @import("epoch.zig");
/// Sleep for the specified duration
-pub fn sleep(seconds: usize, nanoseconds: usize) void {
+pub fn sleep(nanoseconds: u64) void {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
- posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds));
+ const s = nanoseconds / ns_per_s;
+ const ns = nanoseconds % ns_per_s;
+ posixSleep(@intCast(u63, s), @intCast(u63, ns));
},
Os.windows => {
const ns_per_ms = ns_per_s / ms_per_s;
- const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
+ const milliseconds = nanoseconds / ns_per_ms;
windows.Sleep(@intCast(windows.DWORD, milliseconds));
},
else => @compileError("Unsupported OS"),
@@ -99,6 +101,14 @@ fn milliTimestampPosix() u64 {
return sec_ms + nsec_ms;
}
+/// Multiples of a base unit (nanoseconds)
+pub const nanosecond = 1;
+pub const microsecond = 1000 * nanosecond;
+pub const millisecond = 1000 * microsecond;
+pub const second = 1000 * millisecond;
+pub const minute = 60 * second;
+pub const hour = 60 * minute;
+
/// Divisions of a second
pub const ns_per_s = 1000000000;
pub const us_per_s = 1000000;
@@ -251,7 +261,7 @@ pub const Timer = struct {
};
test "os.time.sleep" {
- sleep(0, 1);
+ sleep(1);
}
test "os.time.timestamp" {
@@ -259,7 +269,7 @@ test "os.time.timestamp" {
const margin = 50;
const time_0 = milliTimestamp();
- sleep(0, ns_per_ms);
+ sleep(ns_per_ms);
const time_1 = milliTimestamp();
const interval = time_1 - time_0;
debug.assert(interval > 0 and interval < margin);
@@ -270,7 +280,7 @@ test "os.time.Timer" {
const margin = ns_per_ms * 150;
var timer = try Timer.start();
- sleep(0, 10 * ns_per_ms);
+ sleep(10 * ns_per_ms);
const time_0 = timer.read();
debug.assert(time_0 > 0 and time_0 < margin);