kernel: Implement event delivery
307f500bb4a13e0721e35eb8e82f500aedb18252466efcd1ce133f715bea6ea1
Assisted-by: Codex:gpt-6
1 parent
2d5bac68
kernel/kernel.rad
+1 -0
| 10 | 10 | export mod sync; |
|
| 11 | 11 | export mod platform; |
|
| 12 | 12 | export mod trap; |
|
| 13 | 13 | export mod capability; |
|
| 14 | 14 | export mod transactions; |
|
| 15 | + | export mod events; |
|
| 15 | 16 | export mod frames; |
|
| 16 | 17 | export mod backing; |
|
| 17 | 18 | export mod pages; |
|
| 18 | 19 | export mod boot; |
|
| 19 | 20 | @test export mod tests; |
kernel/kernel/events.rad
added
+258 -0
| 1 | + | //! Shared event rings and terminal records retained until acknowledgement. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::sync; |
|
| 6 | + | ||
| 7 | + | /// Shared ring entry count, a power of two. |
|
| 8 | + | export constant CAPACITY: u32 = 256; |
|
| 9 | + | /// Occupancy limit for ordinary events, leaving 64 entries for lifecycle events. |
|
| 10 | + | export constant ORDINARY: u32 = 192; |
|
| 11 | + | /// Device interrupt notification. |
|
| 12 | + | export constant INTERRUPT: u16 = 1; |
|
| 13 | + | /// Timer notification. |
|
| 14 | + | export constant TIMEOUT: u16 = 2; |
|
| 15 | + | /// Terminal exception report. |
|
| 16 | + | export constant FAULT: u16 = 3; |
|
| 17 | + | /// Normal domain termination report. |
|
| 18 | + | export constant CHILD_EXIT: u16 = 4; |
|
| 19 | + | /// Peer wake notification. |
|
| 20 | + | export constant WAKEUP: u16 = 5; |
|
| 21 | + | ||
| 22 | + | /// One asynchronous notification in the shared ABI. |
|
| 23 | + | export record Event: Copy { |
|
| 24 | + | /// Notification kind. |
|
| 25 | + | kind: u16, |
|
| 26 | + | /// Zero for every published event. |
|
| 27 | + | reserved: u16, |
|
| 28 | + | /// Interrupt number, status, fault code, or notification token. |
|
| 29 | + | code: u32, |
|
| 30 | + | /// Kind-specific payload, including generation-bearing terminal domain IDs. |
|
| 31 | + | value: u64, |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// Shared queue memory. Consumer operations require one serialized reader. |
|
| 35 | + | export record Ring: Copy { |
|
| 36 | + | /// Entries published before the producer advances tail. |
|
| 37 | + | data: [Event; CAPACITY], |
|
| 38 | + | /// Consumer progress, accessed through ordered 32-bit operations. |
|
| 39 | + | head: u32, |
|
| 40 | + | /// Producer progress, accessed through ordered 32-bit operations. |
|
| 41 | + | tail: u32, |
|
| 42 | + | /// CAPACITY minus one. |
|
| 43 | + | mask: u32, |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Trusted producer metadata, separate from consumer-writable ring memory. |
|
| 47 | + | export record Queue: Copy { |
|
| 48 | + | /// Live receiver generation, or zero for a closed queue. |
|
| 49 | + | generation: u32, |
|
| 50 | + | /// Last validated consumer counter. |
|
| 51 | + | head: u32, |
|
| 52 | + | /// Authoritative producer counter. |
|
| 53 | + | tail: u32, |
|
| 54 | + | /// Terminal domain per occupied slot; generation zero denotes an ordinary event. |
|
| 55 | + | subjects: [abi::Ref; CAPACITY], |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Lifetime state of a terminal-event record. |
|
| 59 | + | export union State: Copy { |
|
| 60 | + | /// Available for a new domain's terminal reservation. |
|
| 61 | + | Free, |
|
| 62 | + | /// Capacity owned by an unfinished lifecycle transaction. |
|
| 63 | + | Reserved, |
|
| 64 | + | /// Event retained until its receiver has ring capacity. |
|
| 65 | + | Pending, |
|
| 66 | + | /// Event published and awaiting consumer acknowledgement. |
|
| 67 | + | Queued, |
|
| 68 | + | /// Consumer acknowledged the event, or the receiver exited. |
|
| 69 | + | Acknowledged, |
|
| 70 | + | } |
|
| 71 | + | ||
| 72 | + | /// A domain's terminal notification and its retained identity. |
|
| 73 | + | export record Terminal: Copy { |
|
| 74 | + | /// Domain whose identifier must remain reserved. |
|
| 75 | + | subject: abi::Ref, |
|
| 76 | + | /// Receiver selected when termination commits. |
|
| 77 | + | receiver: abi::Ref, |
|
| 78 | + | /// Complete retained event payload. |
|
| 79 | + | event: Event, |
|
| 80 | + | /// Terminal publication and acknowledgement state. |
|
| 81 | + | state: State, |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | /// Fixed event metadata. Callers serialize all kernel-side mutations. |
|
| 85 | + | export record Store: Copy { |
|
| 86 | + | /// Producer metadata indexed by receiver domain. |
|
| 87 | + | queues: [Queue; limits::DOMAINS], |
|
| 88 | + | /// Terminal storage indexed by the terminating domain. |
|
| 89 | + | terminals: [Terminal; limits::DOMAINS], |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Terminal capacity that must be committed or cancelled for the same domain. |
|
| 93 | + | export union Reservation: Once { |
|
| 94 | + | /// Domain generation owning one terminal record. |
|
| 95 | + | Held(abi::Ref), |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | /// Compute forward distance in the shared 32-bit counter space. |
|
| 99 | + | fn distance(end: u32, start: u32) -> u32 { |
|
| 100 | + | return ((end as u64 + 0x100000000 - start as u64) & 0xffffffff) as u32; |
|
| 101 | + | } |
|
| 102 | + | ||
| 103 | + | /// Advance a shared counter with explicit modulo arithmetic. |
|
| 104 | + | fn next(value: u32) -> u32 { return ((value as u64 + 1) & 0xffffffff) as u32; } |
|
| 105 | + | ||
| 106 | + | /// Initialize metadata before any queue or terminal record exists. |
|
| 107 | + | export fn initialize(store: &mut Store) { |
|
| 108 | + | for i in 0..limits::DOMAINS { |
|
| 109 | + | set store.queues[i].generation = 0; |
|
| 110 | + | set store.terminals[i].state = State::Free; |
|
| 111 | + | } |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Test the receiver generation without reading beyond the fixed table. |
|
| 115 | + | fn live(store: &Store, receiver: abi::Ref) -> bool { |
|
| 116 | + | return receiver.index < limits::DOMAINS and receiver.generation <> 0 |
|
| 117 | + | and store.queues[receiver.index].generation == receiver.generation; |
|
| 118 | + | } |
|
| 119 | + | ||
| 120 | + | /// Open a queue over mapped shared memory before publishing its Events handle. |
|
| 121 | + | export fn open(store: &mut Store, receiver: abi::Ref, ring: &mut Ring) throws (abi::Error) { |
|
| 122 | + | if receiver.index >= limits::DOMAINS or receiver.generation == 0 { throw abi::Error::InvalidArg; } |
|
| 123 | + | let queue = &mut store.queues[receiver.index]; |
|
| 124 | + | if queue.generation <> 0 { throw abi::Error::Busy; } |
|
| 125 | + | set queue.generation = receiver.generation; |
|
| 126 | + | set queue.head = 0; |
|
| 127 | + | set queue.tail = 0; |
|
| 128 | + | set ring.mask = CAPACITY - 1; |
|
| 129 | + | sync::storeRelease32(&mut ring.head, 0); |
|
| 130 | + | sync::storeRelease32(&mut ring.tail, 0); |
|
| 131 | + | } |
|
| 132 | + | ||
| 133 | + | /// Validate consumer progress and acknowledge terminal records before slot reuse. |
|
| 134 | + | export fn refresh(store: &mut Store, receiver: abi::Ref, ring: &Ring) throws (abi::Error) { |
|
| 135 | + | if not live(store, receiver) { throw abi::Error::BadHandle; } |
|
| 136 | + | let queue = &mut store.queues[receiver.index]; |
|
| 137 | + | let head = sync::loadAcquire32(&ring.head); |
|
| 138 | + | let advance = distance(head, queue.head); |
|
| 139 | + | if advance > distance(queue.tail, queue.head) { throw abi::Error::InvalidArg; } |
|
| 140 | + | for i in 0..advance { |
|
| 141 | + | let subject = queue.subjects[queue.head & (CAPACITY - 1)]; |
|
| 142 | + | if subject.generation <> 0 { |
|
| 143 | + | let terminal = &mut store.terminals[subject.index]; |
|
| 144 | + | assert terminal.subject == subject |
|
| 145 | + | and terminal.receiver == receiver |
|
| 146 | + | and terminal.state == State::Queued; |
|
| 147 | + | set terminal.state = State::Acknowledged; |
|
| 148 | + | } |
|
| 149 | + | set queue.head = next(queue.head); |
|
| 150 | + | } |
|
| 151 | + | } |
|
| 152 | + | ||
| 153 | + | /// Publish one event after capacity and receiver validation. |
|
| 154 | + | fn publish(queue: &mut Queue, ring: &mut Ring, event: Event, subject: abi::Ref) { |
|
| 155 | + | let index = queue.tail & (CAPACITY - 1); |
|
| 156 | + | set ring.data[index] = event; |
|
| 157 | + | set queue.subjects[index] = subject; |
|
| 158 | + | set queue.tail = next(queue.tail); |
|
| 159 | + | sync::storeRelease32(&mut ring.tail, queue.tail); |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Send an ordinary event, returning false when its reserved occupancy limit is reached. |
|
| 163 | + | export fn send(store: &mut Store, receiver: abi::Ref, ring: &mut Ring, event: Event) -> bool throws (abi::Error) { |
|
| 164 | + | if event.reserved <> 0 or (event.kind <> INTERRUPT and event.kind <> TIMEOUT and event.kind <> WAKEUP) { |
|
| 165 | + | throw abi::Error::InvalidArg; |
|
| 166 | + | } |
|
| 167 | + | try refresh(store, receiver, ring); |
|
| 168 | + | let queue = &mut store.queues[receiver.index]; |
|
| 169 | + | if distance(queue.tail, queue.head) >= ORDINARY { return false; } |
|
| 170 | + | publish(queue, ring, event, abi::Ref { index: 0, generation: 0 }); |
|
| 171 | + | return true; |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | /// Read one published event and release its slot. Callers serialize consumers. |
|
| 175 | + | export fn pop(ring: &mut Ring) -> ?Event throws (abi::Error) { |
|
| 176 | + | let head = sync::loadAcquire32(&ring.head); |
|
| 177 | + | let tail = sync::loadAcquire32(&ring.tail); |
|
| 178 | + | let count = distance(tail, head); |
|
| 179 | + | if count > CAPACITY or ring.mask <> CAPACITY - 1 { throw abi::Error::InvalidArg; } |
|
| 180 | + | if count == 0 { return nil; } |
|
| 181 | + | let event = ring.data[head & (CAPACITY - 1)]; |
|
| 182 | + | sync::storeRelease32(&mut ring.head, next(head)); |
|
| 183 | + | return event; |
|
| 184 | + | } |
|
| 185 | + | ||
| 186 | + | /// Reserve terminal capacity before committing domain termination. |
|
| 187 | + | export fn reserve(store: &mut Store, subject: abi::Ref) -> Reservation throws (abi::Error) { |
|
| 188 | + | if subject.index >= limits::DOMAINS or subject.generation == 0 { throw abi::Error::InvalidArg; } |
|
| 189 | + | if store.terminals[subject.index].state <> State::Free { throw abi::Error::Busy; } |
|
| 190 | + | set store.terminals[subject.index].subject = subject; set store.terminals[subject.index].state = State::Reserved; |
|
| 191 | + | return Reservation::Held(subject); |
|
| 192 | + | } |
|
| 193 | + | ||
| 194 | + | /// Cancel terminal capacity before domain termination commits. |
|
| 195 | + | export fn cancel(store: &mut Store, reservation: Reservation) { |
|
| 196 | + | match reservation { |
|
| 197 | + | case Reservation::Held(subject) => { |
|
| 198 | + | assert store.terminals[subject.index].subject == subject and store.terminals[subject.index].state == State::Reserved; |
|
| 199 | + | set store.terminals[subject.index].state = State::Free; |
|
| 200 | + | }, |
|
| 201 | + | } |
|
| 202 | + | } |
|
| 203 | + | ||
| 204 | + | /// Commit one terminal event to its selected receiver. A missing receiver needs no delivery. |
|
| 205 | + | export fn finish(store: &mut Store, reservation: Reservation, receiver: abi::Ref, kind: u16, code: u32) { |
|
| 206 | + | assert kind == FAULT or kind == CHILD_EXIT; |
|
| 207 | + | let active = live(store, receiver); |
|
| 208 | + | match reservation { |
|
| 209 | + | case Reservation::Held(subject) => { |
|
| 210 | + | let terminal = &mut store.terminals[subject.index]; |
|
| 211 | + | assert terminal.subject == subject and terminal.state == State::Reserved; |
|
| 212 | + | set terminal.receiver = receiver; |
|
| 213 | + | set terminal.event = Event { kind, reserved: 0, code, value: abi::id(subject) }; |
|
| 214 | + | if active { set terminal.state = State::Pending; } |
|
| 215 | + | else { set terminal.state = State::Acknowledged; } |
|
| 216 | + | }, |
|
| 217 | + | } |
|
| 218 | + | } |
|
| 219 | + | ||
| 220 | + | /// Publish retained terminal events in domain-index order as ring capacity permits. |
|
| 221 | + | export fn flush(store: &mut Store, receiver: abi::Ref, ring: &mut Ring) -> u32 throws (abi::Error) { |
|
| 222 | + | try refresh(store, receiver, ring); |
|
| 223 | + | let mut count: u32 = 0; |
|
| 224 | + | for i in 0..limits::DOMAINS { |
|
| 225 | + | if distance(store.queues[receiver.index].tail, store.queues[receiver.index].head) == CAPACITY { break; } |
|
| 226 | + | if store.terminals[i].state <> State::Pending or store.terminals[i].receiver <> receiver { continue; } |
|
| 227 | + | set store.terminals[i].state = State::Queued; |
|
| 228 | + | let event = store.terminals[i].event; |
|
| 229 | + | let subject = store.terminals[i].subject; |
|
| 230 | + | publish(&mut store.queues[receiver.index], ring, event, subject); |
|
| 231 | + | set count += 1; |
|
| 232 | + | } |
|
| 233 | + | return count; |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Report whether a retained terminal identity can be released. |
|
| 237 | + | export fn acknowledged(store: &Store, subject: abi::Ref) -> bool throws (abi::Error) { |
|
| 238 | + | if subject.index >= limits::DOMAINS { throw abi::Error::BadHandle; } |
|
| 239 | + | if store.terminals[subject.index].state == State::Free or store.terminals[subject.index].subject <> subject { throw abi::Error::BadHandle; } |
|
| 240 | + | return store.terminals[subject.index].state == State::Acknowledged; |
|
| 241 | + | } |
|
| 242 | + | ||
| 243 | + | /// Release terminal metadata only after its queued identifier is no longer observable. |
|
| 244 | + | export fn release(store: &mut Store, subject: abi::Ref) throws (abi::Error) { |
|
| 245 | + | if not try acknowledged(store, subject) { throw abi::Error::Busy; } |
|
| 246 | + | set store.terminals[subject.index].state = State::Free; |
|
| 247 | + | } |
|
| 248 | + | ||
| 249 | + | /// Close a stopped domain's queue and end all terminal references held by that receiver. |
|
| 250 | + | export fn close(store: &mut Store, receiver: abi::Ref) throws (abi::Error) { |
|
| 251 | + | if not live(store, receiver) { throw abi::Error::BadHandle; } |
|
| 252 | + | set store.queues[receiver.index].generation = 0; |
|
| 253 | + | for i in 0..limits::DOMAINS { |
|
| 254 | + | if (store.terminals[i].state == State::Pending or store.terminals[i].state == State::Queued) and store.terminals[i].receiver == receiver { |
|
| 255 | + | set store.terminals[i].state = State::Acknowledged; |
|
| 256 | + | } |
|
| 257 | + | } |
|
| 258 | + | } |
kernel/kernel/tests.rad
+1 -0
| 9 | 9 | export mod capability; |
|
| 10 | 10 | export mod frames; |
|
| 11 | 11 | export mod backing; |
|
| 12 | 12 | export mod pages; |
|
| 13 | 13 | export mod transactions; |
|
| 14 | + | export mod events; |
kernel/kernel/tests/events.rad
added
+177 -0
| 1 | + | //! Event ring saturation, counter validation, and retained terminal identities. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::events; |
|
| 6 | + | use kernel::sync; |
|
| 7 | + | ||
| 8 | + | /// Kernel event metadata workspace. |
|
| 9 | + | unsafe static STORE: events::Store = undefined; |
|
| 10 | + | /// Consumer-visible ring for the receiver. |
|
| 11 | + | unsafe static RING: events::Ring = undefined; |
|
| 12 | + | ||
| 13 | + | /// Return the receiver's current domain reference. |
|
| 14 | + | fn receiver() -> abi::Ref { return abi::Ref { index: 0, generation: 1 }; } |
|
| 15 | + | ||
| 16 | + | /// Reset metadata and open one queue. |
|
| 17 | + | unsafe fn initialize() { |
|
| 18 | + | events::initialize(&mut STORE); |
|
| 19 | + | try! events::open(&mut STORE, receiver(), &mut RING); |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Build an ordinary wakeup with a recognizable payload. |
|
| 23 | + | fn wake(value: u64) -> events::Event { |
|
| 24 | + | return events::Event { kind: events::WAKEUP, reserved: 0, code: 17, value }; |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Fill ordinary capacity while preserving the reserved lifecycle capacity. |
|
| 28 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 29 | + | initialize(); |
|
| 30 | + | for i in 0..events::ORDINARY { |
|
| 31 | + | try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(i as u64))); |
|
| 32 | + | } |
|
| 33 | + | try testing::expect(not try! events::send(&mut STORE, receiver(), &mut RING, wake(999))); |
|
| 34 | + | for i in 0..65 { |
|
| 35 | + | let subject = abi::Ref { index: i + 1, generation: 1 }; |
|
| 36 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 37 | + | events::finish(&mut STORE, pending, receiver(), events::CHILD_EXIT, i); |
|
| 38 | + | } |
|
| 39 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 64); |
|
| 40 | + | let subject = abi::Ref { index: 65, generation: 1 }; |
|
| 41 | + | try testing::expect(not try! events::acknowledged(&STORE, subject)); |
|
| 42 | + | for i in 0..events::ORDINARY { |
|
| 43 | + | let event = try! events::pop(&mut RING) else panic "ordinary event"; |
|
| 44 | + | try testing::expect(event == wake(i as u64)); |
|
| 45 | + | } |
|
| 46 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 47 | + | for i in 0..65 { |
|
| 48 | + | let event = try! events::pop(&mut RING) else panic "terminal event"; |
|
| 49 | + | try testing::expect(event.kind == events::CHILD_EXIT and event.code == i); |
|
| 50 | + | try testing::expect(event.value == abi::id(abi::Ref { index: i + 1, generation: 1 })); |
|
| 51 | + | } |
|
| 52 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 53 | + | try testing::expect(try! events::acknowledged(&STORE, subject)); |
|
| 54 | + | try! events::release(&mut STORE, subject); |
|
| 55 | + | let replacement = try! events::reserve(&mut STORE, abi::Ref { index: 65, generation: 2 }); |
|
| 56 | + | events::cancel(&mut STORE, replacement); |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Head and tail arithmetic remains valid across the u32 boundary. |
|
| 60 | + | @test unsafe fn counterWrap() throws (testing::TestError) { |
|
| 61 | + | initialize(); |
|
| 62 | + | set STORE.queues[0].head = 0xfffffffe; |
|
| 63 | + | set STORE.queues[0].tail = 0xfffffffe; |
|
| 64 | + | sync::storeRelease32(&mut RING.head, 0xfffffffe); |
|
| 65 | + | sync::storeRelease32(&mut RING.tail, 0xfffffffe); |
|
| 66 | + | for i in 0..4 { try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(i as u64))); } |
|
| 67 | + | for i in 0..4 { |
|
| 68 | + | let event = try! events::pop(&mut RING) else panic "wrapped event"; |
|
| 69 | + | try testing::expect(event == wake(i as u64)); |
|
| 70 | + | } |
|
| 71 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 72 | + | try testing::expect(STORE.queues[0].head == 2 and STORE.queues[0].tail == 2); |
|
| 73 | + | try testing::expect(try! events::pop(&mut RING) == nil); |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Invalid consumer advances cannot acknowledge or overwrite a pending event. |
|
| 77 | + | @test unsafe fn invalidConsumer() throws (testing::TestError) { |
|
| 78 | + | initialize(); |
|
| 79 | + | try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(42))); |
|
| 80 | + | for head in &[2 as u32, 0xffffffff] { |
|
| 81 | + | sync::storeRelease32(&mut RING.head, head); |
|
| 82 | + | let mut failed = false; |
|
| 83 | + | try events::refresh(&mut STORE, receiver(), &RING) catch err { |
|
| 84 | + | try testing::expect(err == abi::Error::InvalidArg); set failed = true; |
|
| 85 | + | }; |
|
| 86 | + | try testing::expect(failed and STORE.queues[0].head == 0 and STORE.queues[0].tail == 1); |
|
| 87 | + | } |
|
| 88 | + | sync::storeRelease32(&mut RING.head, 0); |
|
| 89 | + | let event = try! events::pop(&mut RING) else panic "retained event"; |
|
| 90 | + | try testing::expect(event == wake(42)); |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// A receiver's exit ends both queued and unpublished terminal references. |
|
| 94 | + | @test unsafe fn receiverExit() throws (testing::TestError) { |
|
| 95 | + | initialize(); |
|
| 96 | + | let subject = abi::Ref { index: 1, generation: 7 }; |
|
| 97 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 98 | + | events::finish(&mut STORE, pending, receiver(), events::FAULT, 23); |
|
| 99 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 100 | + | let waiting = abi::Ref { index: 2, generation: 3 }; |
|
| 101 | + | let second = try! events::reserve(&mut STORE, waiting); |
|
| 102 | + | events::finish(&mut STORE, second, receiver(), events::CHILD_EXIT, 0); |
|
| 103 | + | try! events::close(&mut STORE, receiver()); |
|
| 104 | + | try testing::expect(try! events::acknowledged(&STORE, subject)); |
|
| 105 | + | try testing::expect(try! events::acknowledged(&STORE, waiting)); |
|
| 106 | + | try! events::open(&mut STORE, abi::Ref { index: 0, generation: 2 }, &mut RING); |
|
| 107 | + | try testing::expect(try! events::pop(&mut RING) == nil); |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Terminal IDs stay reserved through counter wrap and until producer acknowledgement. |
|
| 111 | + | @test unsafe fn terminalIdentity() throws (testing::TestError) { |
|
| 112 | + | initialize(); |
|
| 113 | + | set STORE.queues[0].head = 0xffffffff; |
|
| 114 | + | set STORE.queues[0].tail = 0xffffffff; |
|
| 115 | + | sync::storeRelease32(&mut RING.head, 0xffffffff); |
|
| 116 | + | sync::storeRelease32(&mut RING.tail, 0xffffffff); |
|
| 117 | + | let subject = abi::Ref { index: 1, generation: 4 }; |
|
| 118 | + | let replacement = abi::Ref { index: 1, generation: 5 }; |
|
| 119 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 120 | + | events::finish(&mut STORE, pending, receiver(), events::FAULT, 13); |
|
| 121 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 122 | + | let event = try! events::pop(&mut RING) else panic "terminal wrap"; |
|
| 123 | + | try testing::expect(event.kind == events::FAULT and event.code == 13 and event.value == abi::id(subject)); |
|
| 124 | + | let mut busy: u32 = 0; |
|
| 125 | + | try events::release(&mut STORE, subject) catch err { |
|
| 126 | + | try testing::expect(err == abi::Error::Busy); set busy += 1; |
|
| 127 | + | }; |
|
| 128 | + | try reserveAndCancel(replacement) catch err { |
|
| 129 | + | try testing::expect(err == abi::Error::Busy); set busy += 1; |
|
| 130 | + | }; |
|
| 131 | + | sync::storeRelease32(&mut RING.head, 1); |
|
| 132 | + | let mut invalid = false; |
|
| 133 | + | try events::refresh(&mut STORE, receiver(), &RING) catch err { |
|
| 134 | + | try testing::expect(err == abi::Error::InvalidArg); set invalid = true; |
|
| 135 | + | }; |
|
| 136 | + | try testing::expect(invalid and busy == 2 and not try! events::acknowledged(&STORE, subject)); |
|
| 137 | + | sync::storeRelease32(&mut RING.head, 0); |
|
| 138 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 139 | + | try! events::release(&mut STORE, subject); |
|
| 140 | + | let next = try! events::reserve(&mut STORE, replacement); |
|
| 141 | + | events::finish(&mut STORE, next, abi::Ref { index: 0, generation: 0 }, events::CHILD_EXIT, 0); |
|
| 142 | + | try testing::expect(try! events::acknowledged(&STORE, replacement)); |
|
| 143 | + | let mut stale = false; |
|
| 144 | + | try events::acknowledged(&STORE, subject) catch err { |
|
| 145 | + | try testing::expect(err == abi::Error::BadHandle); set stale = true; |
|
| 146 | + | }; |
|
| 147 | + | try testing::expect(stale); |
|
| 148 | + | } |
|
| 149 | + | ||
| 150 | + | /// Consume a successful reservation probe without committing termination. |
|
| 151 | + | unsafe fn reserveAndCancel(subject: abi::Ref) throws (abi::Error) { |
|
| 152 | + | let pending = try events::reserve(&mut STORE, subject); |
|
| 153 | + | events::cancel(&mut STORE, pending); |
|
| 154 | + | } |
|
| 155 | + | ||
| 156 | + | /// Queue and terminal pools reject invalid generations and restore cancelled capacity. |
|
| 157 | + | @test unsafe fn capacityAndValidation() throws (testing::TestError) { |
|
| 158 | + | initialize(); |
|
| 159 | + | try testing::expect(@sizeOf(events::Event) == 16 and @sizeOf(events::Ring) == 4112); |
|
| 160 | + | for i in 0..512 { try! reserveAndCancel(abi::Ref { index: i, generation: 1 }); } |
|
| 161 | + | let mut invalid: u32 = 0; |
|
| 162 | + | for subject in &[abi::Ref { index: 512, generation: 1 }, abi::Ref { index: 0, generation: 0 }] { |
|
| 163 | + | try reserveAndCancel(subject) catch err { |
|
| 164 | + | try testing::expect(err == abi::Error::InvalidArg); set invalid += 1; |
|
| 165 | + | }; |
|
| 166 | + | } |
|
| 167 | + | let mut event = wake(0); |
|
| 168 | + | set event.kind = events::FAULT; |
|
| 169 | + | try events::send(&mut STORE, receiver(), &mut RING, event) catch err { |
|
| 170 | + | try testing::expect(err == abi::Error::InvalidArg); set invalid += 1; |
|
| 171 | + | }; |
|
| 172 | + | set event.kind = events::WAKEUP; set event.reserved = 1; |
|
| 173 | + | try events::send(&mut STORE, receiver(), &mut RING, event) catch err { |
|
| 174 | + | try testing::expect(err == abi::Error::InvalidArg); set invalid += 1; |
|
| 175 | + | }; |
|
| 176 | + | try testing::expect(invalid == 4 and STORE.queues[0].tail == 0); |
|
| 177 | + | } |