Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-prism

Source of release 0.6.1

0.6.1 / tests / store_workers / main.lucb

## Share the Store façade pointer across Base workers; each thread begin()s locally.
from luce_prism import prism
import thread
import interop
import strings

var ready: @u64
var go: @u64
var errors: @u64
var next_worker: @u64

struct Context:
    var store: prism.Store*

func work(raw: void*?):
    let context = (Context*)(raw else trap("worker context"))
    let worker = next_worker.add(1, .relaxed)
    var identity_scratch: u8[32]
    let identity = strings.format(identity_scratch, f"worker-{worker}") catch failure:
        _ = errors.add(1, .relaxed)
        recover ""
    _ = ready.add(1, .release)
    while go.load(.acquire) == 0:
        thread.sleep(1)
    for round in 0..<8:
        var scratch: u8[32]
        let path = strings.format(scratch, f"/w{round}") catch failure:
            _ = errors.add(1, .relaxed)
            recover ""
        if identity.length == 0 or path.length == 0:
            continue
        var finished = false
        for attempt in 0..<128:
            let tx: interop.Reference[prism.Session]? = context.store.begin_identity(identity) catch failure:
                if failure.code != prism.busy and failure.code != prism.timed_out:
                    _ = errors.add(1, .relaxed)
                thread.sleep(1)
                recover none
            let session = tx else continue
            defer session.release()
            var added = true
            session.value().add(path, "file") catch failure:
                if failure.code != prism.conflict and failure.code != prism.busy:
                    _ = errors.add(1, .relaxed)
                added = false
            if not added:
                continue
            var committed = true
            _ = session.value().commit(wait_ms = 1000) catch failure:
                if failure.code != prism.conflict and failure.code != prism.busy and failure.code != prism.timed_out:
                    _ = errors.add(1, .relaxed)
                committed = false
                thread.sleep(1)
                recover -1
            if committed:
                _ = context.store.bake_identity(identity, wait_ms = 1000) catch failure:
                    _ = errors.add(1, .relaxed)
                    recover -1
                finished = true
                break
        if not finished:
            _ = errors.add(1, .relaxed)

pub func main(arguments: str[]) -> i32!:
    var store = try prism.Store()
    defer store.close()
    for i in 0..<4:
        var identity_scratch: u8[32]
        let identity = try strings.format(identity_scratch, f"worker-{i}")
        try store.mount(identity)
    var workers: thread.Handle[4]
    var context = Context(store = &store)
    for i in 0..<4:
        workers[i] = try thread.spawn(work, &context, name = "prism-store")
    while ready.load(.acquire) != 4:
        thread.sleep(1)
    go.store(1, .release)
    for worker in workers:
        try worker.join()
    assert(errors.load(.acquire) == 0)
    for i in 0..<4:
        var identity_scratch: u8[32]
        let identity = try strings.format(identity_scratch, f"worker-{i}")
        let snap = try store.snapshot_identity(identity)
        defer snap.release()
        let kids = try snap.value().children("/")
        defer kids.release()
        assert(kids.value().count() == 8)
    print("PASS 4 identities concurrently bake 32 Store commits through one shared pool")
    return 0