Skip to content

Commit

Permalink
Mark Pizzolato changes:
Browse files Browse the repository at this point in the history
Always perform time comparisons in nanoseconds and comparing rounded
values

Thus we can avoid any reference to ftime64() which only has millisecond
resolution and rounds or truncates differently on some C runtime
libraries.
  • Loading branch information
rocso committed Aug 8, 2018
1 parent 9439748 commit 8c1d612
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 106 deletions.
14 changes: 12 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

* Makefile: "nmake realclean VC VC-static" and similar wasn't remaking pthread.obj.
* common.mk: changes to accommodate the above.
* config.h (NEED_FTIME): defined for all which uses GetSystemTimeAsFileTime()
* _ptw32.h (NEED_FTIME): Likewise.
* GNUmakefile: use changes in common.mk but still has problem with
"make realclean GC GC-static" and similar.
* configure.ac (AC_INIT): package name change.

2018-08-08 Mark Pizzolato <markpizzolato dash pthreads dash win32 at subscriptions dot pizzolato dot net>

* config.h (NEED_FTIME): Removed
* _ptw32.h (NEED_FTIME): Removed.
* ptw32_timespec.c (NEED_FTIME): Removed conditional.
* ptw32_relmillisecs: Fix long-standing bug in NEED_FTIME code; remove NEED_FTIME
and all !NEED_FTIME code; compare nanoseconds and convert to milliseconds
at the end.
* implement.h (NEED_FTIME): remove conditionals.
* pthread.h: Remove Borland compiler time types no longer needed.
* configure.ac (NEED_FTIME): Removed check.

2018-08-07 Ross Johnson <ross dot johnson at homemail dot com dot au>

Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ install-headers: pthread.h sched.h semaphore.h _ptw32.h
%.o: %.rc
$(RC) $(RC_TARGET) $(RCFLAGS) $(CLEANUP) -o $@ -i $<

.SUFFIXES: .dll .rc .c .o ._o
.SUFFIXES: .dll .rc .c .o

.c.o:
$(CC) -c -o $@ $(CFLAGS) $(XC_FLAGS) $<
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ realclean: clean
if exist *.lib del *.lib
if exist *.a del *.a
if exist *.manifest del *.manifest
if exist *_stamp del *_stamp
if exist make.log.txt del make.log.txt
cd tests && $(MAKE) realclean

Expand All @@ -210,6 +209,7 @@ clean:
if exist *.o del *.o
if exist *.i del *.i
if exist *.res del *.res
if exist *_stamp del *_stamp
cd tests && $(MAKE) clean

# Very basic install. It assumes "realclean" was done just prior to build target.
Expand Down
7 changes: 0 additions & 7 deletions _ptw32.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,12 @@

#if !defined (__PTW32_CONFIG_H) && !defined(__PTW32_PSEUDO_CONFIG_H_SOURCED)
# define __PTW32_PSEUDO_CONFIG_H_SOURCED
/*
* We define NEED_FTIME for all now. GetSystemTimeAsFileTime() is pretty
* much universal now and consistently higher resolution than _ftime64().
*/
# define NEED_FTIME

# if defined(WINCE)
# undef HAVE_CPU_AFFINITY
# define NEED_DUPLICATEHANDLE
# define NEED_CREATETHREAD
# define NEED_ERRNO
# define NEED_CALLOC
# define NEED_FTIME
# define NEED_UNICODE_CONSTS
# define NEED_PROCESS_AFFINITY_MASK
/* This may not be needed */
Expand Down
6 changes: 0 additions & 6 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
/* Define if you don't have Win32 calloc. (eg. WinCE) */
#undef NEED_CALLOC

/*
* Define this now that all systems since Server 2000 support
* GetSystemTimeAsFileTime() (and WINCE before that).
*/
#define NEED_FTIME

/* Define if you don't have Win32 semaphores. (eg. WinCE 2.1 or earlier) */
#undef NEED_SEM

Expand Down
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ PTW32_AC_CHECK_TYPEDEF([struct timespec],[time.h])
#
PTW32_AC_NEED_ERRNO
PTW32_AC_NEED_FUNC([NEED_CALLOC],[calloc])
PTW32_AC_NEED_FUNC([NEED_FTIME],[ftime])
PTW32_AC_NEED_FUNC([NEED_CREATETHREAD],[_beginthreadex])
PTW32_AC_CHECK_CPU_AFFINITY

Expand Down
3 changes: 1 addition & 2 deletions implement.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,9 @@ __PTW32_BEGIN_C_DECLS

void __ptw32_mcs_node_transfer (__ptw32_mcs_local_node_t * new_node, __ptw32_mcs_local_node_t * old_node);

#if defined(NEED_FTIME)
void __ptw32_timespec_to_filetime (const struct timespec *ts, FILETIME * ft);

void __ptw32_filetime_to_timespec (const FILETIME * ft, struct timespec *ts);
#endif

/* Declared in pthw32_calloc.c */
#if defined(NEED_CALLOC)
Expand Down
8 changes: 0 additions & 8 deletions pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -1134,14 +1134,6 @@ typedef void (*__ptw32_terminate_handler)();
__PTW32_DLLPORT __ptw32_terminate_handler __PTW32_CDECL pthread_win32_set_terminate_np(__ptw32_terminate_handler termFunction);
#endif

/*
* Some compiler environments don't define some things.
*/
#if defined(__BORLANDC__)
# define _ftime ftime
# define _timeb timeb
#endif

#if defined(__cplusplus)

/*
Expand Down
89 changes: 15 additions & 74 deletions ptw32_relmillisecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@

#include "pthread.h"
#include "implement.h"
#if !defined(NEED_FTIME)
#include <sys/timeb.h>
#endif

static const int64_t NANOSEC_PER_SEC = 1000000000;
static const int64_t NANOSEC_PER_MILLISEC = 1000000;
Expand All @@ -53,23 +50,15 @@ DWORD
__ptw32_relmillisecs (const struct timespec * abstime)
{
DWORD milliseconds;
int64_t tmpAbsMilliseconds;
int64_t tmpAbsNanoseconds;
int64_t tmpCurrMilliseconds;
int64_t tmpCurrNanoseconds;

#if defined(NEED_FTIME)
struct timespec currSysTime;
FILETIME ft;
#else /* ! NEED_FTIME */
#if ( defined(_MSC_VER) && _MSC_VER >= 1300 ) /* MSVC7+ */ || \
( defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x0601 )
struct __timeb64 currSysTime;
#else
struct _timeb currSysTime;
#endif
#endif /* NEED_FTIME */

# if defined(WINCE)
SYSTEMTIME st;
#endif

/*
* Calculate timeout as milliseconds from current system time.
Expand All @@ -82,17 +71,11 @@ __ptw32_relmillisecs (const struct timespec * abstime)
*
* Assume all integers are unsigned, i.e. cannot test if less than 0.
*/
tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
tmpAbsNanoseconds = (int64_t)abstime->tv_nsec + ((int64_t)abstime->tv_sec * NANOSEC_PER_SEC);

/* get current system time */

#if defined(NEED_FTIME)

# if defined(WINCE)

SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
# else
Expand All @@ -101,36 +84,21 @@ __ptw32_relmillisecs (const struct timespec * abstime)

__ptw32_filetime_to_timespec(&ft, &currSysTime);

tmpCurrMilliseconds = (int64_t)currSysTime.tv_sec * MILLISEC_PER_SEC;
tmpCurrMilliseconds += ((int64_t)currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2))
/ NANOSEC_PER_MILLISEC;
tmpCurrNanoseconds = (int64_t)currSysTime.tv_nsec + ((int64_t)currSysTime.tv_sec * NANOSEC_PER_SEC);

#else /* ! NEED_FTIME */

#if defined(_MSC_VER) && _MSC_VER >= 1400 /* MSVC8+ */
_ftime64_s(&currSysTime);
#elif ( defined(_MSC_VER) && _MSC_VER >= 1300 ) /* MSVC7+ */ || \
( defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x0601 )
_ftime64(&currSysTime);
#else
_ftime(&currSysTime);
#endif

tmpCurrMilliseconds = (int64_t) currSysTime.time * MILLISEC_PER_SEC;
tmpCurrMilliseconds += (int64_t) currSysTime.millitm;
tmpCurrNanoseconds = tmpCurrMilliseconds * NANOSEC_PER_MILLISEC;

#endif /* NEED_FTIME */

if (tmpAbsMilliseconds > tmpCurrMilliseconds)
if (tmpAbsNanoseconds > tmpCurrNanoseconds)
{
milliseconds = (DWORD) (tmpAbsMilliseconds - tmpCurrMilliseconds);
if (milliseconds == INFINITE)
{
/* Timeouts must be finite */
milliseconds--;
}
int64_t deltaNanoseconds = tmpAbsNanoseconds - tmpCurrNanoseconds;

if (deltaNanoseconds >= ((int64_t)INFINITE * NANOSEC_PER_MILLISEC))
{
/* Timeouts must be finite */
milliseconds = INFINITE - 1;
}
else
{
milliseconds = (DWORD)(deltaNanoseconds / NANOSEC_PER_MILLISEC);
}
}
else
{
Expand Down Expand Up @@ -161,22 +129,11 @@ pthread_win32_getabstime_np (struct timespec * abstime, const struct timespec *
int64_t sec;
int64_t nsec;

#if defined(NEED_FTIME)
struct timespec currSysTime;
FILETIME ft;
#else /* ! NEED_FTIME */
#if ( defined(_MSC_VER) && _MSC_VER >= 1300 ) /* MSVC7+ */ || \
( defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x0601 )
struct __timeb64 currSysTime;
#else
struct _timeb currSysTime;
#endif
#endif /* NEED_FTIME */

/* get current system time */

#if defined(NEED_FTIME)

# if defined(WINCE)

SYSTEMTIME st;
Expand All @@ -191,22 +148,6 @@ pthread_win32_getabstime_np (struct timespec * abstime, const struct timespec *
sec = currSysTime.tv_sec;
nsec = currSysTime.tv_nsec;

#else /* ! NEED_FTIME */

#if defined(_MSC_VER) && _MSC_VER >= 1400 /* MSVC8+ */
_ftime64_s(&currSysTime);
#elif ( defined(_MSC_VER) && _MSC_VER >= 1300 ) /* MSVC7+ */ || \
( defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x0601 )
_ftime64(&currSysTime);
#else
_ftime(&currSysTime);
#endif

sec = currSysTime.time;
nsec = currSysTime.millitm * NANOSEC_PER_MILLISEC;

#endif /* NEED_FTIME */

if (NULL != relative)
{
nsec += relative->tv_nsec;
Expand Down
4 changes: 0 additions & 4 deletions ptw32_timespec.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
# include <config.h>
#endif

#if defined(NEED_FTIME)

#include "pthread.h"
#include "implement.h"

Expand Down Expand Up @@ -80,5 +78,3 @@ __ptw32_filetime_to_timespec (const FILETIME * ft, struct timespec *ts)
(int) ((*(uint64_t *) ft - __PTW32_TIMESPEC_TO_FILETIME_OFFSET -
((uint64_t) ts->tv_sec * (uint64_t) 10000000UL)) * 100);
}

#endif

0 comments on commit 8c1d612

Please sign in to comment.