-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std/time: reimplement the Sleep function
- Loading branch information
1 parent
7cd8768
commit c931a33
Showing
9 changed files
with
146 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
// Reports whether n fits in bits. | ||
fn Fits(n: i64, bits: uint): bool { | ||
const MaxBits = 64 | ||
if bits < MaxBits { | ||
if n < 0 { | ||
limit := i64(-1) << (bits - 1) | ||
if n < limit { | ||
ret false | ||
} | ||
} else { | ||
limit := i64(1)<<(bits-1) - 1 | ||
if n > limit { | ||
ret false | ||
} | ||
} | ||
} | ||
ret true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
type sleepDuration: i64 | ||
|
||
// See documentation of the `time::Sleep` function. | ||
fn sleep(dur: sleepDuration) { _sleep(dur) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
use "std/internal/zbits" | ||
use "std/mem" | ||
|
||
cpp unsafe fn nanosleep(*cpp.timespec, *cpp.timespec): int | ||
|
||
// See documentation of the sleep function. | ||
fn _sleep(dur: sleepDuration) { | ||
mut sec := dur / _Second | ||
nsec := dur % _Second // always fits in timespec.tv_nsec | ||
|
||
mut req := cpp.timespec{} | ||
secBits := mem::SizeOf(req.tv_sec) * 8 | ||
limit := i64(1)<<(secBits-1) - 1 | ||
for ; sec -= limit { | ||
if sec <= limit { | ||
req.tv_sec = sec | ||
req.tv_nsec = nsec | ||
unsafe { cpp.nanosleep(&req, nil) } | ||
break | ||
} else { | ||
req.tv_sec = limit | ||
unsafe { cpp.nanosleep(&req, nil) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
use "std/internal/zbits" | ||
|
||
type _DWORD: u32 | ||
|
||
cpp fn Sleep(_DWORD) | ||
|
||
// See documentation of the sleep function. | ||
fn _sleep(dur: sleepDuration) { | ||
mut ms := dur / _Millisecond | ||
if ms == 0 { | ||
// Less than a millisecond, Windows's Sleep function have no precision <1ms. | ||
// To make guaranteed at least given duration is elapsed, sleep for a millisecond. | ||
ms = 1 | ||
} | ||
const dwordBits = 32 | ||
limit := i64(1) << (dwordBits) | ||
for ; ms -= limit { | ||
if ms <= limit { | ||
cpp.Sleep(unsafe { _DWORD(ms) }) | ||
break | ||
} else { | ||
cpp.Sleep(unsafe { _DWORD(limit) }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
// A nanosecond. | ||
const _Nanosecond = 1 | ||
|
||
// Nanoseconds in microsecond. | ||
// How many nanoseconds are in microsecond. | ||
const _Microsecond = 1000 * _Nanosecond | ||
|
||
// Nanoseconds in millisecond. | ||
// How many nanoseconds are in millisecond. | ||
const _Millisecond = 1000 * _Microsecond | ||
|
||
// Nanoseconds in second. | ||
// How many nanoseconds are in second. | ||
const _Second = 1000 * _Millisecond | ||
|
||
// Nanoseconds in minute. | ||
// How many nanoseconds are in minute. | ||
const _Minute = 60 * _Second | ||
|
||
// Nanoseconds in hour. | ||
// How many nanoseconds are in hour. | ||
const _Hour = 60 * _Minute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
cpp use "<time.h>" | ||
|
||
cpp struct timespec { | ||
tv_sec: i64 | ||
tv_nsec: i64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.