Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.3 KiB

  1. package Thread;
  2. require Exporter;
  3. use XSLoader ();
  4. our($VERSION, @ISA, @EXPORT);
  5. $VERSION = "1.0";
  6. @ISA = qw(Exporter);
  7. @EXPORT_OK = qw(yield cond_signal cond_broadcast cond_wait async);
  8. =head1 NAME
  9. Thread - manipulate threads in Perl (EXPERIMENTAL, subject to change)
  10. =head1 CAVEAT
  11. The Thread extension requires Perl to be built in a particular way to
  12. enable the older 5.005 threading model. Just to confuse matters, there
  13. is an alternate threading model known as "ithreads" that does NOT
  14. support this extension. If you are using a binary distribution such
  15. as ActivePerl that is built with ithreads support, this extension CANNOT
  16. be used.
  17. =head1 SYNOPSIS
  18. use Thread;
  19. my $t = new Thread \&start_sub, @start_args;
  20. $result = $t->join;
  21. $result = $t->eval;
  22. $t->detach;
  23. if($t->equal($another_thread)) {
  24. # ...
  25. }
  26. my $tid = Thread->self->tid;
  27. my $tlist = Thread->list;
  28. lock($scalar);
  29. yield();
  30. use Thread 'async';
  31. =head1 DESCRIPTION
  32. WARNING: Threading is an experimental feature. Both the interface
  33. and implementation are subject to change drastically. In fact, this
  34. documentation describes the flavor of threads that was in version
  35. 5.005. Perl 5.6.0 and later have the beginnings of support for
  36. interpreter threads, which (when finished) is expected to be
  37. significantly different from what is described here. The information
  38. contained here may therefore soon be obsolete. Use at your own risk!
  39. The C<Thread> module provides multithreading support for perl.
  40. =head1 FUNCTIONS
  41. =over 8
  42. =item new \&start_sub
  43. =item new \&start_sub, LIST
  44. C<new> starts a new thread of execution in the referenced subroutine. The
  45. optional list is passed as parameters to the subroutine. Execution
  46. continues in both the subroutine and the code after the C<new> call.
  47. C<new Thread> returns a thread object representing the newly created
  48. thread.
  49. =item lock VARIABLE
  50. C<lock> places a lock on a variable until the lock goes out of scope. If
  51. the variable is locked by another thread, the C<lock> call will block until
  52. it's available. C<lock> is recursive, so multiple calls to C<lock> are
  53. safe--the variable will remain locked until the outermost lock on the
  54. variable goes out of scope.
  55. Locks on variables only affect C<lock> calls--they do I<not> affect normal
  56. access to a variable. (Locks on subs are different, and covered in a bit)
  57. If you really, I<really> want locks to block access, then go ahead and tie
  58. them to something and manage this yourself. This is done on purpose. While
  59. managing access to variables is a good thing, perl doesn't force you out of
  60. its living room...
  61. If a container object, such as a hash or array, is locked, all the elements
  62. of that container are not locked. For example, if a thread does a C<lock
  63. @a>, any other thread doing a C<lock($a[12])> won't block.
  64. You may also C<lock> a sub, using C<lock &sub>. Any calls to that sub from
  65. another thread will block until the lock is released. This behaviour is not
  66. equivalent to declaring the sub with the C<locked> attribute. The C<locked>
  67. attribute serializes access to a subroutine, but allows different threads
  68. non-simultaneous access. C<lock &sub>, on the other hand, will not allow
  69. I<any> other thread access for the duration of the lock.
  70. Finally, C<lock> will traverse up references exactly I<one> level.
  71. C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
  72. =item async BLOCK;
  73. C<async> creates a thread to execute the block immediately following
  74. it. This block is treated as an anonymous sub, and so must have a
  75. semi-colon after the closing brace. Like C<new Thread>, C<async> returns a
  76. thread object.
  77. =item Thread->self
  78. The C<Thread-E<gt>self> function returns a thread object that represents
  79. the thread making the C<Thread-E<gt>self> call.
  80. =item Thread->list
  81. C<Thread-E<gt>list> returns a list of thread objects for all running and
  82. finished but un-C<join>ed threads.
  83. =item cond_wait VARIABLE
  84. The C<cond_wait> function takes a B<locked> variable as a parameter,
  85. unlocks the variable, and blocks until another thread does a C<cond_signal>
  86. or C<cond_broadcast> for that same locked variable. The variable that
  87. C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
  88. If there are multiple threads C<cond_wait>ing on the same variable, all but
  89. one will reblock waiting to reaquire the lock on the variable. (So if
  90. you're only using C<cond_wait> for synchronization, give up the lock as
  91. soon as possible)
  92. =item cond_signal VARIABLE
  93. The C<cond_signal> function takes a locked variable as a parameter and
  94. unblocks one thread that's C<cond_wait>ing on that variable. If more than
  95. one thread is blocked in a C<cond_wait> on that variable, only one (and
  96. which one is indeterminate) will be unblocked.
  97. If there are no threads blocked in a C<cond_wait> on the variable, the
  98. signal is discarded.
  99. =item cond_broadcast VARIABLE
  100. The C<cond_broadcast> function works similarly to C<cond_signal>.
  101. C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
  102. in a C<cond_wait> on the locked variable, rather than only one.
  103. =item yield
  104. The C<yield> function allows another thread to take control of the
  105. CPU. The exact results are implementation-dependent.
  106. =back
  107. =head1 METHODS
  108. =over 8
  109. =item join
  110. C<join> waits for a thread to end and returns any values the thread exited
  111. with. C<join> will block until the thread has ended, though it won't block
  112. if the thread has already terminated.
  113. If the thread being C<join>ed C<die>d, the error it died with will be
  114. returned at this time. If you don't want the thread performing the C<join>
  115. to die as well, you should either wrap the C<join> in an C<eval> or use the
  116. C<eval> thread method instead of C<join>.
  117. =item eval
  118. The C<eval> method wraps an C<eval> around a C<join>, and so waits for a
  119. thread to exit, passing along any values the thread might have returned.
  120. Errors, of course, get placed into C<$@>.
  121. =item detach
  122. C<detach> tells a thread that it is never going to be joined i.e.
  123. that all traces of its existence can be removed once it stops running.
  124. Errors in detached threads will not be visible anywhere - if you want
  125. to catch them, you should use $SIG{__DIE__} or something like that.
  126. =item equal
  127. C<equal> tests whether two thread objects represent the same thread and
  128. returns true if they do.
  129. =item tid
  130. The C<tid> method returns the tid of a thread. The tid is a monotonically
  131. increasing integer assigned when a thread is created. The main thread of a
  132. program will have a tid of zero, while subsequent threads will have tids
  133. assigned starting with one.
  134. =back
  135. =head1 LIMITATIONS
  136. The sequence number used to assign tids is a simple integer, and no
  137. checking is done to make sure the tid isn't currently in use. If a program
  138. creates more than 2^32 - 1 threads in a single run, threads may be assigned
  139. duplicate tids. This limitation may be lifted in a future version of Perl.
  140. =head1 SEE ALSO
  141. L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, L<Thread::Specific>.
  142. =cut
  143. #
  144. # Methods
  145. #
  146. #
  147. # Exported functions
  148. #
  149. sub async (&) {
  150. return new Thread $_[0];
  151. }
  152. sub eval {
  153. return eval { shift->join; };
  154. }
  155. XSLoader::load 'Thread';
  156. 1;