This commit is contained in:
blueloveTH 2024-11-02 23:28:11 +08:00
parent 9482909ee7
commit 56996cc2e7
2 changed files with 10 additions and 12 deletions

View File

@ -2,6 +2,8 @@
#include "pocketpy/pocketpy.h"
#include <time.h>
int64_t time_ns(); // from random.c
/* https://github.com/clibs/mt19937ar
Copyright (c) 2011 Mutsuo Saito, Makoto Matsumoto, Hiroshima
@ -76,7 +78,8 @@ static uint32_t mt19937__next_uint32(mt19937* self) {
int kk;
if(self->mti == N + 1) { /* if init_genrand() has not been called, */
mt19937__seed(self, clock());
int64_t seed = time_ns();
mt19937__seed(self, (uint32_t)seed);
// seed(5489UL); /* a default initial seed is used */
}

View File

@ -4,7 +4,7 @@
#define NANOS_PER_SEC 1000000000
static bool get_ns(int64_t* out) {
int64_t time_ns() {
struct timespec tms;
#if defined( __ANDROID__) || defined(__MINGW32__) || defined(__MINGW64__)
clock_gettime(CLOCK_REALTIME, &tms);
@ -16,22 +16,19 @@ static bool get_ns(int64_t* out) {
int64_t nanos = tms.tv_sec * NANOS_PER_SEC;
/* Add full nanoseconds */
nanos += tms.tv_nsec;
*out = nanos;
return true;
return nanos;
}
static bool time_time(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int64_t nanos;
if(!get_ns(&nanos)) return false;
int64_t nanos = time_ns();
py_newfloat(py_retval(), (double)nanos / NANOS_PER_SEC);
return true;
}
static bool time_time_ns(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int64_t nanos;
if(!get_ns(&nanos)) return false;
int64_t nanos = time_ns();
py_newint(py_retval(), nanos);
return true;
}
@ -41,12 +38,10 @@ static bool time_sleep(int argc, py_Ref argv) {
py_f64 secs;
if(!py_castfloat(argv, &secs)) return false;
int64_t start;
if(!get_ns(&start)) return false;
int64_t start = time_ns();
const int64_t end = start + secs * 1000000000;
while(true) {
int64_t now;
if(!get_ns(&now)) return false;
int64_t now = time_ns();
if(now >= end) break;
}
py_newnone(py_retval());