mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: consolidate the different sleep functions into time.wait(Duration) (#8853)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
module time
|
||||
|
||||
fn assert_greater_time(ms int, t1 Time) {
|
||||
sleep_ms(ms)
|
||||
wait(ms * millisecond)
|
||||
t2 := now()
|
||||
assert t2 > t1
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ fn test_stopwatch_works_as_intended() {
|
||||
mut sw := time.new_stopwatch({})
|
||||
// sample code that you want to measure:
|
||||
println('Hello world')
|
||||
time.sleep_ms(1)
|
||||
time.wait(1 * time.millisecond)
|
||||
//
|
||||
println('Greeting the world took: ${sw.elapsed().nanoseconds()}ns')
|
||||
assert sw.elapsed().nanoseconds() > 0
|
||||
@@ -16,18 +16,18 @@ fn test_stopwatch_works_as_intended() {
|
||||
fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() {
|
||||
println('Testing pause function')
|
||||
mut sw := time.new_stopwatch({})
|
||||
time.sleep_ms(10) // A
|
||||
time.wait(10 * time.millisecond) // A
|
||||
eprintln('Elapsed after 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 8 // sometimes it sleeps for 9ms on windows..
|
||||
sw.pause()
|
||||
time.sleep_ms(10)
|
||||
time.wait(10 * time.millisecond)
|
||||
eprintln('Elapsed after pause and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 8
|
||||
$if stopwatch ? {
|
||||
assert sw.elapsed().milliseconds() < 20
|
||||
}
|
||||
sw.start()
|
||||
time.sleep_ms(10) // B
|
||||
time.wait(10 * time.millisecond) // B
|
||||
eprintln('Elapsed after resume and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
||||
assert sw.elapsed().milliseconds() >= 18
|
||||
$if stopwatch ? {
|
||||
|
||||
@@ -325,31 +325,21 @@ pub fn ticks() i64 {
|
||||
}
|
||||
|
||||
// sleep makes the calling thread sleep for a given number of seconds.
|
||||
[deprecated: 'call time.wait(n * time.second)']
|
||||
pub fn sleep(seconds int) {
|
||||
$if windows {
|
||||
C.Sleep(seconds * 1000)
|
||||
} $else {
|
||||
C.sleep(seconds)
|
||||
}
|
||||
wait(seconds * time.second)
|
||||
}
|
||||
|
||||
// sleep_ms makes the calling thread sleep for a given number of milliseconds.
|
||||
[deprecated: 'call time.wait(n * time.millisecond)']
|
||||
pub fn sleep_ms(milliseconds int) {
|
||||
$if windows {
|
||||
C.Sleep(milliseconds)
|
||||
} $else {
|
||||
C.usleep(milliseconds * 1000)
|
||||
}
|
||||
wait(milliseconds * time.millisecond)
|
||||
}
|
||||
|
||||
// usleep makes the calling thread sleep for a given number of microseconds.
|
||||
[deprecated: 'call time.wait(n * time.microsecond)']
|
||||
pub fn usleep(microseconds int) {
|
||||
$if windows {
|
||||
milliseconds := microseconds / 1000
|
||||
C.Sleep(milliseconds)
|
||||
} $else {
|
||||
C.usleep(microseconds)
|
||||
}
|
||||
wait(microseconds * time.microsecond)
|
||||
}
|
||||
|
||||
// is_leap_year checks if a given a year is a leap year.
|
||||
|
||||
@@ -45,6 +45,8 @@ mut:
|
||||
// the first arg is defined in include/bits/types.h as `__S32_TYPE`, which is `int`
|
||||
fn C.clock_gettime(int, &C.timespec)
|
||||
|
||||
fn C.nanosleep(req &C.timespec, rem &C.timespec) int
|
||||
|
||||
// sys_mono_now returns a *monotonically increasing time*, NOT a time adjusted for daylight savings, location etc.
|
||||
pub fn sys_mono_now() u64 {
|
||||
$if macos {
|
||||
@@ -127,3 +129,9 @@ pub fn zero_timespec() C.timespec {
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
// wait makes the calling thread sleep for a given duration (in nanoseconds).
|
||||
pub fn wait(duration Duration) {
|
||||
ts := &C.timespec{duration / second, duration % second}
|
||||
C.nanosleep(ts, C.NULL)
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ fn test_utc() {
|
||||
|
||||
fn test_unix_time() {
|
||||
t1 := time.utc()
|
||||
time.sleep_ms(50)
|
||||
time.wait(50 * time.millisecond)
|
||||
t2 := time.utc()
|
||||
ut1 := t1.unix_time()
|
||||
ut2 := t2.unix_time()
|
||||
|
||||
@@ -212,3 +212,8 @@ pub struct C.timeval {
|
||||
tv_sec u64
|
||||
tv_usec u64
|
||||
}
|
||||
|
||||
// wait makes the calling thread sleep for a given duration (in nanoseconds).
|
||||
pub fn wait(duration Duration) {
|
||||
C.Sleep(int(duration / millisecond))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user