feat: Std::Sync — Mutex and RwLock primitives

- Add bux_mutex_* and bux_rwlock_* C runtime functions
- Add library/std/Sync.bux with Mutex and RwLock wrappers
- Add examples/sync.bux — threaded counter with Mutex
- Update docs/Stdlib.md with Sync section and example
- Update README.md feature table
This commit is contained in:
2026-06-05 22:30:44 +03:00
parent 809785711c
commit d0de9c2abf
5 changed files with 231 additions and 2 deletions
+35
View File
@@ -0,0 +1,35 @@
import Std::Io::{PrintLine, PrintInt};
import Std::Task::{Task_Join, TaskHandle};
import Std::Sync::{Mutex, Mutex_New, Mutex_Lock, Mutex_Unlock};
struct Counter {
value: int;
mtx: Mutex;
}
func Incrementer(arg: *void) -> *void {
let c: *Counter = arg as *Counter;
var i: int = 0;
while i < 100000 {
Mutex_Lock(&c.mtx);
c.value = c.value + 1;
Mutex_Unlock(&c.mtx);
i = i + 1;
}
return null;
}
func Main() -> int {
var counter: Counter = Counter { value: 0, mtx: Mutex_New() };
let a: *void = spawn Incrementer(&counter);
let b: *void = spawn Incrementer(&counter);
Task_Join(TaskHandle { handle: a });
Task_Join(TaskHandle { handle: b });
PrintLine("Counter:");
PrintInt(counter.value);
PrintLine("");
return 0;
}