home: hub: hare-pthread

Download patch

ref: fadbab49dc34508f835d31aaaa86135e51b3154e
parent: b1e7119f598c5375b72008f85e0bb98740ba5377
author: grobe0ba <grobe0ba@tcp80.org>
date: Mon Oct 16 16:50:45 CDT 2023

update for hare changes

also correct and update test

--- a/fns.ha
+++ b/fns.ha
@@ -2,7 +2,7 @@
 export @symbol("pthread_atfork") fn atfork(pthreadfn, pthreadfn, pthreadfn) int;
 
 export @symbol("pthread_attr_destroy") fn attr_destroy(*attr_t) int;
-export @symbol("pthread_attr_getstack") fn attr_getstack(*attr_t, nullable **void, *u64) int;
+export @symbol("pthread_attr_getstack") fn attr_getstack(*attr_t, nullable **opaque, *u64) int;
 export @symbol("pthread_attr_getstacksize") fn attr_getstacksize(const *attr_t, *u64) int;
 export @symbol("pthread_attr_getstackaddr") fn attr_getstackaddr(const *attr_t, *u64) int;
 export @symbol("pthread_attr_getguardsize") fn attr_getguardsize(const *attr_t, *u64) int;
@@ -9,13 +9,13 @@
 export @symbol("pthread_attr_getdetachstate") fn attr_getdetachstate(const *attr_t, *int) int;
 export @symbol("pthread_attr_init") fn attr_init(*attr_t) int;
 export @symbol("pthread_attr_setstacksize") fn attr_setstacksize(*attr_t, u64) int;
-export @symbol("pthread_attr_setstack") fn attr_setstack(*attr_t, nullable *void, *u64) int;
-export @symbol("pthread_attr_setstackaddr") fn attr_setstackaddr(*attr_t, nullable *void) int;
+export @symbol("pthread_attr_setstack") fn attr_setstack(*attr_t, nullable *opaque, *u64) int;
+export @symbol("pthread_attr_setstackaddr") fn attr_setstackaddr(*attr_t, nullable *opaque) int;
 export @symbol("pthread_attr_setguardsize") fn attr_setguardsize(*attr_t, u64) int;
 export @symbol("pthread_attr_setdetachstate") fn attr_setdetachstate(*attr_t, int) int;
 
 export @symbol("pthread_cleanup_pop") fn cleanup_pop(int) void;
-export @symbol("pthread_cleanup_push") fn cleanup_push(pthreadfn, nullable *void) void;
+export @symbol("pthread_cleanup_push") fn cleanup_push(pthreadfn, nullable *opaque) void;
 
 export @symbol("pthread_condattr_destroy") fn condattr_destroy(*condattr_t) int;
 export @symbol("pthread_condattr_init") fn condattr_init(*condattr_t) int;
@@ -27,12 +27,12 @@
 export @symbol("pthread_cond_timedwait") fn cond_timedwait(*cond_t, *mutex_t, const *rt::timespec) int;
 export @symbol("pthread_cond_wait") fn cond_wait(*cond_t, *mutex_t) int;
 
-export @symbol("pthread_create") fn create(*pthread_t, const *attr_t, pthreadfn, nullable *void) int;
+export @symbol("pthread_create") fn create(*pthread_t, const *attr_t, pthreadfn, nullable *opaque) int;
 export @symbol("pthread_detach") fn detach(pthread_t) int;
 export @symbol("pthread_equal") fn equal(pthread_t, pthread_t) int;
-export @symbol("pthread_exit") @noreturn fn exit(nullable *void) void;
-export @symbol("pthread_getspecific") fn getspecific(key_t) nullable *void;
-export @symbol("pthread_join") fn join(pthread_t, nullable **void) int;
+export @symbol("pthread_exit") fn exit(nullable *opaque) never;
+export @symbol("pthread_getspecific") fn getspecific(key_t) nullable *opaque;
+export @symbol("pthread_join") fn join(pthread_t, nullable **opaque) int;
 
 export @symbol("pthread_key_create") fn key_create(*key_t, pthreadfn) int;
 export @symbol("pthread_key_delete") fn key_delete(key_t) int;
@@ -68,7 +68,7 @@
 export @symbol("pthread_rwlockattr_destroy") fn rwlockattr_destroy(*rwlockattr_t) int;
 
 export @symbol("pthread_self") fn self() pthread_t;
-export @symbol("pthread_setspecific") fn setspecific(key_t, nullable *void) int;
+export @symbol("pthread_setspecific") fn setspecific(key_t, nullable *opaque) int;
 export @symbol("pthread_cancel") fn cancel(pthread_t) int;
 export @symbol("pthread_setcancelstate") fn setcancelstate(int, *int) int;
 export @symbol("pthread_setcanceltype") fn setcanceltype(int, *int) int;
--- a/pthread.ha
+++ b/pthread.ha
@@ -42,17 +42,17 @@
 //
 // These are mostly opaque to the user.
 
-export type pthread = void;
-export type attr = void;
-export type cond = void;
-export type condattr = void;
-export type mutex = void;
-export type mutexattr = void;
-export type rwlock = void;
-export type rwlockattr = void;
-export type barrier = void;
-export type barrierattr = void;
-export type spinlock = void;
+export type pthread = opaque;
+export type attr = opaque;
+export type cond = opaque;
+export type condattr = opaque;
+export type mutex = opaque;
+export type mutexattr = opaque;
+export type rwlock = opaque;
+export type rwlockattr = opaque;
+export type barrier = opaque;
+export type barrierattr = opaque;
+export type spinlock = opaque;
 
 // Primitive system data type definitions required by P1003.1c.
 
@@ -71,8 +71,8 @@
 
 // Additional type definitions
 
-type addr_t = *void;
-type startroutine_t = *fn(*void) void;
+type addr_t = *opaque;
+type startroutine_t = *fn(*opaque) void;
 
 // Once definitions
 export type once_t = struct{
@@ -90,15 +90,15 @@
 export const PRIO_Protect: int = 2;
 
 // Mutex types
-type mutextype = enum int {
-	ErrorCheck = 1,
-	Recursive = 2,
-	Normal = 3,
-	Strict_NP = 4,
+export type mutextype = enum int {
+	Timed = 0,
+	Recursive = 1,
+	ErrorCheck = 2,
+	Adaptive = 3,
 	Max
 };
 
-export type pthreadfn = *const fn(*void) nullable *void;
+export type pthreadfn = *const fn(*opaque) nullable *opaque;
 
 // From /usr/include/sched.h
 
@@ -113,10 +113,10 @@
 type thread_info = struct {
 	thread_id: pthread_t,
 	thread_num: int,
-	thread_data: nullable *void,
+	thread_data: nullable *opaque,
 };
 
-fn thread_start(arg: *void) nullable *void = {
+fn thread_start(arg: *opaque) nullable *opaque = {
 	let tinfo: *thread_info = (arg: *thread_info);
 	let wrmtx: *mutex_t = (tinfo.thread_data: *mutex_t);
 
@@ -124,7 +124,7 @@
 	fmt::printf("Thread {}\n", tinfo.thread_num)!;
 	mutex_unlock(wrmtx);
 
-	return null;
+	exit(null);
 };
 
 @test fn primary_test() void = {
@@ -140,9 +140,14 @@
 	let wrmtx: mutex_t = null;
 	let mtxattr: mutexattr_t = null;
 
-	mutexattr_init(&mtxattr);
-	mutex_init(&wrmtx, &mtxattr);
+	if(mutexattr_init(&mtxattr) != 0) {
+		fmt::fatal("pthread::mutexattr_init");
+	};
 
+	if(mutex_init(&wrmtx, &mtxattr) != 0) {
+		fmt::fatal("pthread::mutex_init");
+	};
+
 	let tinfo: [4]thread_info = [thread_info { thread_id = null, thread_num = 0, thread_data = &wrmtx }...];
 
 	for (let i = 0; i < num_threads; i += 1) {
@@ -153,9 +158,6 @@
 		};
 	};
 
-	if(attr_destroy(&attr) != 0) {
-		fmt::fatal("pthread::attr_destroy");
-	};
 
 	for (let i = 0; i < num_threads; i += 1) {
 		if (join(tinfo[i].thread_id, null) != 0) {
@@ -165,6 +167,14 @@
 		mutex_lock(&wrmtx);
 		fmt::printf("Joined with thread {}...\n", tinfo[i].thread_num)!;
 		mutex_unlock(&wrmtx);
+	};
+
+	if(attr_destroy(&attr) != 0) {
+		fmt::fatal("pthread::attr_destroy");
+	};
+
+	if(mutexattr_destroy(&mtxattr) != 0) {
+		fmt::fatal("pthread::mutexattr_destroy");
 	};
 
 };