diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-01-02 16:24:07 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-01-02 16:24:07 -0500 |
| commit | be384c0caa92cb152c264125fb265373e6a50440 (patch) | |
| tree | b823fb0dd4cfec643670236688a2a7ca76787d7b /test | |
| parent | 2fcf1939bb295887592c1f24f7b8fbb10efcfcba (diff) | |
Replace threads with generic mutexed datastructures.
Diffstat (limited to 'test')
| -rw-r--r-- | test/optionals.tm | 19 | ||||
| -rw-r--r-- | test/threads.tm | 96 |
2 files changed, 52 insertions, 63 deletions
diff --git a/test/optionals.tm b/test/optionals.tm index 5aefedf4..13a8dcae 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -61,12 +61,6 @@ func maybe_c_string(should_i:Bool->CString?): else: return none -func maybe_channel(should_i:Bool->|Int|?): - if should_i: - return |:Int|? - else: - return none - func maybe_thread(should_i:Bool->Thread?): if should_i: return Thread.new(func(): pass) @@ -246,19 +240,6 @@ func main(): do: !! ... - !! Channels: - >> yep := maybe_channel(yes) - # No "=" test here because channels use addresses in the text version - >> nope := maybe_channel(no) - = none : |:Int|? - >> if yep: >> yep - else: fail("Falsey: $yep") - >> if nope: - fail("Truthy: $nope") - else: !! Falsey: $nope - - do: - !! ... !! Threads: >> yep := maybe_thread(yes) # No "=" test here because threads use addresses in the text version diff --git a/test/threads.tm b/test/threads.tm index 8fa61778..772c866f 100644 --- a/test/threads.tm +++ b/test/threads.tm @@ -1,66 +1,74 @@ enum Job(Increment(x:Int), Decrement(x:Int)) func main(): - do: - >> channel := |:Int| - >> channel:give(10) - >> channel:give(20) - >> channel:give(30) - >> channel:view() - = [10, 20, 30] - >> channel:peek() - = 10 - >> channel:peek(front=no) - = 30 + >> with_nums := mutexed [10, 20, 30] + with_nums(func(nums:&[Int]): + >> nums[] + = [10, 20, 30] + nums:insert(40) + ) + with_nums(func(nums:&[Int]): + >> nums[] + = [10, 20, 30, 40] + ) - >> channel:give(-10, front=yes) - >> channel:view() - = [-10, 10, 20, 30] + with_jobs := mutexed [Job.Increment(5)] + enqueue_job := func(job:Job): + with_jobs(func(jobs:&[Job]): jobs:insert(job)) + dequeue_job := func(): + job := @none:Job + with_jobs(func(jobs:&[Job]): job[] = jobs:pop(1)) + job[] - jobs := |:Job; max_size=2| - >> jobs:give(Increment(5)) - >> jobs:peek() - = Job.Increment(5) + with_results := mutexed [:Int] + enqueue_result := func(result:Int): + with_results(func(results:&[Int]): results:insert(result)) + dequeue_result := func(): + result := @none:Int + repeat: + with_results(func(results:&[Int]): result[] = results:pop(1)) + stop if result[] + sleep(0.00001) + result[]! - results := |:Int; max_size| >> thread := Thread.new(func(): !! In another thread! repeat: - >> got := jobs:get() - when got is Increment(x): - >> results:give(x+1) + job := dequeue_job() or stop + when job is Increment(x): + enqueue_result(x + 1) is Decrement(x): - >> results:give(x-1) + enqueue_result(x - 1) ) - >> jobs:give(Decrement(100)) - >> jobs:give(Decrement(100)) - >> jobs:give(Decrement(100)) - >> jobs:give(Decrement(100)) - >> jobs:give(Decrement(100)) - >> jobs:give(Decrement(100)) + enqueue_job(Decrement(100)) + enqueue_job(Decrement(200)) + enqueue_job(Decrement(300)) + enqueue_job(Decrement(400)) + enqueue_job(Decrement(500)) + enqueue_job(Decrement(600)) - >> results:get() - = 6 + >> enqueue_job(Increment(1000)) - >> jobs:give(Increment(1000)) - >> results:get() + >> dequeue_result() + = 6 + >> dequeue_result() = 99 - >> results:get() - = 99 - >> results:get() - = 99 - >> results:get() - = 99 - >> results:get() - = 99 - >> results:get() - = 99 + >> dequeue_result() + = 199 + >> dequeue_result() + = 299 + >> dequeue_result() + = 399 + >> dequeue_result() + = 499 + >> dequeue_result() + = 599 - >> results:get() + >> dequeue_result() = 1001 !! Canceling... |
