moontalk/server/eventloop-server-experiment/events.4th

39 lines
1.0 KiB
Plaintext
Raw Permalink Normal View History

2024-02-17 13:26:14 -05:00
require util.4th
1024 constant MAX_EVENTS
2024-02-03 12:37:01 -05:00
0
cell +field event.id
cell +field event.data
constant /EVENT
2024-02-17 13:26:14 -05:00
0 variable! current-event
0 variable! last-event
2024-02-03 12:37:01 -05:00
2024-02-17 13:26:14 -05:00
MAX_EVENTS /EVENT * constant EVENTS_SIZE
2024-02-03 12:37:01 -05:00
create events EVENTS_SIZE allot
2024-02-17 13:26:14 -05:00
: (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 @ <> ;
2024-02-03 12:37:01 -05:00
: events.enqueue ( data-u id-u -- )
2024-02-17 13:26:14 -05:00
last-event @ dup 1+ dup >r current-event @ = abort" Queue is full."
(translate) (write) r> (wrap) last-event ! ;
2024-02-03 12:37:01 -05:00
: events.dequeue ( -- data-u id-u )
2024-02-17 13:26:14 -05:00
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 ;
2024-02-03 12:37:01 -05:00
events.clear