home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / apache / files / ActivePerl-5.6.1.638-MSWin32-x86.msi / _6f11300e7db101ed4c9961b69917b78f < prev    next >
Encoding:
Text File  |  2004-04-13  |  2.3 KB  |  90 lines

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