[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/linuxaux/opt/perl/lib/5.10.0/Thread/ -> Semaphore.pm (source)

   1  package Thread::Semaphore;
   2  
   3  use threads::shared;
   4  
   5  our $VERSION = '2.01';
   6  
   7  =head1 NAME
   8  
   9  Thread::Semaphore - thread-safe semaphores
  10  
  11  =head1 SYNOPSIS
  12  
  13      use Thread::Semaphore;
  14      my $s = new Thread::Semaphore;
  15      $s->down;    # Also known as the semaphore P operation.
  16      # The guarded section is here
  17      $s->up;    # Also known as the semaphore V operation.
  18  
  19      # The default semaphore value is 1.
  20      my $s = new Thread::Semaphore($initial_value);
  21      $s->down($down_value);
  22      $s->up($up_value);
  23  
  24  =head1 DESCRIPTION
  25  
  26  Semaphores provide a mechanism to regulate access to resources. Semaphores,
  27  unlike locks, aren't tied to particular scalars, and so may be used to
  28  control access to anything you care to use them for.
  29  
  30  Semaphores don't limit their values to zero or one, so they can be used to
  31  control access to some resource that there may be more than one of. (For
  32  example, filehandles.) Increment and decrement amounts aren't fixed at one
  33  either, so threads can reserve or return multiple resources at once.
  34  
  35  =head1 FUNCTIONS AND METHODS
  36  
  37  =over 8
  38  
  39  =item new
  40  
  41  =item new NUMBER
  42  
  43  C<new> creates a new semaphore, and initializes its count to the passed
  44  number. If no number is passed, the semaphore's count is set to one.
  45  
  46  =item down
  47  
  48  =item down NUMBER
  49  
  50  The C<down> method decreases the semaphore's count by the specified number,
  51  or by one if no number has been specified. If the semaphore's count would drop
  52  below zero, this method will block until such time that the semaphore's
  53  count is equal to or larger than the amount you're C<down>ing the
  54  semaphore's count by.
  55  
  56  This is the semaphore "P operation" (the name derives from the Dutch
  57  word "pak", which means "capture" -- the semaphore operations were
  58  named by the late Dijkstra, who was Dutch).
  59  
  60  =item up
  61  
  62  =item up NUMBER
  63  
  64  The C<up> method increases the semaphore's count by the number specified,
  65  or by one if no number has been specified. This will unblock any thread blocked
  66  trying to C<down> the semaphore if the C<up> raises the semaphore count
  67  above the amount that the C<down>s are trying to decrement it by.
  68  
  69  This is the semaphore "V operation" (the name derives from the Dutch
  70  word "vrij", which means "release").
  71  
  72  =back
  73  
  74  =cut
  75  
  76  sub new {
  77      my $class = shift;
  78      my $val : shared = @_ ? shift : 1;
  79      bless \$val, $class;
  80  }
  81  
  82  sub down {
  83      my $s = shift;
  84      lock($$s);
  85      my $inc = @_ ? shift : 1;
  86      cond_wait $$s until $$s >= $inc;
  87      $$s -= $inc;
  88  }
  89  
  90  sub up {
  91      my $s = shift;
  92      lock($$s);
  93      my $inc = @_ ? shift : 1;
  94      ($$s += $inc) > 0 and cond_broadcast $$s;
  95  }
  96  
  97  1;


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1