forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
268 additions
and
126 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
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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
#include <time.h> | ||
|
||
#include "logger.h" | ||
#include "mono_time.h" | ||
#include "network.h" | ||
#include "util.h" | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "mono_time.h" | ||
#include "util.h" | ||
|
||
|
||
|
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#ifndef _XOPEN_SOURCE | ||
#define _XOPEN_SOURCE 600 | ||
#endif | ||
|
||
#if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) | ||
#define OS_WIN32 | ||
#endif | ||
|
||
#ifdef __APPLE__ | ||
#include <mach/clock.h> | ||
#endif | ||
|
||
#if !defined(OS_WIN32) && !defined(__APPLE__) | ||
#include <sys/time.h> | ||
#include <time.h> | ||
#endif | ||
|
||
#include "mono_time.h" | ||
|
||
#include <stdlib.h> | ||
|
||
#include "ccompat.h" | ||
|
||
/* don't call into system billions of times for no reason */ | ||
struct Mono_Time { | ||
uint64_t time; | ||
uint64_t base_time; | ||
}; | ||
|
||
Mono_Time *mono_time_new(void) | ||
{ | ||
Mono_Time *monotime = (Mono_Time *)malloc(sizeof(Mono_Time)); | ||
|
||
if (monotime == nullptr) { | ||
return nullptr; | ||
} | ||
|
||
monotime->time = 0; | ||
monotime->base_time = 0; | ||
|
||
return monotime; | ||
} | ||
|
||
void mono_time_free(Mono_Time *monotime) | ||
{ | ||
free(monotime); | ||
} | ||
|
||
void mono_time_update(Mono_Time *monotime) | ||
{ | ||
if (monotime->base_time == 0) { | ||
monotime->base_time = ((uint64_t)time(nullptr) - (current_time_monotonic() / 1000ULL)); | ||
} | ||
|
||
monotime->time = (current_time_monotonic() / 1000ULL) + monotime->base_time; | ||
} | ||
|
||
uint64_t mono_time_get(const Mono_Time *monotime) | ||
{ | ||
return monotime->time; | ||
} | ||
|
||
bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_t timeout) | ||
{ | ||
return timestamp + timeout <= mono_time_get(monotime); | ||
} | ||
|
||
|
||
static Mono_Time global_time; | ||
|
||
/* XXX: note that this is not thread-safe; if multiple threads call unix_time_update() concurrently, the return value of | ||
* unix_time() may fail to increase monotonically with increasing time */ | ||
void unix_time_update(void) | ||
{ | ||
mono_time_update(&global_time); | ||
} | ||
uint64_t unix_time(void) | ||
{ | ||
return mono_time_get(&global_time); | ||
} | ||
int is_timeout(uint64_t timestamp, uint64_t timeout) | ||
{ | ||
return mono_time_is_timeout(&global_time, timestamp, timeout); | ||
} | ||
|
||
|
||
|
||
/* return current UNIX time in microseconds (us). */ | ||
uint64_t current_time_actual(void) | ||
{ | ||
uint64_t time; | ||
#ifdef OS_WIN32 | ||
/* This probably works fine */ | ||
FILETIME ft; | ||
GetSystemTimeAsFileTime(&ft); | ||
time = ft.dwHighDateTime; | ||
time <<= 32; | ||
time |= ft.dwLowDateTime; | ||
time -= 116444736000000000ULL; | ||
return time / 10; | ||
#else | ||
struct timeval a; | ||
gettimeofday(&a, nullptr); | ||
time = 1000000ULL * a.tv_sec + a.tv_usec; | ||
return time; | ||
#endif | ||
} | ||
|
||
|
||
#ifdef OS_WIN32 | ||
static uint64_t last_monotime; | ||
static uint64_t add_monotime; | ||
#endif | ||
|
||
/* return current monotonic time in milliseconds (ms). */ | ||
uint64_t current_time_monotonic(void) | ||
{ | ||
uint64_t time; | ||
#ifdef OS_WIN32 | ||
uint64_t old_add_monotime = add_monotime; | ||
time = (uint64_t)GetTickCount() + add_monotime; | ||
|
||
/* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race | ||
* conditions when multiple threads call this function at once */ | ||
if (time + 0x10000 < last_monotime) { | ||
uint32_t add = ~0; | ||
/* use old_add_monotime rather than simply incrementing add_monotime, to handle the case that many threads | ||
* simultaneously detect an overflow */ | ||
add_monotime = old_add_monotime + add; | ||
time += add; | ||
} | ||
|
||
last_monotime = time; | ||
#else | ||
struct timespec monotime; | ||
#if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) | ||
clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); | ||
#elif defined(__APPLE__) | ||
clock_serv_t muhclock; | ||
mach_timespec_t machtime; | ||
|
||
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock); | ||
clock_get_time(muhclock, &machtime); | ||
mach_port_deallocate(mach_task_self(), muhclock); | ||
|
||
monotime.tv_sec = machtime.tv_sec; | ||
monotime.tv_nsec = machtime.tv_nsec; | ||
#else | ||
clock_gettime(CLOCK_MONOTONIC, &monotime); | ||
#endif | ||
time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); | ||
#endif | ||
return time; | ||
} |
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,35 @@ | ||
#ifndef C_TOXCORE_TOXCORE_MONO_TIME_H | ||
#define C_TOXCORE_TOXCORE_MONO_TIME_H | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct Mono_Time Mono_Time; | ||
|
||
Mono_Time *mono_time_new(void); | ||
void mono_time_free(Mono_Time *monotime); | ||
|
||
void mono_time_update(Mono_Time *monotime); | ||
uint64_t mono_time_get(const Mono_Time *monotime); | ||
bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_t timeout); | ||
|
||
// TODO(#980): Use per-tox monotime, delete these functions. | ||
void unix_time_update(void); | ||
uint64_t unix_time(void); | ||
int is_timeout(uint64_t timestamp, uint64_t timeout); | ||
|
||
/* return current UNIX time in microseconds (us). */ | ||
uint64_t current_time_actual(void); | ||
|
||
/* return current monotonic time in milliseconds (ms). */ | ||
uint64_t current_time_monotonic(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // C_TOXCORE_TOXCORE_MONO_TIME_H |
Oops, something went wrong.