Skip to content

Commit

Permalink
win32/pthread: implement clock_gettime for high-res timer purposes
Browse files Browse the repository at this point in the history
Also apply some fixes to pthread_cond_timedwait while we're at it.

Note that by using GetSystemTimePreciseAsFileTime here we lose support
for Windows 7. This is considered acceptable.
  • Loading branch information
sfan5 committed Oct 20, 2023
1 parent 9f14749 commit 66c3110
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
8 changes: 7 additions & 1 deletion osdep/win32/include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <sys/types.h>

#define _POSIX_TIMERS 0
#define _POSIX_TIMERS 200809L

// Note: all pthread functions are mangled to make static linking easier.
#define pthread_once m_pthread_once
Expand All @@ -35,6 +35,7 @@
#define pthread_detach m_pthread_detach
#define pthread_create m_pthread_create
#define pthread_set_name_np m_pthread_set_name_np
#define clock_gettime m_clock_gettime

#define pthread_once_t INIT_ONCE
#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
Expand Down Expand Up @@ -76,6 +77,11 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex);
#define pthread_cond_broadcast(cond) WakeAllConditionVariable(cond)
#define pthread_cond_signal(cond) WakeConditionVariable(cond)

#define clockid_t int
#define CLOCK_REALTIME 1

int clock_gettime(clockid_t clockid, struct timespec *tp);

int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
Expand Down
32 changes: 24 additions & 8 deletions osdep/win32/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
return 0;
}

int clock_gettime(clockid_t clockid, struct timespec *tp)
{
if (clockid != CLOCK_REALTIME) {
errno = EINVAL;
return -1;
}
union {
FILETIME ft;
ULARGE_INTEGER i;
} r;
GetSystemTimePreciseAsFileTime(&r.ft);
r.i.QuadPart -= UINT64_C(116444736000000000); // MS epoch -> Unix epoch
tp->tv_sec = r.i.QuadPart / UINT64_C(10000000);
tp->tv_nsec = (r.i.QuadPart % UINT64_C(10000000)) * 100;
return 0;
}

static int cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
DWORD ms)
Expand All @@ -95,16 +112,15 @@ int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime)
{
// mpv uses mingw's gettimeofday() as time source too.
struct timeval tv;
gettimeofday(&tv, NULL);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
DWORD timeout_ms = 0;
if (abstime->tv_sec >= INT64_MAX / 10000) {
if (abstime->tv_sec >= INT64_MAX / 1000) { // overflow
timeout_ms = INFINITE;
} else if (abstime->tv_sec >= tv.tv_sec) {
long long msec = (abstime->tv_sec - tv.tv_sec) * 1000LL +
abstime->tv_nsec / 1000LL / 1000LL - tv.tv_usec / 1000LL;
if (msec > INT_MAX) {
} else if (abstime->tv_sec >= ts.tv_sec) {
int64_t msec = (abstime->tv_sec - ts.tv_sec) * INT64_C(1000) +
(abstime->tv_nsec - ts.tv_nsec) / INT64_C(10000000);
if (msec > ULONG_MAX) {
timeout_ms = INFINITE;
} else if (msec > 0) {
timeout_ms = msec;
Expand Down

7 comments on commit 66c3110

@Theo1996
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just checking the OS and if it is win7 jump to the old code, instead of just dropping support completely.

@Hrxn
Copy link
Contributor

@Hrxn Hrxn commented on 66c3110 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about no?
Not even MSFT themselves supports Windows 7 anymore.

@Dudemanguy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of this code even exists anymore.

@Theo1996
Copy link

@Theo1996 Theo1996 commented on 66c3110 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saying.... instead of dropping support for an OS that was supported up until recently maybe disable the new features that cant run on this OS

Not even MSFT themselves supports Windows 7 anymore.

Why should it matter? Micro$haft is a shit company.

None of this code even exists anymore.

Maybe but it was were support was dropped.

@Hrxn
Copy link
Contributor

@Hrxn Hrxn commented on 66c3110 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should it matter? Micro$haft is a shit company.

Really? Seriously?

You're asking a free software/open source software project to spend effort/provide labor.
While asking transnational multi-billion dollar MegaCorp the same does not even cross your mind?

@Theo1996
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt ask for labor, I was asking if it can be done. Microsoft doesnt care nor does it want to listen to what users say. Here I can at least ask "if" it can be done, am I supposed to ask microsoft to update win7 to support the newest mpv versions?
More specifically what I meant is disabling the new features when needed and reenabling them where they are supported, not removing them completely to support an old OS, I dont know if it was conveyed properly.

@libreom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider a backwards compatible approach, i.e, it checks if the os supports it, Windows 7 still has extended security updates going on, latest chromium 121 is supported, firefox esr is supported

Please sign in to comment.