diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-17 20:42:05 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-17 20:42:05 -0400 |
| commit | ae81254f990e5c4cf766848493263ac88251e646 (patch) | |
| tree | b13f731ee687aabaf858964abe43a2e4638d487c | |
| parent | 2b2eff0987ad3f7e46c39906f32680bc61f48c87 (diff) | |
Add pthread example
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | examples/pthread/pthread.tm | 41 |
2 files changed, 43 insertions, 1 deletions
@@ -72,7 +72,8 @@ clean: pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@ examples: - ./tomo -IL examples/vectors examples/commands examples/shell examples/base64 examples/log examples/ini examples/game examples/http examples/threads examples/tomodeps examples/tomo-install examples/wrap + ./tomo -IL examples/commands examples/shell examples/base64 examples/log examples/ini examples/vectors examples/game \ + examples/http examples/threads examples/tomodeps examples/tomo-install examples/wrap examples/pthread ./tomo examples/learnxiny.tm install: tomo libtomo.so tomo.1 diff --git a/examples/pthread/pthread.tm b/examples/pthread/pthread.tm new file mode 100644 index 00000000..339cc160 --- /dev/null +++ b/examples/pthread/pthread.tm @@ -0,0 +1,41 @@ +use <pthread.h> + +struct pthread_cond_t(; extern, opaque): + func new(->@pthread_cond_t): + return inline C : @pthread_cond_t { + pthread_cond_t *cond = new(pthread_cond_t); + pthread_cond_init(cond, NULL); + GC_register_finalizer(cond, (void*)pthread_cond_destroy, NULL, NULL, NULL); + cond + } + +struct pthread_mutex_t(; extern, opaque): + func new(->@pthread_mutex_t): + return inline C : @pthread_mutex_t { + pthread_mutex_t *mutex = new(pthread_mutex_t); + pthread_mutex_init(mutex, NULL); + GC_register_finalizer(mutex, (void*)pthread_mutex_destroy, NULL, NULL, NULL); + mutex + } + +extern pthread_cond_wait:func(cond:&pthread_cond_t, mutex:&pthread_mutex_t -> Int32) +extern pthread_cond_signal:func(cond:&pthread_cond_t -> Int32) +extern pthread_cond_broadcast:func(cond:&pthread_cond_t -> Int32) +extern pthread_mutex_lock:func(mutex:&pthread_mutex_t -> Int32) +extern pthread_mutex_unlock:func(mutex:&pthread_mutex_t -> Int32) + +# extern pthread_join:func(thread:pthread_t, retval=none:@@Memory) +# extern pthread_cancel:func(thread:pthread_t) +# extern pthread_detach:func(thread:pthread_t) + +struct pthread_t(; extern, opaque): + func new(fn:func() -> @pthread_t): + return inline C : @pthread_t { + pthread_t *thread = new(pthread_t); + pthread_create(thread, NULL, _$fn.fn, _$fn.userdata); + thread + } + + func join(p:@pthread_t): inline C { pthread_join(*_$p, NULL); } + func cancel(p:@pthread_t): inline C { pthread_cancel(*_$p); } + func detatch(p:@pthread_t): inline C { pthread_detach(*_$p); } |
