Changeset 14496 for docs/Perl6/Spec
- Timestamp:
- 10/24/06 17:20:23 (2 years ago)
- svk:copy_cache_prev:
- 21659
- Files:
-
- 1 modified
-
docs/Perl6/Spec/Concurrency.pod (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
docs/Perl6/Spec/Concurrency.pod
r12127 r14496 103 103 the BEGIN TRANSACTION was executed. 104 104 105 Perl 6 supports this concept through Code blocks which are marked "is atomic". 105 Perl 6 supports this concept through C<contend> blocks. 106 106 107 These sections are guaranteed to either be completed totally (when the Code 107 108 block is exited), or have their state reverted to the state at the start of 108 the Code block (with the L< retry> statement).109 the Code block (with the L<defer> statement). 109 110 110 111 (EM: maybe point out if / how old style locks can be "simulated", for those … … 114 115 115 116 my ($x, $y); 116 sub c is atomic{117 sub c { contend { 117 118 $x -= 3; 118 119 $y += 3; 119 if $x < 10 { retry } 120 }; 121 122 $e = &c.retry_with( &d ); # 123 $e(); 124 125 if $i { is atomic; ... } else { ...; } 126 127 A Code block can be marked as "is atomic". This means that code executed 120 if $x < 10 { defer } 121 } }; 122 123 maybe { c() } maybe { d() }; 124 125 if $i { contend { ...} } else { ...; } 126 127 A Code block can be prefixed with C<contend>. This means that code executed 128 128 inside that scope is guaranteed not to be interrupted in any way. 129 129 130 The start of a block marked "is atomic" also becomes a "checkpoint"to130 The start of a block marked C<contend> also becomes a I<checkpoint> to 131 131 which execution can return (in exactly the same state) if a problem occurs 132 (a.k.a. a L< retry> is done) inside the scope of the Code block.133 134 =head3 retry135 136 The C< retry> function basically restores the state of the thread at the132 (a.k.a. a L<defer> is done) inside the scope of the Code block. 133 134 =head3 defer 135 136 The C<defer> function basically restores the state of the thread at the 137 137 last checkpoint and will wait there until an external event allows it to 138 potentially run that atomic section of code again without having to retry139 again.138 potentially run that atomic C<contend> section of code again without having 139 to defer again. 140 140 141 141 If there are no external events possible that could restart execution, an 142 142 exception will be raised. 143 143 144 The last checkpoint is either the last atomic / non-atomic boundary, or 145 the most immediate caller constructed with C<retry_with>. 146 147 =head3 retry_with 148 149 The C<retry_with> method on an atomic Code object causes a checkpoint to 150 be made for C<retry>, creating an alternate execution path to be followed 151 when a C<retry> is done. 144 The last checkpoint is either the outermose C<contend> boundary, or 145 the most immediate caller constructed with C<maybe>. 146 147 =head3 maybe 148 149 The C<maybe> statement causes a checkpoint to be made for C<defer> for 150 each block in the C<maybe> chain, creating an alternate execution path to 151 be followed when a C<defer> is done. For example: 152 153 maybe { 154 ... 155 some_condition() or defer; 156 ... 157 } maybe { 158 ... 159 some_other_condition() or defer; 160 ... 161 } maybe { 162 ... 163 } 164 165 If placed outside a C<contend> block, the C<maybe> statement creates its 166 own C<contend> barrier. 152 167 153 168 =head3 limitations … … 180 195 A Code block marked "is critical" can not be interrupted in any way. But 181 196 since it is able to access non-revertible data structures (such as 182 non-seekable file handles), it cannot do a C< retry> as it would be impossible197 non-seekable file handles), it cannot do a C<defer> as it would be impossible 183 198 to restore the state to the beginning of the Code block. 184 199 … … 294 309 # if the counter with the "key" equals or exceeds a closure's limit, 295 310 # the closure can't be entered until it's released 296 # (this can be trivially implmented using atomic+retry)311 # (this can be trivially implmented using contend+defer) 297 312 } 298 313 … … 328 343 { 329 344 330 is atomic; # retry/orelse/whatever other rollback stuff345 is atomic; # contend/maybe/whatever other rollback stuff 331 346 # limitation: no external IO (without lethal warnings anyway) 332 347 # can't do anything irreversible
