1
0
mirror of https://github.com/anope/anope.git synced 2026-06-27 03:56:37 +02:00

Use clock_gettime if it is available.

This commit is contained in:
Sadie Powell
2024-03-18 11:13:46 +00:00
parent 753119c4a1
commit cb3848b7db
13 changed files with 42 additions and 31 deletions
+1
View File
@@ -224,6 +224,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINF
endif()
# Check for the existence of the following functions
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
check_function_exists(umask HAVE_UMASK)
check_function_exists(epoll_wait HAVE_EPOLL)
check_function_exists(poll HAVE_POLL)
+4
View File
@@ -367,6 +367,7 @@ namespace Anope
* Use this unless you need very specific time checks
*/
extern CoreExport time_t CurTime;
extern CoreExport long long CurTimeNs;
/** The debug level we are running at.
*/
@@ -575,6 +576,9 @@ namespace Anope
* @param s2 The second string.
*/
extern CoreExport size_t Distance(const Anope::string &s1, const Anope::string &s2);
/** Update the current time. */
extern CoreExport void UpdateTime();
}
/** sepstream allows for splitting token separated lists.
+3
View File
@@ -20,6 +20,9 @@
// Whether Anope was built in debug mode.
#cmakedefine01 DEBUG_BUILD
// Whether the clock_gettime() function is available.
#cmakedefine01 HAVE_CLOCK_GETTIME
// Whether Anope was built with localization support.
#cmakedefine01 HAVE_LOCALIZATION
+1
View File
@@ -290,6 +290,7 @@ bool Anope::Init(int ac, char **av)
umask(DEFUMASK);
#endif
Anope::UpdateTime();
Serialize::RegisterTypes();
/* Parse command line arguments */
+3 -8
View File
@@ -29,20 +29,15 @@
static Anope::string GetTimeStamp()
{
char tbuf[256];
time_t t;
if (time(&t) < 0)
t = Anope::CurTime;
tm tm = *localtime(&t);
Anope::UpdateTime();
auto tm = *localtime(&Anope::CurTime);
if (Anope::Debug)
{
char *s;
struct timeval tv;
gettimeofday(&tv, NULL);
strftime(tbuf, sizeof(tbuf) - 1, "[%b %d %H:%M:%S", &tm);
s = tbuf + strlen(tbuf);
s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06d", static_cast<int>(tv.tv_usec));
s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06lld", static_cast<long long>(Anope::CurTimeNs / 1000));
strftime(s, sizeof(tbuf) - (s - tbuf) - 1, " %Y]", &tm);
}
else
+3 -2
View File
@@ -36,8 +36,9 @@ Anope::string Anope::QuitReason;
static Anope::string BinaryDir; /* Full path to services bin directory */
time_t Anope::StartTime = time(NULL);
time_t Anope::CurTime = time(NULL);
time_t Anope::StartTime = 0;
time_t Anope::CurTime = 0;
long long Anope::CurTimeNs = 0;
size_t Anope::CurrentUplink = -1;
+23
View File
@@ -785,3 +785,26 @@ size_t Anope::Distance(const Anope::string &s1, const Anope::string &s2)
}
return costs[s2.length()];
}
void Anope::UpdateTime()
{
#ifdef _WIN32
SYSTEMTIME st;
GetSystemTime(&st);
CurTime = time(nullptr);
CurTimeNs = st.wMilliseconds;
#elif HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
CurTime = ts.tv_sec;
CurTimeNs = ts.tv_nsec;
#else
struct timeval tv;
gettimeofday(&tv, nullptr);
CurTime = tv.tv_sec;
CurTimeNs = tv.tv_usec * 1000;
#endif
}
+1 -1
View File
@@ -76,7 +76,7 @@ void SocketEngine::Process()
events.resize(events.size() * 2);
int total = epoll_wait(EngineHandle, &events.front(), events.size(), Config->ReadTimeout * 1000);
Anope::CurTime = time(NULL);
Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total == -1)
+1 -1
View File
@@ -77,7 +77,7 @@ void SocketEngine::Process()
static timespec kq_timespec = { Config->ReadTimeout, 0 };
int total = kevent(kq_fd, &change_events.front(), change_count, &event_events.front(), event_events.size(), &kq_timespec);
change_count = 0;
Anope::CurTime = time(NULL);
Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total == -1)
+1 -1
View File
@@ -100,7 +100,7 @@ void SocketEngine::Change(Socket *s, bool set, SocketFlag flag)
void SocketEngine::Process()
{
int total = poll(&events.front(), events.size(), Config->ReadTimeout * 1000);
Anope::CurTime = time(NULL);
Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total < 0)
+1 -1
View File
@@ -100,7 +100,7 @@ void SocketEngine::Process()
#endif
int sresult = select(MaxFD + 1, &rfdset, &wfdset, &efdset, &tval);
Anope::CurTime = time(NULL);
Anope::UpdateTime();
if (sresult == -1)
{
-1
View File
@@ -65,7 +65,6 @@ namespace Anope
extern CoreExport void OnStartup();
extern CoreExport void OnShutdown();
extern CoreExport USHORT WindowsGetLanguage(const Anope::string &lang);
extern CoreExport int gettimeofday(timeval *tv, void *);
extern int setenv(const char *name, const char *value, int overwrite);
extern int unsetenv(const char *name);
extern int mkstemp(char *input);
-16
View File
@@ -64,22 +64,6 @@ USHORT WindowsGetLanguage(const Anope::string &lang)
return LANG_NEUTRAL;
}
/** Like gettimeofday(), but it works on Windows.
* @param tv A timeval struct
* @param tz Should be NULL, it is not used
* @return 0 on success
*/
int gettimeofday(timeval *tv, void *)
{
SYSTEMTIME st;
GetSystemTime(&st);
tv->tv_sec = Anope::CurTime;
tv->tv_usec = st.wMilliseconds;
return 0;
}
int setenv(const char *name, const char *value, int overwrite)
{
return SetEnvironmentVariable(name, value);