moontalk/server/eventloop-server-experiment/events.4th
2024-02-17 18:26:14 +00:00

39 lines
1.0 KiB
Forth

require util.4th
1024 constant MAX_EVENTS
0
cell +field event.id
cell +field event.data
constant /EVENT
0 variable! current-event
0 variable! last-event
MAX_EVENTS /EVENT * constant EVENTS_SIZE
create events EVENTS_SIZE allot
: (translate) ( index-u -- event-addr )
]] /EVENT * events + [[ ; IMMEDIATE
: (wrap) ( index-u -- index-u )
]] MAX_EVENTS mod [[ ; IMMEDIATE
: (read) ( addr -- data-u id-u )
]] dup event.data @ swap event.id @ [[ ; IMMEDIATE
: (write) ( data-u id-u addr -- )
]] tuck event.id ! event.data ! [[ ; IMMEDIATE
: events.has-item? ( -- flag )
current-event @ last-event @ <> ;
: events.enqueue ( data-u id-u -- )
last-event @ dup 1+ dup >r current-event @ = abort" Queue is full."
(translate) (write) r> (wrap) last-event ! ;
: events.dequeue ( -- data-u id-u )
events.has-item? invert abort" No events in queue."
current-event @ dup (translate) (read)
rot 1+ (wrap) current-event ! ;
: events.clear ( -- )
0 current-event !
0 last-event !
events EVENTS_SIZE erase ;
events.clear