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
+70
View File
@@ -876,6 +876,76 @@ void bux_channel_free(void* handle) {
free(ch);
}
/* ============================================================================
* Synchronization primitives (Mutex, RwLock)
* ============================================================================ */
typedef struct bux_mutex {
pthread_mutex_t mtx;
} bux_mutex_t;
typedef struct bux_rwlock {
pthread_rwlock_t rwl;
} bux_rwlock_t;
void* bux_mutex_new(void) {
bux_mutex_t* m = (bux_mutex_t*)malloc(sizeof(bux_mutex_t));
if (!m) return NULL;
pthread_mutex_init(&m->mtx, NULL);
return m;
}
void bux_mutex_lock(void* handle) {
if (!handle) return;
bux_mutex_t* m = (bux_mutex_t*)handle;
pthread_mutex_lock(&m->mtx);
}
void bux_mutex_unlock(void* handle) {
if (!handle) return;
bux_mutex_t* m = (bux_mutex_t*)handle;
pthread_mutex_unlock(&m->mtx);
}
void bux_mutex_free(void* handle) {
if (!handle) return;
bux_mutex_t* m = (bux_mutex_t*)handle;
pthread_mutex_destroy(&m->mtx);
free(m);
}
void* bux_rwlock_new(void) {
bux_rwlock_t* rw = (bux_rwlock_t*)malloc(sizeof(bux_rwlock_t));
if (!rw) return NULL;
pthread_rwlock_init(&rw->rwl, NULL);
return rw;
}
void bux_rwlock_rdlock(void* handle) {
if (!handle) return;
bux_rwlock_t* rw = (bux_rwlock_t*)handle;
pthread_rwlock_rdlock(&rw->rwl);
}
void bux_rwlock_wrlock(void* handle) {
if (!handle) return;
bux_rwlock_t* rw = (bux_rwlock_t*)handle;
pthread_rwlock_wrlock(&rw->rwl);
}
void bux_rwlock_unlock(void* handle) {
if (!handle) return;
bux_rwlock_t* rw = (bux_rwlock_t*)handle;
pthread_rwlock_unlock(&rw->rwl);
}
void bux_rwlock_free(void* handle) {
if (!handle) return;
bux_rwlock_t* rw = (bux_rwlock_t*)handle;
pthread_rwlock_destroy(&rw->rwl);
free(rw);
}
/* ============================================================================
* Stackful Coroutines + Async Scheduler (Phase 8.3 true async)
* ============================================================================ */
+58
View File
@@ -0,0 +1,58 @@
module Std::Sync {
extern func bux_mutex_new() -> *void;
extern func bux_mutex_lock(handle: *void);
extern func bux_mutex_unlock(handle: *void);
extern func bux_mutex_free(handle: *void);
extern func bux_rwlock_new() -> *void;
extern func bux_rwlock_rdlock(handle: *void);
extern func bux_rwlock_wrlock(handle: *void);
extern func bux_rwlock_unlock(handle: *void);
extern func bux_rwlock_free(handle: *void);
struct Mutex {
handle: *void;
}
struct RwLock {
handle: *void;
}
func Mutex_New() -> Mutex {
return Mutex { handle: bux_mutex_new() };
}
func Mutex_Lock(m: *Mutex) {
bux_mutex_lock(m.handle);
}
func Mutex_Unlock(m: *Mutex) {
bux_mutex_unlock(m.handle);
}
func Mutex_Free(m: *Mutex) {
bux_mutex_free(m.handle);
}
func RwLock_New() -> RwLock {
return RwLock { handle: bux_rwlock_new() };
}
func RwLock_ReadLock(rw: *RwLock) {
bux_rwlock_rdlock(rw.handle);
}
func RwLock_WriteLock(rw: *RwLock) {
bux_rwlock_wrlock(rw.handle);
}
func RwLock_Unlock(rw: *RwLock) {
bux_rwlock_unlock(rw.handle);
}
func RwLock_Free(rw: *RwLock) {
bux_rwlock_free(rw.handle);
}
}