authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-10-11 19:02:59+13:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-10-11 11:57:59-04:00
log67fb4d13593fb6f940415fda2abd56c452c7a28e
tree0f6c98e549425c18377fa6e59aa5aaad09b571f1
parenta22d9daaecd94f71690e6296aff3ee0a2da00892

Improve time.sleep api


5 files changed, 23 insertions(+), 13 deletions(-)

std/atomic/queue.zig+2-2
...@@ -196,7 +196,7 @@ fn startPuts(ctx: *Context) u8 {...@@ -196,7 +196,7 @@ fn startPuts(ctx: *Context) u8 {
196 var put_count: usize = puts_per_thread;196 var put_count: usize = puts_per_thread;
197 var r = std.rand.DefaultPrng.init(0xdeadbeef);197 var r = std.rand.DefaultPrng.init(0xdeadbeef);
198 while (put_count != 0) : (put_count -= 1) {198 while (put_count != 0) : (put_count -= 1) {
199 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz199 std.os.time.sleep(1); // let the os scheduler be our fuzz
200 const x = @bitCast(i32, r.random.scalar(u32));200 const x = @bitCast(i32, r.random.scalar(u32));
201 const node = ctx.allocator.create(Queue(i32).Node{201 const node = ctx.allocator.create(Queue(i32).Node{
202 .prev = undefined,202 .prev = undefined,
...@@ -214,7 +214,7 @@ fn startGets(ctx: *Context) u8 {...@@ -214,7 +214,7 @@ fn startGets(ctx: *Context) u8 {
214 const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;214 const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
215215
216 while (ctx.queue.get()) |node| {216 while (ctx.queue.get()) |node| {
217 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz217 std.os.time.sleep(1); // let the os scheduler be our fuzz
218 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);218 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
219 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);219 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
220 }220 }
std/atomic/stack.zig+2-2
...@@ -123,7 +123,7 @@ fn startPuts(ctx: *Context) u8 {...@@ -123,7 +123,7 @@ fn startPuts(ctx: *Context) u8 {
123 var put_count: usize = puts_per_thread;123 var put_count: usize = puts_per_thread;
124 var r = std.rand.DefaultPrng.init(0xdeadbeef);124 var r = std.rand.DefaultPrng.init(0xdeadbeef);
125 while (put_count != 0) : (put_count -= 1) {125 while (put_count != 0) : (put_count -= 1) {
126 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz126 std.os.time.sleep(1); // let the os scheduler be our fuzz
127 const x = @bitCast(i32, r.random.scalar(u32));127 const x = @bitCast(i32, r.random.scalar(u32));
128 const node = ctx.allocator.create(Stack(i32).Node{128 const node = ctx.allocator.create(Stack(i32).Node{
129 .next = undefined,129 .next = undefined,
...@@ -140,7 +140,7 @@ fn startGets(ctx: *Context) u8 {...@@ -140,7 +140,7 @@ fn startGets(ctx: *Context) u8 {
140 const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;140 const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
141141
142 while (ctx.stack.pop()) |node| {142 while (ctx.stack.pop()) |node| {
143 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz143 std.os.time.sleep(1); // let the os scheduler be our fuzz
144 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);144 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
145 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);145 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
146 }146 }
std/event/group.zig+1-1
...@@ -151,7 +151,7 @@ async fn testGroup(loop: *Loop) void {...@@ -151,7 +151,7 @@ async fn testGroup(loop: *Loop) void {
151}151}
152152
153async fn sleepALittle(count: *usize) void {153async fn sleepALittle(count: *usize) void {
154 std.os.time.sleep(0, 1000000);154 std.os.time.sleep(1 * std.os.time.millisecond);
155 _ = @atomicRmw(usize, count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);155 _ = @atomicRmw(usize, count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
156}156}
157157
std/event/rwlock.zig+2-2
...@@ -267,7 +267,7 @@ async fn writeRunner(lock: *RwLock) void {...@@ -267,7 +267,7 @@ async fn writeRunner(lock: *RwLock) void {
267267
268 var i: usize = 0;268 var i: usize = 0;
269 while (i < shared_test_data.len) : (i += 1) {269 while (i < shared_test_data.len) : (i += 1) {
270 std.os.time.sleep(0, 100000);270 std.os.time.sleep(100 * std.os.time.microsecond);
271 const lock_promise = async lock.acquireWrite() catch @panic("out of memory");271 const lock_promise = async lock.acquireWrite() catch @panic("out of memory");
272 const handle = await lock_promise;272 const handle = await lock_promise;
273 defer handle.release();273 defer handle.release();
...@@ -282,7 +282,7 @@ async fn writeRunner(lock: *RwLock) void {...@@ -282,7 +282,7 @@ async fn writeRunner(lock: *RwLock) void {
282282
283async fn readRunner(lock: *RwLock) void {283async fn readRunner(lock: *RwLock) void {
284 suspend; // resumed by onNextTick284 suspend; // resumed by onNextTick
285 std.os.time.sleep(0, 1);285 std.os.time.sleep(1);
286286
287 var i: usize = 0;287 var i: usize = 0;
288 while (i < shared_test_data.len) : (i += 1) {288 while (i < shared_test_data.len) : (i += 1) {
std/os/time.zig+16-6
...@@ -11,14 +11,16 @@ const posix = std.os.posix;...@@ -11,14 +11,16 @@ const posix = std.os.posix;
11pub const epoch = @import("epoch.zig");11pub const epoch = @import("epoch.zig");
1212
13/// Sleep for the specified duration13/// Sleep for the specified duration
14pub fn sleep(seconds: usize, nanoseconds: usize) void {14pub fn sleep(nanoseconds: u64) void {
15 switch (builtin.os) {15 switch (builtin.os) {
16 Os.linux, Os.macosx, Os.ios => {16 Os.linux, Os.macosx, Os.ios => {
17 posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds));17 const s = nanoseconds / ns_per_s;
18 const ns = nanoseconds % ns_per_s;
19 posixSleep(@intCast(u63, s), @intCast(u63, ns));
18 },20 },
19 Os.windows => {21 Os.windows => {
20 const ns_per_ms = ns_per_s / ms_per_s;22 const ns_per_ms = ns_per_s / ms_per_s;
21 const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;23 const milliseconds = nanoseconds / ns_per_ms;
22 windows.Sleep(@intCast(windows.DWORD, milliseconds));24 windows.Sleep(@intCast(windows.DWORD, milliseconds));
23 },25 },
24 else => @compileError("Unsupported OS"),26 else => @compileError("Unsupported OS"),
...@@ -99,6 +101,14 @@ fn milliTimestampPosix() u64 {...@@ -99,6 +101,14 @@ fn milliTimestampPosix() u64 {
99 return sec_ms + nsec_ms;101 return sec_ms + nsec_ms;
100}102}
101103
104/// Multiples of a base unit (nanoseconds)
105pub const nanosecond = 1;
106pub const microsecond = 1000 * nanosecond;
107pub const millisecond = 1000 * microsecond;
108pub const second = 1000 * millisecond;
109pub const minute = 60 * second;
110pub const hour = 60 * minute;
111
102/// Divisions of a second112/// Divisions of a second
103pub const ns_per_s = 1000000000;113pub const ns_per_s = 1000000000;
104pub const us_per_s = 1000000;114pub const us_per_s = 1000000;
...@@ -251,7 +261,7 @@ pub const Timer = struct {...@@ -251,7 +261,7 @@ pub const Timer = struct {
251};261};
252262
253test "os.time.sleep" {263test "os.time.sleep" {
254 sleep(0, 1);264 sleep(1);
255}265}
256266
257test "os.time.timestamp" {267test "os.time.timestamp" {
...@@ -259,7 +269,7 @@ test "os.time.timestamp" {...@@ -259,7 +269,7 @@ test "os.time.timestamp" {
259 const margin = 50;269 const margin = 50;
260270
261 const time_0 = milliTimestamp();271 const time_0 = milliTimestamp();
262 sleep(0, ns_per_ms);272 sleep(ns_per_ms);
263 const time_1 = milliTimestamp();273 const time_1 = milliTimestamp();
264 const interval = time_1 - time_0;274 const interval = time_1 - time_0;
265 debug.assert(interval > 0 and interval < margin);275 debug.assert(interval > 0 and interval < margin);
...@@ -270,7 +280,7 @@ test "os.time.Timer" {...@@ -270,7 +280,7 @@ test "os.time.Timer" {
270 const margin = ns_per_ms * 150;280 const margin = ns_per_ms * 150;
271281
272 var timer = try Timer.start();282 var timer = try Timer.start();
273 sleep(0, 10 * ns_per_ms);283 sleep(10 * ns_per_ms);
274 const time_0 = timer.read();284 const time_0 = timer.read();
275 debug.assert(time_0 > 0 and time_0 < margin);285 debug.assert(time_0 > 0 and time_0 < margin);
276286