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 / omega_fp8_slot_table.c
| 1 | /* omega_fp8_slot_table.c: host-only slot state machine (see the header). */ |
| 2 | #include "omega_fp8_slot_table.h" |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | int omega_fp8_slot_table_init(OmegaFp8SlotTable *t, uint32_t slots) { |
| 7 | if (!t || slots == 0) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 8 | memset(t, 0, sizeof *t); |
| 9 | t->rec = calloc(slots, sizeof *t->rec); |
| 10 | if (!t->rec) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 11 | if (pthread_mutex_init(&t->mu, NULL) != 0) { free(t->rec); t->rec = NULL; return OMEGA_FP8_SLOT_BAD_ARGS; } |
| 12 | t->slots = slots; |
| 13 | t->next_version = 1; |
| 14 | return OMEGA_FP8_SLOT_OK; |
| 15 | } |
| 16 | |
| 17 | void omega_fp8_slot_table_fini(OmegaFp8SlotTable *t) { |
| 18 | if (!t || !t->rec) return; |
| 19 | pthread_mutex_destroy(&t->mu); |
| 20 | free(t->rec); |
| 21 | t->rec = NULL; |
| 22 | t->slots = 0; |
| 23 | } |
| 24 | |
| 25 | #define LOCKED(t, body) do { pthread_mutex_lock(&(t)->mu); body; pthread_mutex_unlock(&(t)->mu); } while (0) |
| 26 | |
| 27 | int omega_fp8_slot_begin_load(OmegaFp8SlotTable *t, uint32_t slot) { |
| 28 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 29 | int rc = OMEGA_FP8_SLOT_OK; |
| 30 | LOCKED(t, { |
| 31 | OmegaFp8SlotRec *r = &t->rec[slot]; |
| 32 | if (r->state == OMEGA_FP8_SLOT_LOADING || (r->pins && !t->mutant)) { rc = OMEGA_FP8_SLOT_BUSY; t->stats.busy_refusals++; } |
| 33 | else r->state = OMEGA_FP8_SLOT_LOADING; /* hides the old version: pin(old) is STALE from here */ |
| 34 | }); |
| 35 | return rc; |
| 36 | } |
| 37 | |
| 38 | int omega_fp8_slot_finish_load(OmegaFp8SlotTable *t, uint32_t slot, uint64_t key, int ok, uint64_t *version) { |
| 39 | if (version) *version = 0; |
| 40 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 41 | int rc = OMEGA_FP8_SLOT_OK; |
| 42 | LOCKED(t, { |
| 43 | OmegaFp8SlotRec *r = &t->rec[slot]; |
| 44 | if (r->state != OMEGA_FP8_SLOT_LOADING) rc = OMEGA_FP8_SLOT_BAD_ARGS; |
| 45 | else if (ok) { |
| 46 | r->state = OMEGA_FP8_SLOT_READY; r->key = key; r->version = t->next_version++; |
| 47 | if (version) *version = r->version; |
| 48 | t->stats.loads++; |
| 49 | } else { r->state = OMEGA_FP8_SLOT_EMPTY; r->key = 0; t->stats.load_failures++; } |
| 50 | }); |
| 51 | return rc; |
| 52 | } |
| 53 | |
| 54 | int omega_fp8_slot_pin(OmegaFp8SlotTable *t, uint32_t slot, uint64_t version) { |
| 55 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 56 | int rc = OMEGA_FP8_SLOT_OK; |
| 57 | LOCKED(t, { |
| 58 | OmegaFp8SlotRec *r = &t->rec[slot]; |
| 59 | if (r->state != OMEGA_FP8_SLOT_READY || r->version != version || version == 0) { rc = OMEGA_FP8_SLOT_STALE; t->stats.stale_refusals++; } |
| 60 | else { |
| 61 | r->pins++; t->stats.pins++; t->stats.pinned_now++; |
| 62 | if (t->stats.pinned_now > t->stats.pinned_high) t->stats.pinned_high = t->stats.pinned_now; |
| 63 | } |
| 64 | }); |
| 65 | return rc; |
| 66 | } |
| 67 | |
| 68 | int omega_fp8_slot_unpin(OmegaFp8SlotTable *t, uint32_t slot, uint64_t version) { |
| 69 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 70 | int rc = OMEGA_FP8_SLOT_OK; |
| 71 | LOCKED(t, { |
| 72 | OmegaFp8SlotRec *r = &t->rec[slot]; |
| 73 | /* Under the mutant a pinned slot may have been reloaded: the pin is still dropped (version not checked). */ |
| 74 | if (r->pins == 0 || (!t->mutant && r->version != version)) rc = OMEGA_FP8_SLOT_BAD_ARGS; |
| 75 | else { r->pins--; t->stats.pinned_now--; } |
| 76 | }); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | int omega_fp8_slot_evict(OmegaFp8SlotTable *t, uint32_t slot) { |
| 81 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 82 | int rc = OMEGA_FP8_SLOT_OK; |
| 83 | LOCKED(t, { |
| 84 | OmegaFp8SlotRec *r = &t->rec[slot]; |
| 85 | if (r->pins || r->state == OMEGA_FP8_SLOT_LOADING) { rc = OMEGA_FP8_SLOT_BUSY; t->stats.busy_refusals++; } |
| 86 | else { if (r->state == OMEGA_FP8_SLOT_READY) t->stats.evictions++; r->state = OMEGA_FP8_SLOT_EMPTY; r->key = 0; } |
| 87 | }); |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | int omega_fp8_slot_query(OmegaFp8SlotTable *t, uint32_t slot, OmegaFp8SlotRec *out) { |
| 92 | if (!t || !t->rec || slot >= t->slots || !out) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 93 | LOCKED(t, { *out = t->rec[slot]; }); |
| 94 | return OMEGA_FP8_SLOT_OK; |
| 95 | } |
| 96 | |
| 97 | int omega_fp8_slot_table_idle(OmegaFp8SlotTable *t) { |
| 98 | if (!t || !t->rec) return 1; |
| 99 | int idle = 1; |
| 100 | LOCKED(t, { |
| 101 | for (uint32_t i = 0; i < t->slots; i++) |
| 102 | if (t->rec[i].pins || t->rec[i].state == OMEGA_FP8_SLOT_LOADING) { idle = 0; break; } |
| 103 | }); |
| 104 | return idle; |
| 105 | } |
| 106 | |
| 107 | void omega_fp8_slot_table_stats(OmegaFp8SlotTable *t, OmegaFp8SlotStats *out) { |
| 108 | if (!out) return; |
| 109 | memset(out, 0, sizeof *out); |
| 110 | if (!t || !t->rec) return; |
| 111 | LOCKED(t, { *out = t->stats; }); |
| 112 | } |
| 113 | |
| 114 | void omega_fp8_slot_table_set_mutant(OmegaFp8SlotTable *t, int mutant) { |
| 115 | if (!t || !t->rec) return; |
| 116 | LOCKED(t, { t->mutant = mutant; }); |
| 117 | } |