| 1 | package Thread; |
| 2 | |
| 3 | $VERSION = '2.00'; |
| 4 | |
| 5 | use strict; |
| 6 | |
| 7 | our $ithreads; |
| 8 | our $othreads; |
| 9 | |
| 10 | BEGIN { |
| 11 | use Config; |
| 12 | $ithreads = $Config{useithreads}; |
| 13 | $othreads = $Config{use5005threads}; |
| 14 | } |
| 15 | |
| 16 | require Exporter; |
| 17 | use XSLoader (); |
| 18 | our($VERSION, @ISA, @EXPORT, @EXPORT_OK); |
| 19 | |
| 20 | @ISA = qw(Exporter); |
| 21 | |
| 22 | BEGIN { |
| 23 | if ($ithreads) { |
| 24 | @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock) |
| 25 | } elsif ($othreads) { |
| 26 | @EXPORT_OK = qw(cond_signal cond_broadcast cond_wait); |
| 27 | } |
| 28 | push @EXPORT_OK, qw(async yield); |
| 29 | } |
| 30 | |
| 31 | =head1 NAME |
| 32 | |
| 33 | Thread - manipulate threads in Perl |
| 34 | |
| 35 | =head1 CAVEAT |
| 36 | |
| 37 | Perl has two thread models. |
| 38 | |
| 39 | In Perl 5.005 the thread model was that all data is implicitly shared |
| 40 | and shared access to data has to be explicitly synchronized. |
| 41 | This model is called "5005threads". |
| 42 | |
| 43 | In Perl 5.6 a new model was introduced in which all is was thread |
| 44 | local and shared access to data has to be explicitly declared. |
| 45 | This model is called "ithreads", for "interpreter threads". |
| 46 | |
| 47 | In Perl 5.6 the ithreads model was not available as a public API, |
| 48 | only as an internal API that was available for extension writers, |
| 49 | and to implement fork() emulation on Win32 platforms. |
| 50 | |
| 51 | In Perl 5.8 the ithreads model became available through the C<threads> |
| 52 | module. |
| 53 | |
| 54 | Neither model is configured by default into Perl (except, as mentioned |
| 55 | above, in Win32 ithreads are always available.) |
| 56 | |
| 57 | For backwards compatibility, the Thread module has been reworked |
| 58 | to function as a frontend for both 5005threads and ithreads. |
| 59 | Note that the compatibility is not complete: because the data sharing |
| 60 | models are directly opposed, anything to do with data sharing has to |
| 61 | be thought differently. With the ithreads you must explicitly share() |
| 62 | variables between the threads. |
| 63 | |
| 64 | Finally, note that there are many known serious problems with the |
| 65 | 5005threads, one of the least of which is that regular expression |
| 66 | match variables like $1 are not threadsafe, that is, they easily get |
| 67 | corrupted by competing threads. Other problems include more insidious |
| 68 | data corruption and mysterious crashes. You are seriously urged to |
| 69 | use ithreads instead. |
| 70 | |
| 71 | =head1 SYNOPSIS |
| 72 | |
| 73 | use Thread; |
| 74 | |
| 75 | my $t = Thread->new(\&start_sub, @start_args); |
| 76 | |
| 77 | $result = $t->join; |
| 78 | $result = $t->eval; |
| 79 | $t->detach; |
| 80 | |
| 81 | if ($t->done) { |
| 82 | $t->join; |
| 83 | } |
| 84 | |
| 85 | if($t->equal($another_thread)) { |
| 86 | # ... |
| 87 | } |
| 88 | |
| 89 | yield(); |
| 90 | |
| 91 | my $tid = Thread->self->tid; |
| 92 | |
| 93 | lock($scalar); |
| 94 | lock(@array); |
| 95 | lock(%hash); |
| 96 | |
| 97 | lock(\&sub); # not available with ithreads |
| 98 | |
| 99 | $flags = $t->flags; # not available with ithreads |
| 100 | |
| 101 | my @list = Thread->list; # not available with ithreads |
| 102 | |
| 103 | unlock(...); # not available with the 5.005 threads |
| 104 | |
| 105 | use Thread 'async'; |
| 106 | |
| 107 | =head1 DESCRIPTION |
| 108 | |
| 109 | The C<Thread> module provides multithreading support for perl. |
| 110 | |
| 111 | =head1 FUNCTIONS |
| 112 | |
| 113 | =over 8 |
| 114 | |
| 115 | =item $thread = Thread->new(\&start_sub) |
| 116 | |
| 117 | =item $thread = Thread->new(\&start_sub, LIST) |
| 118 | |
| 119 | C<new> starts a new thread of execution in the referenced subroutine. The |
| 120 | optional list is passed as parameters to the subroutine. Execution |
| 121 | continues in both the subroutine and the code after the C<new> call. |
| 122 | |
| 123 | C<Thread->new> returns a thread object representing the newly created |
| 124 | thread. |
| 125 | |
| 126 | =item lock VARIABLE |
| 127 | |
| 128 | C<lock> places a lock on a variable until the lock goes out of scope |
| 129 | (with ithreads you can also explicitly unlock()). |
| 130 | |
| 131 | If the variable is locked by another thread, the C<lock> call will |
| 132 | block until it's available. C<lock> is recursive, so multiple calls |
| 133 | to C<lock> are safe--the variable will remain locked until the |
| 134 | outermost lock on the variable goes out of scope. |
| 135 | |
| 136 | Locks on variables only affect C<lock> calls--they do I<not> affect normal |
| 137 | access to a variable. (Locks on subs are different, and covered in a bit.) |
| 138 | If you really, I<really> want locks to block access, then go ahead and tie |
| 139 | them to something and manage this yourself. This is done on purpose. |
| 140 | While managing access to variables is a good thing, Perl doesn't force |
| 141 | you out of its living room... |
| 142 | |
| 143 | If a container object, such as a hash or array, is locked, all the |
| 144 | elements of that container are not locked. For example, if a thread |
| 145 | does a C<lock @a>, any other thread doing a C<lock($a[12])> won't |
| 146 | block. |
| 147 | |
| 148 | With 5005threads you may also C<lock> a sub, using C<lock &sub>. |
| 149 | Any calls to that sub from another thread will block until the lock |
| 150 | is released. This behaviour is not equivalent to declaring the sub |
| 151 | with the C<locked> attribute. The C<locked> attribute serializes |
| 152 | access to a subroutine, but allows different threads non-simultaneous |
| 153 | access. C<lock &sub>, on the other hand, will not allow I<any> other |
| 154 | thread access for the duration of the lock. |
| 155 | |
| 156 | Finally, C<lock> will traverse up references exactly I<one> level. |
| 157 | C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not. |
| 158 | |
| 159 | =item async BLOCK; |
| 160 | |
| 161 | C<async> creates a thread to execute the block immediately following |
| 162 | it. This block is treated as an anonymous sub, and so must have a |
| 163 | semi-colon after the closing brace. Like C<Thread->new>, C<async> |
| 164 | returns a thread object. |
| 165 | |
| 166 | =item Thread->self |
| 167 | |
| 168 | The C<Thread-E<gt>self> function returns a thread object that represents |
| 169 | the thread making the C<Thread-E<gt>self> call. |
| 170 | |
| 171 | =item cond_wait VARIABLE |
| 172 | |
| 173 | The C<cond_wait> function takes a B<locked> variable as |
| 174 | a parameter, unlocks the variable, and blocks until another thread |
| 175 | does a C<cond_signal> or C<cond_broadcast> for that same locked |
| 176 | variable. The variable that C<cond_wait> blocked on is relocked |
| 177 | after the C<cond_wait> is satisfied. If there are multiple threads |
| 178 | C<cond_wait>ing on the same variable, all but one will reblock waiting |
| 179 | to reaquire the lock on the variable. (So if you're only using |
| 180 | C<cond_wait> for synchronization, give up the lock as soon as |
| 181 | possible.) |
| 182 | |
| 183 | =item cond_signal VARIABLE |
| 184 | |
| 185 | The C<cond_signal> function takes a locked variable as a parameter and |
| 186 | unblocks one thread that's C<cond_wait>ing on that variable. If more than |
| 187 | one thread is blocked in a C<cond_wait> on that variable, only one (and |
| 188 | which one is indeterminate) will be unblocked. |
| 189 | |
| 190 | If there are no threads blocked in a C<cond_wait> on the variable, |
| 191 | the signal is discarded. |
| 192 | |
| 193 | =item cond_broadcast VARIABLE |
| 194 | |
| 195 | The C<cond_broadcast> function works similarly to C<cond_signal>. |
| 196 | C<cond_broadcast>, though, will unblock B<all> the threads that are |
| 197 | blocked in a C<cond_wait> on the locked variable, rather than only |
| 198 | one. |
| 199 | |
| 200 | =item yield |
| 201 | |
| 202 | The C<yield> function allows another thread to take control of the |
| 203 | CPU. The exact results are implementation-dependent. |
| 204 | |
| 205 | =back |
| 206 | |
| 207 | =head1 METHODS |
| 208 | |
| 209 | =over 8 |
| 210 | |
| 211 | =item join |
| 212 | |
| 213 | C<join> waits for a thread to end and returns any values the thread |
| 214 | exited with. C<join> will block until the thread has ended, though |
| 215 | it won't block if the thread has already terminated. |
| 216 | |
| 217 | If the thread being C<join>ed C<die>d, the error it died with will |
| 218 | be returned at this time. If you don't want the thread performing |
| 219 | the C<join> to die as well, you should either wrap the C<join> in |
| 220 | an C<eval> or use the C<eval> thread method instead of C<join>. |
| 221 | |
| 222 | =item eval |
| 223 | |
| 224 | The C<eval> method wraps an C<eval> around a C<join>, and so waits for |
| 225 | a thread to exit, passing along any values the thread might have returned. |
| 226 | Errors, of course, get placed into C<$@>. (Not available with ithreads.) |
| 227 | |
| 228 | =item detach |
| 229 | |
| 230 | C<detach> tells a thread that it is never going to be joined i.e. |
| 231 | that all traces of its existence can be removed once it stops running. |
| 232 | Errors in detached threads will not be visible anywhere - if you want |
| 233 | to catch them, you should use $SIG{__DIE__} or something like that. |
| 234 | |
| 235 | =item equal |
| 236 | |
| 237 | C<equal> tests whether two thread objects represent the same thread and |
| 238 | returns true if they do. |
| 239 | |
| 240 | =item tid |
| 241 | |
| 242 | The C<tid> method returns the tid of a thread. The tid is |
| 243 | a monotonically increasing integer assigned when a thread is |
| 244 | created. The main thread of a program will have a tid of zero, |
| 245 | while subsequent threads will have tids assigned starting with one. |
| 246 | |
| 247 | =item flags |
| 248 | |
| 249 | The C<flags> method returns the flags for the thread. This is the |
| 250 | integer value corresponding to the internal flags for the thread, |
| 251 | and the value may not be all that meaningful to you. |
| 252 | (Not available with ithreads.) |
| 253 | |
| 254 | =item done |
| 255 | |
| 256 | The C<done> method returns true if the thread you're checking has |
| 257 | finished, and false otherwise. (Not available with ithreads.) |
| 258 | |
| 259 | =back |
| 260 | |
| 261 | =head1 LIMITATIONS |
| 262 | |
| 263 | The sequence number used to assign tids is a simple integer, and no |
| 264 | checking is done to make sure the tid isn't currently in use. If a |
| 265 | program creates more than 2**32 - 1 threads in a single run, threads |
| 266 | may be assigned duplicate tids. This limitation may be lifted in |
| 267 | a future version of Perl. |
| 268 | |
| 269 | =head1 SEE ALSO |
| 270 | |
| 271 | L<threads::shared> (not available with 5005threads) |
| 272 | |
| 273 | L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, |
| 274 | L<Thread::Specific> (not available with ithreads) |
| 275 | |
| 276 | =cut |
| 277 | |
| 278 | # |
| 279 | # Methods |
| 280 | # |
| 281 | |
| 282 | # |
| 283 | # Exported functions |
| 284 | # |
| 285 | |
| 286 | sub async (&) { |
| 287 | return Thread->new($_[0]); |
| 288 | } |
| 289 | |
| 290 | sub eval { |
| 291 | return eval { shift->join; }; |
| 292 | } |
| 293 | |
| 294 | sub unimplemented { |
| 295 | print $_[0], " unimplemented with ", |
| 296 | $Config{useithreads} ? "ithreads" : "5005threads", "\n"; |
| 297 | |
| 298 | } |
| 299 | |
| 300 | sub unimplement { |
| 301 | for my $m (@_) { |
| 302 | no strict 'refs'; |
| 303 | *{"Thread::$m"} = sub { unimplemented $m }; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | BEGIN { |
| 308 | if ($ithreads) { |
| 309 | XSLoader::load 'threads'; |
| 310 | for my $m (qw(new join detach yield self tid equal)) { |
| 311 | no strict 'refs'; |
| 312 | *{"Thread::$m"} = \&{"threads::$m"}; |
| 313 | } |
| 314 | XSLoader::load 'threads::shared'; |
| 315 | for my $m (qw(cond_signal cond_broadcast cond_wait unlock share)) { |
| 316 | no strict 'refs'; |
| 317 | *{"Thread::$m"} = \&{"threads::shared::${m}_enabled"}; |
| 318 | } |
| 319 | # trying to unimplement eval gives redefined warning |
| 320 | unimplement(qw(list done flags)); |
| 321 | } elsif ($othreads) { |
| 322 | XSLoader::load 'Thread'; |
| 323 | unimplement(qw(unlock)); |
| 324 | } else { |
| 325 | require Carp; |
| 326 | Carp::croak("This Perl has neither ithreads not 5005threads"); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | 1; |