diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 21:43:19 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 21:43:19 -0400 |
| commit | d8afa73368cdff38125fa1f7d17ad5ce54c84def (patch) | |
| tree | ac4e54673da6ac32df1e351b913b7c1ddd1118b9 /examples/pthreads | |
| parent | f4020db2f0d772481ba71edf78fbb65575badc00 (diff) | |
Improved inline C code: now uses `C_code` keyword and supports
interpolation with @
Diffstat (limited to 'examples/pthreads')
| -rw-r--r-- | examples/pthreads/pthreads.tm | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/examples/pthreads/pthreads.tm b/examples/pthreads/pthreads.tm index 61f94d0a..7f720d5a 100644 --- a/examples/pthreads/pthreads.tm +++ b/examples/pthreads/pthreads.tm @@ -3,66 +3,66 @@ use <pthread.h> struct pthread_mutex_t(; extern, opaque) func new(->@pthread_mutex_t) - return inline C : @pthread_mutex_t { + return C_code : @pthread_mutex_t( pthread_mutex_t *mutex = GC_MALLOC(sizeof(pthread_mutex_t)); pthread_mutex_init(mutex, NULL); GC_register_finalizer(mutex, (void*)pthread_mutex_destroy, NULL, NULL, NULL); mutex - } + ) func lock(m:&pthread_mutex_t) - fail("Failed to lock mutex") unless inline C : Int32 { pthread_mutex_lock(_$m); } == 0 + fail("Failed to lock mutex") unless C_code:Int32(pthread_mutex_lock(@m)) == 0 func unlock(m:&pthread_mutex_t) - fail("Failed to unlock mutex") unless inline C : Int32 { pthread_mutex_unlock(_$m); } == 0 + fail("Failed to unlock mutex") unless C_code:Int32(pthread_mutex_unlock(@m)) == 0 struct pthread_cond_t(; extern, opaque) func new(->@pthread_cond_t) - return inline C : @pthread_cond_t { + return C_code : @pthread_cond_t( pthread_cond_t *cond = GC_MALLOC(sizeof(pthread_cond_t)); pthread_cond_init(cond, NULL); GC_register_finalizer(cond, (void*)pthread_cond_destroy, NULL, NULL, NULL); cond - } + ) func wait(cond:&pthread_cond_t, mutex:&pthread_mutex_t) - fail("Failed to wait on condition") unless inline C : Int32 { pthread_cond_wait(_$cond, _$mutex); } == 0 + fail("Failed to wait on condition") unless C_code:Int32(pthread_cond_wait(@cond, @mutex)) == 0 func signal(cond:&pthread_cond_t) - fail("Failed to signal pthread_cond_t") unless inline C : Int32 { pthread_cond_signal(_$cond); } == 0 + fail("Failed to signal pthread_cond_t") unless C_code:Int32(pthread_cond_signal(@cond)) == 0 func broadcast(cond:&pthread_cond_t) - fail("Failed to broadcast pthread_cond_t") unless inline C : Int32 { pthread_cond_broadcast(_$cond); } == 0 + fail("Failed to broadcast pthread_cond_t") unless C_code:Int32(pthread_cond_broadcast(@cond)) == 0 struct pthread_rwlock_t(; extern, opaque) func new(->@pthread_rwlock_t) - return inline C : @pthread_rwlock_t { + return C_code : @pthread_rwlock_t ( pthread_rwlock_t *lock = GC_MALLOC(sizeof(pthread_rwlock_t)); pthread_rwlock_init(lock, NULL); GC_register_finalizer(lock, (void*)pthread_rwlock_destroy, NULL, NULL, NULL); lock - } + ) func read_lock(lock:&pthread_rwlock_t) - inline C { pthread_rwlock_rdlock(_$lock); } + C_code { pthread_rwlock_rdlock(@lock); } func write_lock(lock:&pthread_rwlock_t) - inline C { pthread_rwlock_wrlock(_$lock); } + C_code { pthread_rwlock_wrlock(@lock); } func unlock(lock:&pthread_rwlock_t) - inline C { pthread_rwlock_unlock(_$lock); } + C_code { pthread_rwlock_unlock(@lock); } struct pthread_t(; extern, opaque) func new(fn:func() -> @pthread_t) - return inline C : @pthread_t { + return C_code:@pthread_t( pthread_t *thread = GC_MALLOC(sizeof(pthread_t)); - pthread_create(thread, NULL, _$fn.fn, _$fn.userdata); + 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); } + func join(p:pthread_t) C_code { pthread_join(@p, NULL); } + func cancel(p:pthread_t) C_code { pthread_cancel(@p); } + func detatch(p:pthread_t) C_code { pthread_detach(@p); } struct IntQueue(_queue:@[Int], _mutex:@pthread_mutex_t, _cond:@pthread_cond_t) func new(initial:[Int]=[] -> IntQueue) |
