mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
some fix
This commit is contained in:
parent
9482909ee7
commit
56996cc2e7
@ -2,6 +2,8 @@
|
|||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
int64_t time_ns(); // from random.c
|
||||||
|
|
||||||
/* https://github.com/clibs/mt19937ar
|
/* https://github.com/clibs/mt19937ar
|
||||||
|
|
||||||
Copyright (c) 2011 Mutsuo Saito, Makoto Matsumoto, Hiroshima
|
Copyright (c) 2011 Mutsuo Saito, Makoto Matsumoto, Hiroshima
|
||||||
@ -76,7 +78,8 @@ static uint32_t mt19937__next_uint32(mt19937* self) {
|
|||||||
int kk;
|
int kk;
|
||||||
|
|
||||||
if(self->mti == N + 1) { /* if init_genrand() has not been called, */
|
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 */
|
// seed(5489UL); /* a default initial seed is used */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#define NANOS_PER_SEC 1000000000
|
#define NANOS_PER_SEC 1000000000
|
||||||
|
|
||||||
static bool get_ns(int64_t* out) {
|
int64_t time_ns() {
|
||||||
struct timespec tms;
|
struct timespec tms;
|
||||||
#if defined( __ANDROID__) || defined(__MINGW32__) || defined(__MINGW64__)
|
#if defined( __ANDROID__) || defined(__MINGW32__) || defined(__MINGW64__)
|
||||||
clock_gettime(CLOCK_REALTIME, &tms);
|
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;
|
int64_t nanos = tms.tv_sec * NANOS_PER_SEC;
|
||||||
/* Add full nanoseconds */
|
/* Add full nanoseconds */
|
||||||
nanos += tms.tv_nsec;
|
nanos += tms.tv_nsec;
|
||||||
*out = nanos;
|
return nanos;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool time_time(int argc, py_Ref argv) {
|
static bool time_time(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(0);
|
PY_CHECK_ARGC(0);
|
||||||
int64_t nanos;
|
int64_t nanos = time_ns();
|
||||||
if(!get_ns(&nanos)) return false;
|
|
||||||
py_newfloat(py_retval(), (double)nanos / NANOS_PER_SEC);
|
py_newfloat(py_retval(), (double)nanos / NANOS_PER_SEC);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool time_time_ns(int argc, py_Ref argv) {
|
static bool time_time_ns(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(0);
|
PY_CHECK_ARGC(0);
|
||||||
int64_t nanos;
|
int64_t nanos = time_ns();
|
||||||
if(!get_ns(&nanos)) return false;
|
|
||||||
py_newint(py_retval(), nanos);
|
py_newint(py_retval(), nanos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -41,12 +38,10 @@ static bool time_sleep(int argc, py_Ref argv) {
|
|||||||
py_f64 secs;
|
py_f64 secs;
|
||||||
if(!py_castfloat(argv, &secs)) return false;
|
if(!py_castfloat(argv, &secs)) return false;
|
||||||
|
|
||||||
int64_t start;
|
int64_t start = time_ns();
|
||||||
if(!get_ns(&start)) return false;
|
|
||||||
const int64_t end = start + secs * 1000000000;
|
const int64_t end = start + secs * 1000000000;
|
||||||
while(true) {
|
while(true) {
|
||||||
int64_t now;
|
int64_t now = time_ns();
|
||||||
if(!get_ns(&now)) return false;
|
|
||||||
if(now >= end) break;
|
if(now >= end) break;
|
||||||
}
|
}
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user