omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / runtime / rx_seq_reference.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* rx_seq_reference.c -- R15 sequential control. REFERENCE ORACLE ONLY. |
| 18 | * See rx_seq_reference.h. */ |
| 19 | #include "rx_seq_reference.h" |
| 20 | |
| 21 | #include <sched.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | static uint64_t now_ns(void) { |
| 25 | struct timespec ts; |
| 26 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 27 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 28 | } |
| 29 | |
| 30 | static uint64_t thread_cpu_ns(void) { |
| 31 | struct timespec ts; |
| 32 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 33 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 34 | } |
| 35 | |
| 36 | /* Poll one stage: has any field it declares as a trigger moved past what it |
| 37 | * last saw? The cause is the writer of the newest such field. Caller holds |
| 38 | * the world lock. */ |
| 39 | static int poll_ready(RxWorld *w, RxReaction *r, uint64_t *cause) { |
| 40 | int ready = r->seq_pending; |
| 41 | uint64_t newest = 0; |
| 42 | *cause = r->seq_pending ? r->wake_cause : 0; |
| 43 | for (uint32_t t = 0; t < r->desc.n_triggers; t++) { |
| 44 | const RxDep *d = &r->desc.triggers[t]; |
| 45 | const RxObject *o = &w->objects[d->obj.id]; |
| 46 | if (!o->live || o->generation != d->obj.generation) continue; |
| 47 | uint64_t seen = r->seq_seen[t]; |
| 48 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) { |
| 49 | if (!(d->mask & RX_FIELD(f))) continue; |
| 50 | uint64_t v = o->field_version[f]; |
| 51 | if (v > r->seq_seen[t]) r->seq_seen[t] = v; |
| 52 | if (v > seen) { |
| 53 | ready = 1; |
| 54 | if (v > newest) { newest = v; *cause = o->field_writer[f]; } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | return ready; |
| 59 | } |
| 60 | |
| 61 | int rx_seq_pulse(RxWorld *w, const RxSeqPlan *plan, uint32_t *ran) { |
| 62 | if (!w || !w->sequential || !plan) return RX_ERR_ARG; |
| 63 | uint32_t n_ran = 0; |
| 64 | int rc = RX_OK; |
| 65 | pthread_mutex_lock(&w->mu); |
| 66 | uint64_t pulse = ++w->seq_pulse_started; |
| 67 | w->stats.seq_pulses++; |
| 68 | for (uint32_t i = 0; i < plan->n && rc == RX_OK; i++) { |
| 69 | uint32_t rid = plan->order[i]; |
| 70 | if (rid >= w->n_reactions || w->reactions[rid].removed) continue; |
| 71 | RxReaction *r = &w->reactions[rid]; |
| 72 | /* The readiness poll is SEQ's scheduling work (spec §6.4). */ |
| 73 | uint64_t c0 = w->timing ? thread_cpu_ns() : 0; |
| 74 | uint64_t t0 = w->timing ? now_ns() : 0; |
| 75 | w->stats.seq_polls++; |
| 76 | uint64_t cause = 0; |
| 77 | int ready = r->state == RX_DORMANT && poll_ready(w, r, &cause); |
| 78 | uint64_t poll_wall = 0, poll_cpu = 0; |
| 79 | if (w->timing) { |
| 80 | poll_wall = now_ns() - t0; |
| 81 | poll_cpu = thread_cpu_ns() - c0; |
| 82 | w->stats.sched_wall_ns += poll_wall; |
| 83 | w->stats.sched_cpu_ns += poll_cpu; |
| 84 | } |
| 85 | if (!ready) continue; |
| 86 | if (rx_world_seq_activate_timed_locked(w, rid, cause, t0, poll_wall, poll_cpu) != 1) |
| 87 | continue; |
| 88 | n_ran++; |
| 89 | w->stats.seq_runs++; |
| 90 | if (!(r->resident_seat && r->state == RX_RUNNING)) continue; |
| 91 | /* Dispatch GPU, wait. */ |
| 92 | w->stats.gpu_host_waits++; |
| 93 | uint64_t limit = now_ns() + (uint64_t)(plan->gpu_timeout_ms ? plan->gpu_timeout_ms |
| 94 | : 5000) * 1000000ull; |
| 95 | while (r->state != RX_DORMANT) { |
| 96 | pthread_mutex_unlock(&w->mu); |
| 97 | int g = plan->gpu_step ? plan->gpu_step(plan->gpu_ctx) : RX_ERR_ARG; |
| 98 | if (g == 0) sched_yield(); |
| 99 | pthread_mutex_lock(&w->mu); |
| 100 | if (g < 0 && g != RX_ERR_NOT_FOUND) { rc = g; break; } |
| 101 | if (now_ns() > limit) { rc = RX_ERR_TIMEOUT; break; } |
| 102 | } |
| 103 | } |
| 104 | /* Idle only if this pulse polled every registered reaction. A plan built |
| 105 | * before a reaction was added misses it, so finding nothing ready proves |
| 106 | * nothing: the late reaction may have pending input (R15 SEQ setup race, |
| 107 | * spec §2.1). The caller rebuilds its plan and pulses again. */ |
| 108 | if (n_ran == 0 && rc == RX_OK && plan->n >= w->n_reactions) { |
| 109 | w->seq_idle_start = pulse; |
| 110 | pthread_cond_broadcast(&w->idle_cv); |
| 111 | } |
| 112 | pthread_mutex_unlock(&w->mu); |
| 113 | if (ran) *ran = n_ran; |
| 114 | return rc; |
| 115 | } |
| 116 | |
| 117 | int rx_seq_run_until_complete(RxWorld *w, const RxSeqPlan *plan, uint32_t max_pulses, |
| 118 | uint32_t *pulses) { |
| 119 | uint32_t p = 0; |
| 120 | int rc = RX_OK; |
| 121 | for (; p < max_pulses; p++) { |
| 122 | uint32_t ran = 0; |
| 123 | rc = rx_seq_pulse(w, plan, &ran); |
| 124 | if (rc != RX_OK || ran == 0) { p++; break; } |
| 125 | } |
| 126 | if (pulses) *pulses = p; |
| 127 | return rc; |
| 128 | } |