Files
sx/examples/1636-cfnptr-pthread-reentry.sx
agra 4fa12853ed test: pin C function pointers into sx (PLAN-HTTPZ C2 — pre-existing)
Both halves of the C2 contract already work in JIT and AOT; these
examples pin them. 1635: libc qsort drives an sx callconv(.c)
comparator passed by name as a typed fn-pointer param. 1636: a real
pthread enters sx through a callconv(.c) entry, fabricates its own
Context (push Context with a local GPA), and runs default-conv sx code
that allocates through it — the re-entry contract std.thread (S6)
stands on. Also unblocks the sqlite callback APIs (hooks/UDFs) left
unbound by design in P5.1.
2026-06-12 22:14:14 +03:00

30 lines
1.1 KiB
Plaintext

// PLAN-HTTPZ C2: the C->sx re-entry contract. A real OS thread enters
// sx through a `callconv(.c)` entry (which has NO implicit context),
// fabricates its own Context via `push Context.{ allocator = xx gpa }`,
// and calls default-conv sx code that allocates through it.
#import "modules/std.sx";
clib :: #library "c";
pthread_create :: (thread: *usize, attr: *void, start: (*void) -> *void callconv(.c), arg: *void) -> i32 #foreign clib;
pthread_join :: (thread: usize, retval: **void) -> i32 #foreign clib;
entry :: (arg: *void) -> *void callconv(.c) {
p : *i64 = xx arg;
gpa := GPA.init();
push Context.{ allocator = xx gpa } {
s := concat("th", "read"); // allocate through the thread's OWN context
if s.len == 6 { p.* = p.* * 2; }
}
return null;
}
main :: () -> i32 {
v : i64 = 21;
th : usize = 0;
if pthread_create(@th, null, entry, xx @v) != 0 { print("create failed\n"); return 1; }
pthread_join(th, null);
if v != 42 { print("wrong result: {}\n", v); return 1; }
print("pthread into sx ok: {}\n", v);
return 0;
}