Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.46.0

0.46.0 / src / back / native / x86_64 / parallel.lucb

#==============================================================================================
#
#   x86_64/parallel - Generating in parallel
#
#   DESCRIPTION:
#       Each function generated on whichever thread takes it, by a generator of its own.
#
#==============================================================================================
# mark: Generating in parallel ================================================================

## What the threads generating a program's functions share: the generator they copy, each
## function's text or failure, and whether it called the half routines.
struct Generating:
    var model: Generator*
    var texts: str[]
    var halves: bool[]
    var failures: parallel.Failure[]

## Generate function `index` of the job `context` points at into a heap copy of its text.
func generate_task(context: void*?, index: usize):
    let job = (Generating*)(context else return)
    var scratch = arenas.Arena(memory.heap, 1048576)
    defer scratch.destroy()
    generate_one(job, index, &scratch) catch failure:
        job.failures[index] = parallel.failed(failure.code, failure.message)

func generate_one(job: Generating*, index: usize, scratch: arenas.Arena*) -> !:
    var generated: str = ""
    var halves = false
    if index < job.model.reused.length and job.model.reused[index].length > 0:
        # the last build's text of this function as it stands now (`reuse`)
        let text = job.model.reused[index]
        with memory.heap:
            var kept = try new u8[text.length] ---
            memory.copy(kept, text.bytes, text.length)
            job.texts[index] = (str)kept
        job.halves[index] = (job.model.reused_flags[index] & 1) != 0
        return
    with scratch:
        var g = try Generator.like(job.model)
        try g.function(index)
        halves = g.half_routines
        let made = g.writer.out.view()
        with memory.heap:
            var kept = try new u8[made.length] ---
            memory.copy(kept, made.bytes, made.length)
            generated = (str)kept
    job.texts[index] = generated
    job.halves[index] = halves

## The reasons a generated check traps with, added to the unit's texts before its functions
## are generated (`generate`).
let trap_reasons: str[2] = ["index out of bounds", "integer overflow"]