2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bli
|
2011-02-27 20:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-01-19 14:32:28 +01:00
|
|
|
#include "BLI_time.h"
|
2011-02-14 17:55:27 +00:00
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2015-02-08 12:41:45 -05:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2002-10-12 11:37:38 +00:00
|
|
|
# include <windows.h>
|
|
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
double BLI_time_now_seconds(void)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2012-07-01 09:54:44 +00:00
|
|
|
static int hasperfcounter = -1; /* (-1 == unknown) */
|
2002-10-12 11:37:38 +00:00
|
|
|
static double perffreq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (hasperfcounter == -1) {
|
2002-10-12 11:37:38 +00:00
|
|
|
__int64 ifreq;
|
2012-05-12 15:13:06 +00:00
|
|
|
hasperfcounter = QueryPerformanceFrequency((LARGE_INTEGER *)&ifreq);
|
|
|
|
|
perffreq = (double)ifreq;
|
2012-10-21 05:46:41 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (hasperfcounter) {
|
|
|
|
|
__int64 count;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
QueryPerformanceCounter((LARGE_INTEGER *)&count);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
return count / perffreq;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-05-12 15:13:06 +00:00
|
|
|
static double accum = 0.0;
|
|
|
|
|
static int ltick = 0;
|
|
|
|
|
int ntick = GetTickCount();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (ntick < ltick) {
|
|
|
|
|
accum += (0xFFFFFFFF - ltick + ntick) / 1000.0;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-05-12 15:13:06 +00:00
|
|
|
accum += (ntick - ltick) / 1000.0;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
ltick = ntick;
|
2002-10-12 11:37:38 +00:00
|
|
|
return accum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
long int BLI_time_now_seconds_i(void)
|
2015-02-01 01:57:45 +11:00
|
|
|
{
|
2024-02-15 13:15:56 +11:00
|
|
|
return (long int)BLI_time_now_seconds();
|
2015-02-01 01:57:45 +11:00
|
|
|
}
|
|
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
void BLI_time_sleep_ms(int ms)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
Sleep(ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
# include <sys/time.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
# include <unistd.h>
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
double BLI_time_now_seconds(void)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
return ((double)tv.tv_sec + tv.tv_usec / 1000000.0);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
long int BLI_time_now_seconds_i(void)
|
2015-02-01 01:57:45 +11:00
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
|
|
return tv.tv_sec;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 13:15:56 +11:00
|
|
|
void BLI_time_sleep_ms(int ms)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
if (ms >= 1000) {
|
|
|
|
|
sleep(ms / 1000);
|
|
|
|
|
ms = (ms % 1000);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
usleep(ms * 1000);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|