Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_gpu_wait.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_wait.c` 171 lines ยท 6089 bytes Raw Blame History 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 #include "omega_gpu_wait.h" 18 19 #include 20 21 #ifndef OMEGA_GPU_NO_POSIX 22 #include 23 static uint64_t real_now_ns(void *ctx) { 24 (void)ctx; 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 #define DEFAULT_NOW_NS real_now_ns 30 #else 31 /* P07 cut C: the freestanding build has no clock of its own; the caller passes the platform clock 32 * (cfg->now_ns, e.g. OmegaGpuPlatform.time_now_ns) and a wait without one is refused (INVALID_STATE). */ 33 #define DEFAULT_NOW_NS NULL 34 #endif 35 36 static void real_barrier(void *ctx) { 37 (void)ctx; 38 #if defined(__aarch64__) 39 __asm__ volatile("dsb sy" ::: "memory"); 40 #else 41 __atomic_thread_fence(__ATOMIC_SEQ_CST); 42 #endif 43 } 44 45 static void real_relax(void *ctx) { 46 (void)ctx; 47 #if defined(__aarch64__) 48 __asm__ volatile("yield"); 49 #else 50 __asm__ volatile("" ::: "memory"); 51 #endif 52 } 53 54 const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r) { 55 switch (r) { 56 case OMEGA_GPU_WAIT_PASS: return "PASS"; 57 case OMEGA_GPU_WAIT_TIMEOUT: return "TIMEOUT"; 58 case OMEGA_GPU_WAIT_STALLED: return "STALLED"; 59 case OMEGA_GPU_WAIT_INVALID_STATE: return "INVALID_STATE"; 60 case OMEGA_GPU_WAIT_ABORTED: return "ABORTED"; 61 default: return "UNKNOWN"; 62 } 63 } 64 65 const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k) { 66 return k == OMEGA_GPU_WAIT_FIXED ? "fixed" : k == OMEGA_GPU_WAIT_SEQUENCE ? "sequence" : "unknown"; 67 } 68 69 static bool wait_core(omega_gpu_wait_kind_t kind, volatile uint32_t *word, uint32_t expected, 70 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 71 omega_gpu_wait_report_t local; 72 omega_gpu_wait_cfg_t zero; 73 if (!rpt) rpt = &local; 74 memset(rpt, 0, sizeof(*rpt)); 75 rpt->wait_kind = kind; 76 rpt->expected = expected; 77 if (!cfg) { 78 memset(&zero, 0, sizeof(zero)); 79 cfg = &zero; 80 } 81 82 uint64_t (*now_ns)(void *) = cfg->now_ns ? cfg->now_ns : DEFAULT_NOW_NS; 83 if (!now_ns) { 84 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; /* no clock: a bounded wait cannot be bounded */ 85 return false; 86 } 87 void (*barrier)(void *) = cfg->barrier ? cfg->barrier : real_barrier; 88 void (*relax)(void *) = cfg->relax ? cfg->relax : real_relax; 89 uint64_t total_ns = 1000000ull * (cfg->total_timeout_ms ? cfg->total_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS); 90 uint64_t stall_ns = 1000000ull * (cfg->progress_timeout_ms ? cfg->progress_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS); 91 92 uint64_t start = now_ns(cfg->hook_ctx); 93 uint64_t hard_deadline = start + total_ns; 94 uint64_t stall_deadline = start + stall_ns; 95 rpt->start_ns = start; 96 rpt->last_progress_ns = start; 97 uint64_t now = start; 98 bool first = true; 99 100 /* A null word is a caller bug; report it as an invalid state, not a hang. */ 101 if (!word) { 102 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 103 return false; 104 } 105 106 while (now < hard_deadline) { 107 uint32_t m, m2 = 0, snap[2] = {0, 0}; 108 if (cfg->abort && cfg->abort(cfg->hook_ctx)) { 109 rpt->result = OMEGA_GPU_WAIT_ABORTED; 110 rpt->elapsed_ns = now - start; 111 return false; 112 } 113 #ifndef OMEGA_GPU_WAIT_MUTANT_NO_READ_BARRIER 114 barrier(cfg->hook_ctx); 115 #endif 116 m = *word; 117 if (cfg->marker2) m2 = *cfg->marker2; 118 if (cfg->snapshot) cfg->snapshot(cfg->snapshot_ctx, snap); 119 rpt->polls++; 120 now = now_ns(cfg->hook_ctx); 121 122 bool changed = m != rpt->last_observed || m2 != rpt->last_marker2 || 123 snap[0] != rpt->snap[0] || snap[1] != rpt->snap[1]; 124 rpt->last_observed = m; 125 rpt->last_marker2 = m2; 126 rpt->snap[0] = snap[0]; 127 rpt->snap[1] = snap[1]; 128 rpt->elapsed_ns = now - start; 129 130 if (changed && !first) { 131 rpt->progress_count++; 132 rpt->last_progress_ns = now; 133 #ifndef OMEGA_GPU_WAIT_MUTANT_STALL_NO_RESET 134 stall_deadline = now + stall_ns; 135 #endif 136 #ifdef OMEGA_GPU_WAIT_MUTANT_PROGRESS_EXTENDS_HARD 137 hard_deadline = now + total_ns; 138 #endif 139 } 140 bool ok = kind == OMEGA_GPU_WAIT_FIXED ? (m == expected) : ((int32_t)(m - expected) >= 0); 141 if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { 142 barrier(cfg->hook_ctx); /* acquire: later CPU loads are ordered after the marker load */ 143 rpt->result = OMEGA_GPU_WAIT_PASS; 144 return true; 145 } 146 if (cfg->valid && !cfg->valid(cfg->hook_ctx, m)) { 147 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 148 return false; 149 } 150 first = false; 151 if (now >= stall_deadline) { 152 rpt->result = OMEGA_GPU_WAIT_STALLED; 153 return false; 154 } 155 relax(cfg->hook_ctx); 156 now = now_ns(cfg->hook_ctx); 157 } 158 rpt->elapsed_ns = now - start; 159 rpt->result = OMEGA_GPU_WAIT_TIMEOUT; 160 return false; 161 } 162 163 bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, 164 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 165 return wait_core(OMEGA_GPU_WAIT_FIXED, word, expected, rpt, cfg); 166 } 167 168 bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, 169 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 170 return wait_core(OMEGA_GPU_WAIT_SEQUENCE, word, target, rpt, cfg); 171 }