Permalink
Browse files

Add Semaphore.pm

  • Loading branch information...
tadzik committed Dec 18, 2012
1 parent 11ac077 commit b86b6e8011f0f0f86f0670eea0a89c9da98df41e
Showing with 31 additions and 0 deletions.
  1. +31 −0 lib/Semaphore.pm
View
@@ -0,0 +1,31 @@
+use Threads;
+
+class Semaphore {
+ has $.value;
+
+ method wait {
+ async(sub {
+ loop {
+ pir::disable_preemption__0;
+ if $!value > 0 {
+ $!value--;
+ last;
+ }
+ pir::enable_preemption__0;
+ pir::pass__0();
+ 1;
+ }
+ pir::enable_preemption__0;
+ return;
+ }).join;
+ }
+
+ method post {
+ async(sub {
+ pir::disable_preemption__0;
+ $!value++;
+ pir::enable_preemption__0;
+ return;
+ }).join
+ }
+}

0 comments on commit b86b6e8

Please sign in to comment.