From 115239add97b889255b999fc668db04edd5bd79f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 11:10:37 +0000 Subject: [PATCH] Step 13: a thread-safe heap with block markers, native threads Heap takes a lock, refuses bad sizes and alignments, and marks each block it gives out, so that freeing one twice, or one it never gave out, stops the program through Rt.stop. Big blocks now honour their alignment. Out locks its buffer. Lock is a spinlock that yields. Thread, for programs without C (link static), spawns threads with clone on mmaped stacks with a guard page, joins them through the kernel's cleared tid, and has a futex Mutex. Examples with pthread_create and with Thread. --- diff --git a/CLAUDE.md b/CLAUDE.md index 22582eb..d959d3e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -38,8 +38,8 @@ ## Where things stand Stages 1 and 2 are done (steps 1-10). Stage 3 (steps 11-14) is under -way: its report changes are written and approved, and steps 11 and 12 -are done. Next is step 13, threads and a thread-safe heap. +way: its report changes are written and approved, and steps 11 to 13 +are done. Next is step 14, line numbers for gdb. `test/libc` runs its programs over `lib/Sys.jed`, the C one, through a link in its `mods`. A test or example without `.expect` is only @@ -80,6 +80,8 @@ - A real constant is held as an F64; one typed `F32` holds a value an F32 can hold. An `F32` in a register is its low 32 bits; the upper ones are left as they fall. +- `Heap` and `Out` lock with `Lock`, a spinlock that yields, since they + serve C's threads and `Thread`'s alike; `Thread.Mutex` is a futex. - Every failed check says "index out of range"; some errors point at the symbol after the offending expression. Error messages are to be improved later, all together. diff --git a/PLAN.md b/PLAN.md index 5fcb551..7c83de7 100644 --- a/PLAN.md +++ b/PLAN.md @@ -114,7 +114,7 @@ with C's `stdio`: flush one before using the other. - [x] 12. `F32`: arithmetic, conversions, calls; real literals become untyped constants that take `F32` or `F64`. -- [ ] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a +- [x] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a marker in each block's header so that freeing a block twice, or one it never gave out, stops the program; `Out` locks its buffer. An example with threads from `pthread_create`. diff --git a/doc/jed.md b/doc/jed.md index 2e959e0..80b399c 100644 --- a/doc/jed.md +++ b/doc/jed.md @@ -811,12 +811,14 @@ | Module | What | |---|---| -| `Rt` | ending the program, `exit` and `atExit`; what the compiler's checks call when they fail; string comparison | -| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target, and `Sys.jed` over the C library for the others | -| `Out` | writing text and numbers to standard output, buffered | -| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`; a program that imports it uses C | +| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail; string comparison | +| `Sys` | system calls: `write`, `mmap`, `munmap`, `yield`, `exit`; one file per target, and `Sys.jed` over the C library for the others | +| `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | +| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | +| `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | +| `Thread` | threads for a program without C: `spawn`, `join`, and `Mutex`; it says `link static` | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | -| `Heap` | a general allocator, `Heap.heap` | +| `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | | `Str` | operations on `[]U8` strings | | `Buf` | a growable byte buffer | diff --git a/examples/Pthreads.expect b/examples/Pthreads.expect new file mode 100644 index 0000000..9f3810a --- /dev/null +++ b/examples/Pthreads.expect @@ -0,0 +1,5 @@ +a part summed +a part summed +a part summed +a part summed +total 500000500000 diff --git a/examples/Pthreads.jed b/examples/Pthreads.jed new file mode 100644 index 0000000..a38ad4f --- /dev/null +++ b/examples/Pthreads.jed @@ -0,0 +1,52 @@ +(* Threads from C's pthread_create, each summing its part of an array + and using the heap and Out, which lock themselves. *) +module Pthreads; +import Out, C, Heap, Mem; + +const + Workers = 4; + N = 1000000; + +type + Part = record lo, hi, sum: I64; end; + Numbers = [N]I64; + +var numbers: ^Numbers; + +proc work(arg: ^U8): ^U8; +var p: ^Part; i: I64; scratch: ^U8; +begin + p := ^Part(arg); + for i := p^.lo to p^.hi - 1 do + p^.sum := p^.sum + numbers^[i]; + if i mod 1000 = 0 then + scratch := Mem.alloc(Heap.heap, 100, 8); + Mem.free(Heap.heap, scratch); + end; + end; + Out.str("a part summed\n"); + return nil; +end; + +proc main*: I64; +var t: [Workers]U64; parts: [Workers]Part; i, total: I64; +begin + NEW(numbers, Heap.heap); + for i := 0 to N - 1 do numbers^[i] := i + 1; end; + for i := 0 to Workers - 1 do + parts[i] := {lo: i * N div Workers, hi: (i + 1) * N div Workers}; + if C.pthread_create(ADR(t[i]), nil, work, ^U8(ADR(parts[i]))) # 0 then + Out.str("can't start a thread\n"); + return 1; + end; + end; + total := 0; + for i := 0 to Workers - 1 do + C.pthread_join(t[i], nil); + total := total + parts[i].sum; + end; + Out.str("total "); Out.int(total); Out.ln; + return 0; +end; + +end Pthreads. diff --git a/examples/Workers.expect b/examples/Workers.expect new file mode 100644 index 0000000..adf0471 --- /dev/null +++ b/examples/Workers.expect @@ -0,0 +1 @@ +total 8167664175825000 diff --git a/examples/Workers.jed b/examples/Workers.jed new file mode 100644 index 0000000..cd95a50 --- /dev/null +++ b/examples/Workers.jed @@ -0,0 +1,48 @@ +(* Native threads, for a program without C: workers that take jobs + from a shared counter and add up their results under a Mutex. *) +module Workers; +import Out, Thread; + +const + Workers = 4; + Jobs = 100; + +var + next: I64; (* the next job, taken with XADD *) + m: Thread.Mutex; + total: I64; + +(* job n: the sum of the squares up to n *) +proc job(n: I64): I64; +var i, s: I64; +begin + s := 0; + for i := 1 to n do s := s + i * i; end; + return s; +end; + +proc work(arg: ^U8); +var n, r: I64; +begin + n := XADD(ADR(next), 1); + while n < Jobs do + r := job(n * 1000); + Thread.lock(ADR(m)); + total := total + r; + Thread.unlock(ADR(m)); + n := XADD(ADR(next), 1); + end; +end; + +proc main*; +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; +begin + for i := 0 to Workers - 1 do + t[i], err := Thread.spawn(work, nil); + if err # nil then Out.str(MSG(err)); Out.ln; return; end; + end; + for i := 0 to Workers - 1 do Thread.join(t[i]); end; + Out.str("total "); Out.int(total); Out.ln; +end; + +end Workers. diff --git a/lib/C.jed b/lib/C.jed index 23a1a2a..57f05bd 100644 --- a/lib/C.jed +++ b/lib/C.jed @@ -10,6 +10,9 @@ extern proc exit*(status: I32); extern proc _exit*(status: I32); extern proc __errno_location*: ^I32; (* errno is this thread's: through here *) +extern proc sched_yield*: I32; +extern proc pthread_create*(t: ^U64; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; +extern proc pthread_join*(t: U64; result: ^^U8): I32; extern proc printf*(format: ^U8; ...): I32; extern proc fflush*(f: ^U8): I32; extern var stdout*: ^U8; (* FILE * *) diff --git a/lib/Heap.jed b/lib/Heap.jed index 4d549ff..aa50fe1 100644 --- a/lib/Heap.jed +++ b/lib/Heap.jed @@ -1,41 +1,65 @@ (* A general allocator: Heap.heap. Small blocks come in nine sizes, 16 to 4096 bytes with their header, each with a list of the free ones; a block bigger than that gets pages of its own. Every block - has a 16-byte header before it: its size class, or minus its size - in bytes for a big one. *) + has a 16-byte header before it: its size class, or minus the size + of its pages for a big one, which start less than a page before it; + then a marker, which says the block is out, + so that freeing it twice, or freeing what the heap never gave out, + stops the program. One lock keeps it whole across threads. *) module Heap; -import Mem; +import Mem, Lock, Rt; const Classes = 9; Header = 16; Refill = 65536; + Largest = 1 shl 46; (* bigger sizes are refused *) + Out = 5A6564426C6F636BH; (* the marker, xor the header's address *) type Block = record next: ^Block; end; + Head = record size, mark: I64; end; var lists: [Classes]^Block; cur: ^U8; (* where the next new small block comes from *) left: I64; + lock: Lock.Lock; std: Mem.Allocator; heap*: ^Mem.Allocator; +(* The block p, with size for its header, given out. *) +proc give(p: ^U8; size: I64): ^U8; +var h: ^Head; +begin + h := ^Head(p); + h^.size := size; + h^.mark := Out xor I64(h); + return p + Header; +end; + +(* size bytes aligned to align, or nil: also for a size below zero or + too big, and an alignment that isn't a power of two up to a page. *) proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; -var c, n: I64; b: ^Block; p: ^U8; h: ^I64; +var c, n: I64; b: ^Block; p: ^U8; begin + if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) + or (align and (align - 1) # 0) then + return nil; + end; n := size + Header; - if (size < 0) or (align > Header) and (align > Mem.Page) then return nil; end; c := 0; while (c < Classes) and (16 shl c < n) do INC(c); end; if (c = Classes) or (align > Header) then - n := (n + Mem.Page - 1) div Mem.Page * Mem.Page; + (* pages of its own, the block at align from their start *) + if align < Header then align := Header; end; + n := (size + align + Mem.Page - 1) div Mem.Page * Mem.Page; p := Mem.pages(n); if p = nil then return nil; end; - h := ^I64(p); - h^ := -n; - return p + Header; + return give(p + align - Header, -n); end; + Lock.take(ADR(lock)); + defer Lock.give(ADR(lock)); b := lists[c]; if b # nil then lists[c] := b^.next; @@ -50,22 +74,27 @@ cur := cur + (16 shl c); left := left - (16 shl c); end; - h := ^I64(p); - h^ := c; - return p + Header; + return give(p, c); end; proc free(a: ^Mem.Allocator; p: ^U8); -var h: ^I64; b: ^Block; +var h: ^Head; b: ^Block; begin if p = nil then return; end; - h := ^I64(p - Header); - if h^ < 0 then - Mem.unmap(^U8(h), -h^); + h := ^Head(p - Header); + Lock.take(ADR(lock)); + if h^.mark # Out xor I64(h) then + Rt.stop("Heap: a block freed twice, or never given out"); + end; + h^.mark := 0; + if h^.size < 0 then + Lock.give(ADR(lock)); + Mem.unmap(^U8(I64(h) - I64(h) mod Mem.Page), -h^.size); else b := ^Block(h); - b^.next := lists[h^]; - lists[h^] := b; + b^.next := lists[h^.size]; + lists[h^.size] := b; + Lock.give(ADR(lock)); end; end; diff --git a/lib/Lock.jed b/lib/Lock.jed new file mode 100644 index 0000000..143c46d --- /dev/null +++ b/lib/Lock.jed @@ -0,0 +1,25 @@ +(* A lock for short sections of code: a word set to 1, atomically, by + the thread that takes it. A thread that finds it taken tries again, + giving up the processor now and then. It works with C's threads and + with Thread's alike; Thread.Mutex is the one that sleeps. *) +module Lock; +import Sys; + +type Lock* = record held: I64; end; + +proc take*(l: ^Lock); +var tries: I64; +begin + tries := 0; + while not CAS(ADR(l^.held), 0, 1) do + INC(tries); + if tries = 64 then Sys.yield; tries := 0; end; + end; +end; + +proc give*(l: ^Lock); +begin + XCHG(ADR(l^.held), 0); +end; + +end Lock. diff --git a/lib/Out.jed b/lib/Out.jed index 392e1fd..5597fec 100644 --- a/lib/Out.jed +++ b/lib/Out.jed @@ -2,17 +2,20 @@ 4 KB. The buffer is written out when it is full, at each line feed when standard output is a terminal, by flush, and when the program ends by Rt.exit. C's stdio keeps a buffer of its own: a program that - writes with both flushes one before writing with the other. *) + writes with both flushes one before writing with the other. A lock + keeps what each call writes together when threads write at once. *) module Out; -import Rt, Sys; +import Rt, Sys, Lock; var buf: [4096]U8; n: I64; (* bytes in buf *) tty: BOOL; (* standard output is a terminal *) + lock: Lock.Lock; -(* Writes out the buffer. What can't be written is dropped. *) -proc flush*; +(* Writes out the buffer, with the lock held. What can't be written is + dropped. *) +proc out; var i, k: I64; begin i := 0; @@ -25,18 +28,31 @@ n := 0; end; -proc char*(c: U8); +proc flush*; begin - if n = LEN(buf) then flush; end; - buf[n] := c; - INC(n); - if (c = '\n') and tty then flush; end; + Lock.take(ADR(lock)); + out; + Lock.give(ADR(lock)); end; proc str*(s: []U8); var i: I64; begin - for i := 0 to LEN(s) - 1 do char(s[i]); end; + Lock.take(ADR(lock)); + for i := 0 to LEN(s) - 1 do + if n = LEN(buf) then out; end; + buf[n] := s[i]; + INC(n); + if (s[i] = '\n') and tty then out; end; + end; + Lock.give(ADR(lock)); +end; + +proc char*(c: U8); +var b: [1]U8; +begin + b[0] := c; + str(b); end; proc ln*; diff --git a/lib/Rt.jed b/lib/Rt.jed index 24bbe79..e68abd5 100644 --- a/lib/Rt.jed +++ b/lib/Rt.jed @@ -1,5 +1,6 @@ (* The run time: ending the program, and what the compiler's code - calls: fail when a check fails, eq when = or # compares two strings. + calls: fail when a check fails, eq when = or # compares two strings; + and stop, for the library's own failures. The compiler finds fail, eq, exit and cExit by these names. *) module Rt; import Sys; @@ -37,6 +38,15 @@ end; if cExit # nil then cExit(I32(status)); end; Sys.exit(status); +end; + +(* Stops the program: says msg on standard error, then ends it by exit + with status 2, as a failed check does. *) +proc stop*(msg: []U8); +begin + Sys.write(Sys.stderr, msg); + Sys.write(Sys.stderr, "\n"); + exit(2); end; (* A check failed at line of file: says so on standard error and ends diff --git a/lib/Sys.jed b/lib/Sys.jed index 8b6cc70..c1a9dc7 100644 --- a/lib/Sys.jed +++ b/lib/Sys.jed @@ -48,6 +48,11 @@ return C.isatty(I32(fd)) = 1; end; +proc yield*; +begin + C.sched_yield(); +end; + (* Ends the program, at once, with status. *) proc exit*(status: I64); begin diff --git a/lib/Sys.linux-x86_64.jed b/lib/Sys.linux-x86_64.jed index 12321c9..e1bcb53 100644 --- a/lib/Sys.linux-x86_64.jed +++ b/lib/Sys.linux-x86_64.jed @@ -38,6 +38,12 @@ return SYSCALL(16, fd, 5401H, ADR(t)) = 0; end; +(* Gives the processor to another thread. *) +proc yield*; +begin + SYSCALL(24); +end; + (* Ends the program, at once, with status. *) proc exit*(status: I64); begin diff --git a/lib/Thread.linux-x86_64.jed b/lib/Thread.linux-x86_64.jed new file mode 100644 index 0000000..27a0917 --- /dev/null +++ b/lib/Thread.linux-x86_64.jed @@ -0,0 +1,113 @@ +(* Threads for programs without C, made with the clone system call. + Each runs on a stack of its own, mmaped, with a page below it that + can't be touched, so that running off the stack stops the program + rather than spoiling what lies below. join waits on the thread's id, + which the kernel clears, and wakes it for, when the thread ends; + a Mutex sleeps on a futex. A program with C threads uses C's, + through pthread_create, and so can't use these. *) +module Thread; +import Sys; +link static; + +const + Failed*: ERROR = "can't start a thread"; + Stack = 1 shl 20; + Page = 4096; + (* clone: share memory, files, signal handlers, the thread group; + write the id to tid, and clear it at the end *) + Flags = 350F00H; + FutexWait = 0; + FutexWake = 1; + Private = 128; + +type + Thread* = record + tid: I64; (* the kernel's id, until the thread ends *) + base: ^U8; (* its stack's mapping, the guard page first *) + size: I64; + end; + (* 0 free, 1 taken, 2 taken with a thread waiting *) + Mutex* = record state: I64; end; + +(* clone(flags, stack, tid, tid), the new thread starting with fn(arg) + on stack and ending, when fn returns, with the exit system call. + fn and arg go on the new stack first, since the new thread has no + registers but rsp of its own to find them by. Returns the new + thread's id, or minus an errno. *) +proc start(flags: I64; stack: ^U8; ptid, ctid: ^I64; fn: proc(arg: ^U8); arg: ^U8): I64; +code 48H, 83H, 0EEH, 10H, (* sub rsi, 16 *) + 4CH, 89H, 06H, (* mov [rsi], r8 *) + 4CH, 89H, 4EH, 08H, (* mov [rsi + 8], r9 *) + 49H, 89H, 0CAH, (* mov r10, rcx *) + 45H, 31H, 0C0H, (* xor r8d, r8d: no tls *) + 0B8H, 38H, 0, 0, 0, (* mov eax, 56: clone *) + 0FH, 05H, (* syscall *) + 48H, 85H, 0C0H, (* test rax, rax *) + 75H, 0DH, (* jnz parent *) + 58H, (* pop rax: fn *) + 5FH, (* pop rdi: arg *) + 0FFH, 0D0H, (* call rax *) + 31H, 0FFH, (* xor edi, edi *) + 0B8H, 3CH, 0, 0, 0, (* mov eax, 60: exit, this thread only *) + 0FH, 05H, (* syscall *) + 0C3H; (* parent: ret *) + +proc futex(word: ^I64; op, val: I64): I64; +begin + return SYSCALL(202, word, op, val, 0); +end; + +(* Starts p(arg) in a new thread. *) +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); +var base: ^U8; t: ^Thread; size: I64; +begin + size := Stack + Page; + base := Sys.mmap(size); + if base = nil then return nil, Failed; end; + SYSCALL(10, base, Page, 0); (* mprotect: the guard page, no access *) + (* the Thread at the top, the stack below it, aligned to 16 *) + t := ^Thread(base + size - 32); + t^.base := base; + t^.size := size; + if start(Flags, base + size - 32, ADR(t^.tid), ADR(t^.tid), p, arg) < 0 then + Sys.munmap(base, size); + return nil, Failed; + end; + return t, nil; +end; + +(* Waits for t to end, then frees its stack. *) +proc join*(t: ^Thread); +var tid: ^VOLATILE I64; v: I64; base: ^U8; size: I64; +begin + tid := ADR(t^.tid); + v := tid^; + while v # 0 do + futex(ADR(t^.tid), FutexWait, v); + v := tid^; + end; + base := t^.base; + size := t^.size; + Sys.munmap(base, size); +end; + +proc lock*(m: ^Mutex); +var c: I64; +begin + if CAS(ADR(m^.state), 0, 1) then return; end; + c := XCHG(ADR(m^.state), 2); + while c # 0 do + futex(ADR(m^.state), FutexWait + Private, 2); + c := XCHG(ADR(m^.state), 2); + end; +end; + +proc unlock*(m: ^Mutex); +begin + if XADD(ADR(m^.state), -1) # 1 then + XCHG(ADR(m^.state), 0); + futex(ADR(m^.state), FutexWake + Private, 1); + end; +end; + +end Thread. diff --git a/test/err/ThreadC.err b/test/err/ThreadC.err new file mode 100644 index 0000000..bd6f33d --- /dev/null +++ b/test/err/ThreadC.err @@ -0,0 +1 @@ +Thread says link static, but diff --git a/test/err/ThreadC.jed b/test/err/ThreadC.jed new file mode 100644 index 0000000..f3794b8 --- /dev/null +++ b/test/err/ThreadC.jed @@ -0,0 +1,8 @@ +module ThreadC; +import Thread, C; + +proc main*; +begin +end; + +end ThreadC. diff --git a/test/run/HeapCheck.expect b/test/run/HeapCheck.expect new file mode 100644 index 0000000..1bffa8d --- /dev/null +++ b/test/run/HeapCheck.expect @@ -0,0 +1,10 @@ +nil +nil +nil +nil +nil +nil +a block +aligned +a block +freeing it again diff --git a/test/run/HeapCheck.jed b/test/run/HeapCheck.jed new file mode 100644 index 0000000..ac430ad --- /dev/null +++ b/test/run/HeapCheck.jed @@ -0,0 +1,34 @@ +(* The heap refuses bad sizes and alignments, and stops the program + when a block is freed twice. *) +module HeapCheck; +import Out, Heap, Mem; + +proc show(p: ^U8); +begin + if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end; +end; + +proc main*; +var p, q: ^U8; +begin + show(Mem.alloc(Heap.heap, -1, 8)); + show(Mem.alloc(Heap.heap, 1 shl 60, 8)); + show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8)); + show(Mem.alloc(Heap.heap, 10, 3)); + show(Mem.alloc(Heap.heap, 10, 0)); + show(Mem.alloc(Heap.heap, 10, 8192)); + p := Mem.alloc(Heap.heap, 10, 64); + show(p); + if I64(p) mod 64 = 0 then Out.str("aligned\n"); end; + Mem.free(Heap.heap, p); + q := Mem.alloc(Heap.heap, 100000, 8); + show(q); + Mem.free(Heap.heap, q); + p := Mem.alloc(Heap.heap, 24, 8); + Mem.free(Heap.heap, p); + Out.str("freeing it again\n"); + Mem.free(Heap.heap, p); + Out.str("not reached\n"); +end; + +end HeapCheck. diff --git a/test/run/HeapCheck.status b/test/run/HeapCheck.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/HeapCheck.status @@ -0,0 +1 @@ +2 diff --git a/test/run/HeapStray.expect b/test/run/HeapStray.expect new file mode 100644 index 0000000..3102032 --- /dev/null +++ b/test/run/HeapStray.expect @@ -0,0 +1 @@ +freeing a stray pointer diff --git a/test/run/HeapStray.jed b/test/run/HeapStray.jed new file mode 100644 index 0000000..c8f69f9 --- /dev/null +++ b/test/run/HeapStray.jed @@ -0,0 +1,14 @@ +(* Freeing what the heap never gave out stops the program. *) +module HeapStray; +import Out, Heap, Mem; + +var a: [4]I64; + +proc main*; +begin + Out.str("freeing a stray pointer\n"); + Mem.free(Heap.heap, ^U8(ADR(a[2]))); + Out.str("not reached\n"); +end; + +end HeapStray. diff --git a/test/run/HeapStray.status b/test/run/HeapStray.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/HeapStray.status @@ -0,0 +1 @@ +2 diff --git a/test/run/Spawn.expect b/test/run/Spawn.expect new file mode 100644 index 0000000..4269e12 --- /dev/null +++ b/test/run/Spawn.expect @@ -0,0 +1,201 @@ +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +160000 160000 diff --git a/test/run/Spawn.jed b/test/run/Spawn.jed new file mode 100644 index 0000000..22ccd7d --- /dev/null +++ b/test/run/Spawn.jed @@ -0,0 +1,50 @@ +(* Native threads: a counter under a Mutex, the heap and Out used from + every thread at once, and join. *) +module Spawn; +import Out, Thread, Heap, Mem; + +const + Workers = 8; + Rounds = 20000; + +type Node = record next: ^Node; n: I64; end; + +var + m: Thread.Mutex; + count, atomic: I64; + +proc work(arg: ^U8); +var i: I64; p, list: ^Node; +begin + list := nil; + for i := 1 to Rounds do + Thread.lock(ADR(m)); + INC(count); + Thread.unlock(ADR(m)); + XADD(ADR(atomic), 1); + NEW(p, Heap.heap); + p^.next := list; + list := p; + if i mod 4 = 0 then + while list # nil do + p := list^.next; + Mem.free(Heap.heap, ^U8(list)); + list := p; + end; + end; + end; + for i := 1 to 25 do Out.str("the same line from every thread\n"); end; +end; + +proc main*; +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; +begin + for i := 0 to Workers - 1 do + t[i], err := Thread.spawn(work, nil); + if err # nil then Out.str(MSG(err)); Out.ln; return; end; + end; + for i := 0 to Workers - 1 do Thread.join(t[i]); end; + Out.int(count); Out.char(' '); Out.int(atomic); Out.ln; +end; + +end Spawn.