Skip to content

Commit 77858c9

Browse files
Run the clock change test on the old sources
1 parent 0a5a0ce commit 77858c9

5 files changed

Lines changed: 59 additions & 100 deletions

File tree

‎.github/workflows/clock-old.yml‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: clock-old
2+
on:
3+
push:
4+
branches: [scratch/clock-old]
5+
jobs:
6+
clock:
7+
strategy:
8+
fail-fast: false
9+
matrix:
10+
os: [ubuntu-latest, macos-latest]
11+
runs-on: ${{ matrix.os }}
12+
name: clock change on the old sources (${{ matrix.os }})
13+
defaults:
14+
run:
15+
working-directory: test/clock_change
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: ./.github/workflows/setup
19+
with:
20+
haxe: 4.3.7
21+
- run: |
22+
if [ "$RUNNER_OS" = Linux ]; then
23+
sudo timedatectl set-ntp false
24+
else
25+
sudo systemsetup -setusingnetworktime off
26+
fi
27+
- run: haxe build.hxml -D ${{ runner.os == 'Linux' && 'HXCPP_M64' || 'HXCPP_ARM64' }}
28+
- run: bin/Main

‎include/hx/Thread.h‎

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
#include <errno.h>
3434
#include <pthread.h>
3535
#include <sys/time.h>
36-
#include <time.h>
3736
#include <stdio.h>
38-
#ifdef __APPLE__
39-
#include <mach/mach_time.h>
40-
#endif
4137
#define HXCPP_PTHREADS
4238
#endif
4339

@@ -219,12 +215,6 @@ struct HxSemaphore
219215

220216
#define HX_THREAD_SEMAPHORE_LOCKABLE
221217

222-
// Timed waits run on a clock that changing the system time does not move.
223-
// Apple has no condition clock, so it waits for the time left instead.
224-
#if !defined(__APPLE__) && (!defined(__ANDROID__) || __ANDROID_API__ >= 21)
225-
#define HX_THREAD_COND_MONOTONIC
226-
#endif
227-
228218
struct HxSemaphore
229219
{
230220
HxMutex mMutex;
@@ -236,15 +226,7 @@ struct HxSemaphore
236226
{
237227
mSet = false;
238228
mCondition = new pthread_cond_t();
239-
#ifdef HX_THREAD_COND_MONOTONIC
240-
pthread_condattr_t attr;
241-
pthread_condattr_init(&attr);
242-
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
243-
pthread_cond_init(mCondition,&attr);
244-
pthread_condattr_destroy(&attr);
245-
#else
246229
pthread_cond_init(mCondition,0);
247-
#endif
248230
}
249231
~HxSemaphore()
250232
{
@@ -286,63 +268,31 @@ struct HxSemaphore
286268
pthread_cond_wait( mCondition, mMutex.mMutex );
287269
mSet = false;
288270
}
289-
static void Now(timespec &outNow)
290-
{
291-
#ifdef __APPLE__
292-
static mach_timebase_info_data_t timebase;
293-
if (!timebase.denom)
294-
mach_timebase_info(&timebase);
295-
unsigned long long ns = mach_absolute_time() * timebase.numer / timebase.denom;
296-
outNow.tv_sec = ns / 1000000000;
297-
outNow.tv_nsec = ns % 1000000000;
298-
#elif defined(HX_THREAD_COND_MONOTONIC)
299-
clock_gettime(CLOCK_MONOTONIC, &outNow);
300-
#else
301-
clock_gettime(CLOCK_REALTIME, &outNow);
302-
#endif
303-
}
304-
int TimedWait(const timespec &inDeadline)
305-
{
306-
#ifdef __APPLE__
307-
timespec now;
308-
Now(now);
309-
timespec left;
310-
left.tv_sec = inDeadline.tv_sec - now.tv_sec;
311-
left.tv_nsec = inDeadline.tv_nsec - now.tv_nsec;
312-
if (left.tv_nsec<0)
313-
{
314-
left.tv_nsec += 1000000000;
315-
left.tv_sec--;
316-
}
317-
if (left.tv_sec<0)
318-
return ETIMEDOUT;
319-
return pthread_cond_timedwait_relative_np( mCondition, mMutex.mMutex, &left );
320-
#else
321-
return pthread_cond_timedwait( mCondition, mMutex.mMutex, &inDeadline );
322-
#endif
323-
}
324271
// Returns true if the wait was success, false on timeout.
325272
bool WaitSeconds(double inSeconds)
326273
{
274+
struct timeval tv;
275+
gettimeofday(&tv, 0);
276+
327277
// double->int is undefined past 2^31
328278
if (inSeconds > 0x7fffffff)
329279
inSeconds = 0x7fffffff;
330280
int isec = (int)inSeconds;
281+
int usec = (int)((inSeconds-isec)*1000000.0);
331282
timespec spec;
332-
Now(spec);
333-
spec.tv_nsec += (long)((inSeconds-isec)*1000000000.0);
283+
spec.tv_nsec = (tv.tv_usec + usec) * 1000;
334284
if (spec.tv_nsec>=1000000000)
335285
{
336286
spec.tv_nsec-=1000000000;
337287
isec++;
338288
}
339-
spec.tv_sec += isec;
289+
spec.tv_sec = tv.tv_sec + isec;
340290

341291
AutoLock lock(mMutex);
342292

343293
int result = 0;
344294
// Wait for set to be true...
345-
while( !mSet && (result=TimedWait(spec)) != ETIMEDOUT)
295+
while( !mSet && (result=pthread_cond_timedwait( mCondition, mMutex.mMutex, &spec )) != ETIMEDOUT)
346296
{
347297
if (result!=0)
348298
{

‎src/hx/StdLibs.cpp‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#elif defined(__unix__) || defined(__APPLE__)
1010
#include <sys/time.h>
1111
#include <unistd.h>
12-
#ifdef __APPLE__
13-
#include <mach/mach_time.h>
14-
#endif
1512
#ifndef __EMSCRIPTEN__
1613
typedef int64_t __int64;
1714
#endif
@@ -444,22 +441,12 @@ double __time_stamp()
444441
return (now-t0)*period;
445442
}
446443
return (double)clock() / ( (double)CLOCKS_PER_SEC);
447-
#elif defined(__APPLE__)
448-
static mach_timebase_info_data_t timebase;
449-
static unsigned long long t0 = 0;
450-
unsigned long long now = mach_absolute_time();
451-
if (t0==0)
452-
{
453-
mach_timebase_info(&timebase);
454-
t0 = now;
455-
}
456-
return (double)(now-t0) * timebase.numer / timebase.denom * 1e-9;
457-
#elif defined(__unix__)
444+
#elif defined(__unix__) || defined(__APPLE__)
458445
static double t0 = 0;
459-
struct timespec ts;
460-
if( clock_gettime(CLOCK_MONOTONIC,&ts) )
446+
struct timeval tv;
447+
if( gettimeofday(&tv,0) )
461448
throw Dynamic("Could not get time");
462-
double t = ( ts.tv_sec + ((double)ts.tv_nsec) / 1000000000.0 );
449+
double t = ( tv.tv_sec + ((double)tv.tv_usec) / 1000000.0 );
463450
if (t0==0) t0 = t;
464451
return t-t0;
465452
#else

‎src/hx/Thread.cpp‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,22 +375,28 @@ class hxLock : public hx::Object
375375

376376
hx::InternalFinalizer *mFinalizer;
377377

378-
#if defined(__SNC__)
378+
#if defined(HX_WINDOWS)
379+
double Now()
380+
{
381+
// Monotonic, 64-bit and fine-grained: haxe.Timer.stamp's clock, the
382+
// performance counter. clock() is a 32-bit millisecond count on
383+
// MSVC, which wraps negative after ~24.8 days of process uptime and
384+
// breaks every timed wait from then on. GetTickCount64 does not
385+
// wrap, but it moves only on the ~15.6ms system tick, so a deadline
386+
// taken from it came due up to a tick late: a 1ms wait took 15ms
387+
return __time_stamp();
388+
}
389+
#elif defined(__SNC__)
379390
double Now()
380391
{
381392
return (double)clock()/CLOCKS_PER_SEC;
382393
}
383394
#else
384395
double Now()
385396
{
386-
// Monotonic, 64-bit and fine-grained: haxe.Timer.stamp's clock.
387-
// clock() is a 32-bit millisecond count on MSVC, which wraps
388-
// negative after ~24.8 days of process uptime and breaks every
389-
// timed wait from then on. GetTickCount64 does not wrap, but it
390-
// moves only on the ~15.6ms system tick, so a deadline taken from
391-
// it came due up to a tick late: a 1ms wait took 15ms. The wall
392-
// clock moves when the system time is changed
393-
return __time_stamp();
397+
struct timeval tv;
398+
gettimeofday(&tv,0);
399+
return tv.tv_sec + tv.tv_usec*0.000001;
394400
}
395401
#endif
396402

‎src/hx/thread/CountingSemaphore.posix.cpp‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#include <hxcpp.h>
22
#include <semaphore.h>
3-
#include <time.h>
3+
#include <sys/time.h>
44
#include <memory>
55
#include <cerrno>
66
#include <hx/thread/CountingSemaphore.hpp>
77

8-
// sem_timedwait takes a deadline on the wall clock, which changing the system time moves
9-
#if (defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))) || (defined(__ANDROID__) && __ANDROID_API__ >= 30)
10-
#define HX_SEM_CLOCKWAIT
11-
#endif
12-
138
struct hx::thread::CountingSemaphore_obj::Impl
149
{
1510
sem_t semaphore;
@@ -76,31 +71,24 @@ bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
7671
{
7772
hx::EnterGCFreeZone();
7873

74+
struct timeval tv;
7975
struct timespec t;
8076
auto delta = timeout.value;
8177
int idelta = (int)delta, idelta2;
8278
delta -= idelta;
8379
delta *= 1.0e9;
84-
#ifdef HX_SEM_CLOCKWAIT
85-
clock_gettime(CLOCK_MONOTONIC, &t);
86-
#else
87-
clock_gettime(CLOCK_REALTIME, &t);
88-
#endif
89-
delta += t.tv_nsec;
80+
gettimeofday(&tv, NULL);
81+
delta += tv.tv_usec * 1000.0;
9082
idelta2 = (int)(delta / 1e9);
9183
delta -= idelta2 * 1e9;
92-
t.tv_sec += idelta + idelta2;
84+
t.tv_sec = tv.tv_sec + idelta + idelta2;
9385
t.tv_nsec = (long)delta;
9486

9587
// Retry on EINTR - the absolute deadline keeps the timeout intact
9688
int r;
9789
do
9890
{
99-
#ifdef HX_SEM_CLOCKWAIT
100-
r = sem_clockwait(&impl->semaphore, CLOCK_MONOTONIC, &t);
101-
#else
10291
r = sem_timedwait(&impl->semaphore, &t);
103-
#endif
10492
}
10593
while (0 != r && EINTR == errno);
10694

0 commit comments

Comments
 (0)