diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:07:23 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:07:23 -0400 |
| commit | 6782cc5570e194791ca6cdd695b88897e9145564 (patch) | |
| tree | a428e9d954aca251212ec1cf15bd35e0badce630 /examples/pthreads | |
| parent | 448e805293989b06e07878a4a87fdd378f7c6e02 (diff) | |
No more colons for blocks
Diffstat (limited to 'examples/pthreads')
| -rw-r--r-- | examples/pthreads/pthreads.tm | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/examples/pthreads/pthreads.tm b/examples/pthreads/pthreads.tm index 3be052b4..61f94d0a 100644 --- a/examples/pthreads/pthreads.tm +++ b/examples/pthreads/pthreads.tm @@ -1,8 +1,8 @@ # A Posix Threads (pthreads) wrapper use <pthread.h> -struct pthread_mutex_t(; extern, opaque): - func new(->@pthread_mutex_t): +struct pthread_mutex_t(; extern, opaque) + func new(->@pthread_mutex_t) return inline C : @pthread_mutex_t { pthread_mutex_t *mutex = GC_MALLOC(sizeof(pthread_mutex_t)); pthread_mutex_init(mutex, NULL); @@ -10,14 +10,14 @@ struct pthread_mutex_t(; extern, opaque): mutex } - func lock(m:&pthread_mutex_t): + func lock(m:&pthread_mutex_t) fail("Failed to lock mutex") unless inline C : Int32 { pthread_mutex_lock(_$m); } == 0 - func unlock(m:&pthread_mutex_t): + func unlock(m:&pthread_mutex_t) fail("Failed to unlock mutex") unless inline C : Int32 { pthread_mutex_unlock(_$m); } == 0 -struct pthread_cond_t(; extern, opaque): - func new(->@pthread_cond_t): +struct pthread_cond_t(; extern, opaque) + func new(->@pthread_cond_t) return inline C : @pthread_cond_t { pthread_cond_t *cond = GC_MALLOC(sizeof(pthread_cond_t)); pthread_cond_init(cond, NULL); @@ -25,17 +25,17 @@ struct pthread_cond_t(; extern, opaque): cond } - func wait(cond:&pthread_cond_t, mutex:&pthread_mutex_t): + 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 - func signal(cond:&pthread_cond_t): + func signal(cond:&pthread_cond_t) fail("Failed to signal pthread_cond_t") unless inline C : Int32 { pthread_cond_signal(_$cond); } == 0 - func broadcast(cond:&pthread_cond_t): + func broadcast(cond:&pthread_cond_t) fail("Failed to broadcast pthread_cond_t") unless inline C : Int32 { pthread_cond_broadcast(_$cond); } == 0 -struct pthread_rwlock_t(; extern, opaque): - func new(->@pthread_rwlock_t): +struct pthread_rwlock_t(; extern, opaque) + func new(->@pthread_rwlock_t) return inline C : @pthread_rwlock_t { pthread_rwlock_t *lock = GC_MALLOC(sizeof(pthread_rwlock_t)); pthread_rwlock_init(lock, NULL); @@ -43,60 +43,60 @@ struct pthread_rwlock_t(; extern, opaque): lock } - func read_lock(lock:&pthread_rwlock_t): + func read_lock(lock:&pthread_rwlock_t) inline C { pthread_rwlock_rdlock(_$lock); } - func write_lock(lock:&pthread_rwlock_t): + func write_lock(lock:&pthread_rwlock_t) inline C { pthread_rwlock_wrlock(_$lock); } - func unlock(lock:&pthread_rwlock_t): + func unlock(lock:&pthread_rwlock_t) inline C { pthread_rwlock_unlock(_$lock); } -struct pthread_t(; extern, opaque): - func new(fn:func() -> @pthread_t): +struct pthread_t(; extern, opaque) + func new(fn:func() -> @pthread_t) return inline C : @pthread_t { pthread_t *thread = GC_MALLOC(sizeof(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); } + 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); } -struct IntQueue(_queue:@[Int], _mutex:@pthread_mutex_t, _cond:@pthread_cond_t): - func new(initial:[Int]=[] -> IntQueue): +struct IntQueue(_queue:@[Int], _mutex:@pthread_mutex_t, _cond:@pthread_cond_t) + func new(initial:[Int]=[] -> IntQueue) return IntQueue(@initial, pthread_mutex_t.new(), pthread_cond_t.new()) - func give(q:IntQueue, n:Int): - do: q._mutex.lock() + func give(q:IntQueue, n:Int) + do q._mutex.lock() q._queue.insert(n) q._mutex.unlock() q._cond.signal() - func take(q:IntQueue -> Int): - do: q._mutex.lock() + func take(q:IntQueue -> Int) + do q._mutex.lock() n := q._queue.pop(1) - while not n: + while not n q._cond.wait(q._mutex) n = q._queue.pop(1) q._mutex.unlock() return n! fail("Unreachable") -func main(): +func main() jobs := IntQueue.new() results := IntQueue.new() say_mutex := pthread_mutex_t.new() - announce := func(speaker:Text, text:Text): - do: say_mutex.lock() + announce := func(speaker:Text, text:Text) + do say_mutex.lock() say("$\033[2m[$speaker]$\033[m $text") say_mutex.unlock() - worker := pthread_t.new(func(): + worker := pthread_t.new(func() say("I'm in the thread!") - repeat: + repeat announce("worker", "waiting for job") job := jobs.take() result := job * 10 @@ -105,12 +105,12 @@ func main(): announce("worker", "Signaled $result") ) - for i in 10: + for i in 10 announce("boss", "Pushing job $i") jobs.give(i) announce("boss", "Gave job $i") - for i in 10: + for i in 10 announce("boss", "Getting result...") result := results.take() announce("boss", "Got result $result") |
