From b025cf269d2e07e179be4a0e34d936862dc640c2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 2 Jan 2025 20:29:55 -0500 Subject: Use `holding` blocks for mutexed data instead of lambdas --- test/optionals.tm | 20 ++++++++++++++++++++ test/threads.tm | 56 ++++++++++++++++++++++++------------------------------- 2 files changed, 44 insertions(+), 32 deletions(-) (limited to 'test') diff --git a/test/optionals.tm b/test/optionals.tm index 13a8dcae..3ef7f28e 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -67,6 +67,12 @@ func maybe_thread(should_i:Bool->Thread?): else: return none +func maybe_mutexed(should_i:Bool->mutexed(Bool)?): + if should_i: + return mutexed no + else: + return none + func main(): >> 5? = 5 : Int? @@ -251,6 +257,20 @@ func main(): fail("Truthy: $nope") else: !! Falsey: $nope + do: + !! ... + !! Mutexed: + >> yep := maybe_mutexed(yes) + # No "=" test here because threads use addresses in the text version + >> nope := maybe_mutexed(no) + = none : mutexed(Bool)? + >> if yep: >> yep + else: fail("Falsey: $yep") + >> if nope: + fail("Truthy: $nope") + else: !! Falsey: $nope + + if yep := maybe_int(yes): >> yep = 123 : Int diff --git a/test/threads.tm b/test/threads.tm index 772c866f..adf7784a 100644 --- a/test/threads.tm +++ b/test/threads.tm @@ -2,55 +2,47 @@ enum Job(Increment(x:Int), Decrement(x:Int)) func main(): do: - >> with_nums := mutexed [10, 20, 30] - with_nums(func(nums:&[Int]): + >> nums := mutexed [10, 20, 30] + holding nums: >> nums[] = [10, 20, 30] nums:insert(40) - ) - with_nums(func(nums:&[Int]): + + holding nums: >> nums[] = [10, 20, 30, 40] - ) - 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 := mutexed [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 := mutexed [:Int] >> thread := Thread.new(func(): !! In another thread! repeat: - job := dequeue_job() or stop + job := holding jobs: (jobs:pop(1) or stop) when job is Increment(x): - enqueue_result(x + 1) + holding results: results:insert(x + 1) is Decrement(x): - enqueue_result(x - 1) + holding results: results:insert(x - 1) ) - enqueue_job(Decrement(100)) - enqueue_job(Decrement(200)) - enqueue_job(Decrement(300)) - enqueue_job(Decrement(400)) - enqueue_job(Decrement(500)) - enqueue_job(Decrement(600)) + holding jobs: + jobs:insert(Decrement(100)) + jobs:insert(Decrement(200)) + jobs:insert(Decrement(300)) + jobs:insert(Decrement(400)) + jobs:insert(Decrement(500)) + jobs:insert(Decrement(600)) + jobs:insert(Increment(1000)) - >> enqueue_job(Increment(1000)) + dequeue_result := func(): + result := none:Int + repeat: + result = (holding results: results:pop(1)) + stop if result + sleep(0.00001) + return result! >> dequeue_result() = 6 -- cgit v1.2.3