diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-12-06 13:21:56 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-12-08 19:38:18 +0100 |
| commit | 14fe80c171367cdeb2afc430c541953350058f2c (patch) | |
| tree | 305ea628aee839bd2b64b56c88a96b531626f906 /challenge-142 | |
| parent | db3b628498be8e9548a5f36536ac72cd8fc76339 (diff) | |
| download | perlweeklychallenge-club-14fe80c171367cdeb2afc430c541953350058f2c.tar.gz perlweeklychallenge-club-14fe80c171367cdeb2afc430c541953350058f2c.tar.bz2 perlweeklychallenge-club-14fe80c171367cdeb2afc430c541953350058f2c.zip | |
Solution to task 2
Diffstat (limited to 'challenge-142')
| -rwxr-xr-x | challenge-142/jo-37/perl/ch-2.pl | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/challenge-142/jo-37/perl/ch-2.pl b/challenge-142/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..cb1491bfd1 --- /dev/null +++ b/challenge-142/jo-37/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use AnyEvent; +use Coro; +use Coro::AnyEvent; + +use experimental 'signatures'; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [I1 I2 ...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + enable trace output + +I1 I2 ... + nonnegative integers to be sorted + +EOS + + +### Input and Output + +say "@{sleep_sort(\@ARGV)}"; + + +### Implementation + +# Coros, as cooperative threads, generally do not run in parallel, thus +# no locking is required when accessing shared data. Using a semaphore +# for flow control: by waiting for the "ready" semaphore to become +# available, the main thread allows the async coros to be scheduled and +# regains control when all have finished with the same call. +# Note: The sleep function is event-driven and will not block the +# overall processing. +sub sleep_sort ($arr) { + # Collect sorted result here: + my @sorted; + # Will be unlocked when all threads have finished: + my $ready = Coro::Semaphore->new(1 - @$arr); + # Create threads. + async { + my ($ready, $sorted, $time) = @_; + die "time machine not implemented" if $time < 0; + say "sleep for $time s" if $verbose; + Coro::AnyEvent::sleep $time; + say "woke up after $time s" if $verbose; + push @$sorted, $time; + $ready->up; + } $ready, \@sorted, $_ for @$arr; + # Start processing and wait for async threads. + $ready->down; + + \@sorted; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples"; + } + + SKIP: { + skip "tests" unless $tests; + is sleep_sort([3, 1, 2, 1]), [1, 1, 2, 3], 'simple test'; + + local $verbose; + is sleep_sort([2, (1) x 9999]), + array {item 9999 => 2; etc;}, 'check last element'; + } + + done_testing; + exit; +} |
