From b7440c1e4c182ad977996d91bfb8125091ffae92 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 12 Nov 2026 23:28:58 -0800 Subject: ch31 early push needs review --- chapter31/src/main/smp/sys/time/DateTime.smp | 11 ++++++ chapter31/src/main/smp/sys/time/Timers.smp | 55 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 chapter31/src/main/smp/sys/time/DateTime.smp create mode 100644 chapter31/src/main/smp/sys/time/Timers.smp (limited to 'chapter31/src/main/smp/sys/time') diff --git a/chapter31/src/main/smp/sys/time/DateTime.smp b/chapter31/src/main/smp/sys/time/DateTime.smp new file mode 100644 index 00000000..5d117467 --- /dev/null +++ b/chapter31/src/main/smp/sys/time/DateTime.smp @@ -0,0 +1,11 @@ +// Calendar view of an instant. Month/day start at 1; weekday is Sunday=0. +// year==0 means out of the supported calendar range (years 1 through 9999). +struct DateTime { + int !year=0, !month=0, !day=0; + int !hour=0, !minute=0, !second=0, !millisecond=0; + int !weekday=0, !offsetSeconds=0; + bool !daylight=false; +}; + +// Factory in the defining unit, so callers need no forward instance type. +val empty = { -> new DateTime(); }; diff --git a/chapter31/src/main/smp/sys/time/Timers.smp b/chapter31/src/main/smp/sys/time/Timers.smp new file mode 100644 index 00000000..b98c96ef --- /dev/null +++ b/chapter31/src/main/smp/sys/time/Timers.smp @@ -0,0 +1,55 @@ +// Cooperative timers. poll returns IDs; callbacks never run on another thread. +struct Timers { + i64[] !_ids=new i64[8], !_due=new i64[8], !_period=new i64[8]; + i64 !_next=1; + + val _add = { i64 current, i64 delay, i64 period -> + if( current<0 || delay<0 || current>time.LIMIT-delay || _next==time.LIMIT ) return -1; + int slot=0; + while( slot<_ids# && _ids[slot]!=0 ) slot++; + if( slot==_ids# ) { + if( _ids#>1073741823 ) return -1; + val ids=new i64[_ids#*2], due=new i64[_ids#*2], periods=new i64[_ids#*2]; + for( int i=0; i<_ids#; i++ ) { ids[i]=_ids[i]; due[i]=_due[i]; periods[i]=_period[i]; } + _ids=ids; _due=due; _period=periods; + // Retry against the newly published arrays after growing. + return _add(current,delay,period); + } + i64 id=_next++; + _ids[slot]=id; _due[slot]=current+delay; _period[slot]=period; + return id; + }; + // Explicit-clock versions make game logic and tests deterministic. + val afterAt = { i64 current, i64 delay -> _add(current,delay,0); }; + val everyAt = { i64 current, i64 period -> period<=0 ? -1 : _add(current,period,period); }; + val after = { i64 delay -> afterAt(time.monotonic(),delay); }; + val every = { i64 period -> everyAt(time.monotonic(),period); }; + val cancel = { i64 id -> + if( id<=0 ) return false; + for( int i=0; i<_ids#; i++ ) if( _ids[i]==id ) { _ids[i]=0; return true; } + return false; + }; + val nextDeadline = { -> + i64 next=-1; + for( int i=0; i<_ids#; i++ ) + if( _ids[i]!=0 && (next<0 || _due[i] + int slot=-1; + for( int i=0; i<_ids#; i++ ) + if( _ids[i]!=0 && _due[i]<=current && (slot<0 || _due[i]<_due[slot] || + (_due[i]==_due[slot] && _ids[i]<_ids[slot])) ) slot=i; + if( slot<0 ) return -1; + i64 id=_ids[slot], period=_period[slot]; + if( period==0 ) _ids[slot]=0; + else { + i64 steps=(current-_due[slot])/period; + if( steps>=(time.LIMIT-_due[slot])/period ) _ids[slot]=0; + else _due[slot]+=(steps+1)*period; + } + return id; + }; +}; -- cgit v1.2.3