summaryrefslogtreecommitdiff
path: root/chapter31/src/main/smp/sys/time
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-13 13:10:15 -0800
committerCliff Click <cliffc@acm.org>2026-11-13 13:10:15 -0800
commit299addd03094389bb622e0fe1d7ad585b51e146d (patch)
treefde1f3d0d440665428786c8bc35bd9d037034e2c /chapter31/src/main/smp/sys/time
parentb7440c1e4c182ad977996d91bfb8125091ffae92 (diff)
downloadSimple-299addd03094389bb622e0fe1d7ad585b51e146d.tar.gz
Simple-299addd03094389bb622e0fe1d7ad585b51e146d.zip
Cleanp the new time & graphics APIs
Diffstat (limited to 'chapter31/src/main/smp/sys/time')
-rw-r--r--chapter31/src/main/smp/sys/time/DateTime.smp10
-rw-r--r--chapter31/src/main/smp/sys/time/Timers.smp85
2 files changed, 61 insertions, 34 deletions
diff --git a/chapter31/src/main/smp/sys/time/DateTime.smp b/chapter31/src/main/smp/sys/time/DateTime.smp
index 5d117467..53b6688e 100644
--- a/chapter31/src/main/smp/sys/time/DateTime.smp
+++ b/chapter31/src/main/smp/sys/time/DateTime.smp
@@ -1,10 +1,12 @@
// 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).
+
+// All values default zero/false
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;
+ int year, month, day;
+ int hour, minute, second, millisecond;
+ int weekday, offsetSeconds;
+ bool daylight;
};
// Factory in the defining unit, so callers need no forward instance type.
diff --git a/chapter31/src/main/smp/sys/time/Timers.smp b/chapter31/src/main/smp/sys/time/Timers.smp
index b98c96ef..eca32d15 100644
--- a/chapter31/src/main/smp/sys/time/Timers.smp
+++ b/chapter31/src/main/smp/sys/time/Timers.smp
@@ -1,54 +1,79 @@
// Cooperative timers. poll returns IDs; callbacks never run on another thread.
+// IDs are u32; 0 means failure or no timer due. Millisecond values use _ms.
+
+// The
+
+
struct Timers {
- i64[] !_ids=new i64[8], !_due=new i64[8], !_period=new i64[8];
- i64 !_next=1;
+ // Collection of timers (as struct-of-arrays)
+ u32[] !_ids = new u32[8]; // Which timer id for this slot
+ i64[] !_due_ms = new i64[8]; // Monotonic deadline in milliseconds
+ i64[] !_period_ms = new i64[8]; // When does it repeat
+ // Next available timer ID; 0 means all IDs have been used.
+ u32 !_next = 1;
- val _add = { i64 current, i64 delay, i64 period ->
- if( current<0 || delay<0 || current>time.LIMIT-delay || _next==time.LIMIT ) return -1;
+ // Add a new timer
+ { i64 i64 i64 -> u32 } _add = { i64 current_ms, i64 delay_ms, i64 period_ms ->
+ if( current_ms<0 || delay_ms<0 || current_ms>sys.integer.MAX_I64-delay_ms || _next==0 ) return 0;
+ // Ugly linear scan for an available slot
int slot=0;
while( slot<_ids# && _ids[slot]!=0 ) slot++;
+ // Double size for lots of timers
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;
+ if( _ids#>sys.integer.MAX_I32/2 ) return 0;
+ val ids = new u32[_ids#*2];
+ val due_ms = new i64[_ids#*2], periods_ms = new i64[_ids#*2];
+ for( int i=0; i<_ids#; i++ ) { ids[i]=_ids[i]; due_ms[i]=_due_ms[i]; periods_ms[i]=_period_ms[i]; }
+ _ids=ids; _due_ms=due_ms; _period_ms=periods_ms;
// Retry against the newly published arrays after growing.
- return _add(current,delay,period);
+ return _add(current_ms,delay_ms,period_ms);
}
- i64 id=_next++;
- _ids[slot]=id; _due[slot]=current+delay; _period[slot]=period;
+ // Next available timer id
+ u32 id = _next;
+ _next = id==sys.integer.MAX_U32 ? 0 : id+1;
+ _ids[slot]=id; _due_ms[slot]=current_ms+delay_ms; _period_ms[slot]=period_ms;
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;
+ { i64 i64 -> u32 } afterAt = { i64 current_ms, i64 delay_ms -> _add(current_ms,delay_ms,0); };
+ { i64 i64 -> u32 } everyAt = { i64 current_ms, i64 period_ms -> period_ms<=0 ? 0 : _add(current_ms,period_ms,period_ms); };
+ { i64 -> u32 } after = { i64 delay_ms -> afterAt(time.monotonic(),delay_ms ); };
+ { i64 -> u32 } every = { i64 period_ms -> everyAt(time.monotonic(),period_ms); };
+
+ // Remove a timer
+ val cancel = { u32 id ->
+ if( id==0 ) return false;
for( int i=0; i<_ids#; i++ ) if( _ids[i]==id ) { _ids[i]=0; return true; }
return false;
};
+
+ // Earliest absolute monotonic deadline in milliseconds, or -1 when empty.
val nextDeadline = { ->
- i64 next=-1;
+ i64 next_ms = -1;
+ // Ugly linear scan again, but short
for( int i=0; i<_ids#; i++ )
- if( _ids[i]!=0 && (next<0 || _due[i]<next) ) next=_due[i];
- return next;
+ if( _ids[i]!=0 && (next_ms<0 || _due_ms[i] < next_ms) ) next_ms = _due_ms[i];
+ return next_ms;
};
+
+ // Next firing timer.
+
// Earliest deadline first; equal deadlines use registration order.
// Repeats skip missed ticks and retain their phase, avoiding bursts/drift.
- val poll = { i64 current ->
- int slot=-1;
+ { i64 -> u32 } poll = { i64 current_ms ->
+ 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;
+ if( _ids[i]!=0 && _due_ms[i]<=current_ms && (slot<0 || _due_ms[i]<_due_ms[slot] ||
+ (_due_ms[i]==_due_ms[slot] && _ids[i]<_ids[slot])) ) slot=i;
+ if( slot<0 ) return 0;
+ u32 id = _ids[slot];
+ i64 period_ms = _period_ms[slot];
+ if( period_ms==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;
+ i64 steps = (current_ms-_due_ms[slot])/period_ms;
+ if( steps>=(sys.integer.MAX_I64-_due_ms[slot])/period_ms ) _ids[slot]=0;
+ else _due_ms[slot] += (steps+1)*period_ms;
}
return id;
};