Changeset d5f5eb7 for doc/theses/colby_parsons_MMAth
- Timestamp:
- Aug 1, 2023, 10:52:16 PM (17 months ago)
- Branches:
- master
- Children:
- d54ede6
- Parents:
- 210c737
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/colby_parsons_MMAth/text/waituntil.tex
r210c737 rd5f5eb7 175 175 176 176 \begin{lrbox}{\myboxA} 177 \begin{lstlisting}[language=go,literate= ]177 \begin{lstlisting}[language=go,literate=,{moredelim={**[is][\color{red}]{@}{@}}}] 178 178 insert := make( chan int ) 179 179 remove := make( chan * int ) … … 182 182 count := 0 183 183 func in_buf( int val ) { 184 185 184 buffer <- val 185 count++ 186 186 } 187 187 func out_buf( int * ptr ) { 188 189 188 *ptr := <-buffer 189 count-- 190 190 } 191 191 func BoundedBuffer { 192 193 194 195 case i := <- insert: in_buf( i ) 196 case p := <- remove: out_buf( p ) 197 198 199 200 201 case i := <- insert: in_buf( i ) 202 203 204 205 206 case p := <- remove: out_buf( p ) 207 208 209 210 211 192 L: for { 193 if ( count < Size && count > 0 ) { 194 select { // wait for message 195 @case i := <- insert: in_buf( i )@ 196 @case p := <- remove: out_buf( p )@ 197 case <- done: break L 198 } 199 } else if ( count < Size ) { 200 select { // wait for message 201 @case i := <- insert: in_buf( i )@ 202 case <- done: break L 203 } 204 } else ( count > 0 ) { 205 select { // wait for message 206 @case p := <- remove: out_buf( p )@ 207 case <- done: break L; 208 } 209 } 210 } 211 done <- 0 212 212 } 213 213 func main() { 214 214 go BoundedBuffer() // start administrator 215 215 } 216 217 218 219 220 216 \end{lstlisting} 221 217 \end{lrbox} … … 223 219 \begin{lrbox}{\myboxB} 224 220 \begin{lstlisting}[language=uC++,{moredelim={**[is][\color{red}]{@}{@}}}] 221 222 223 224 225 226 227 228 229 230 231 232 225 233 _Task BoundedBuffer { 226 234 int * buffer; 227 235 int front = back = count = 0; 228 236 public: 229 237 // ... constructor implementation 230 238 void insert( int elem ) { 231 239 buffer[front] = elem; 232 240 front = ( front + 1 ) % Size; 233 241 count += 1; 234 242 } 235 243 int remove() { 236 244 int ret = buffer[back]; 237 245 back = ( back + 1 ) % Size; 238 246 count -= 1; 239 247 return ret; 240 248 } 241 249 private: … … 636 644 \begin{cfa} 637 645 bool pending_set_other( select_node & other, select_node & mine ) { 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 646 unsigned long int cmp_status = UNSAT; 647 648 // Try to set other status, if we succeed break and return true 649 while( ! CAS( other.clause_status, &cmp_status, SAT ) ) { 650 if ( cmp_status == SAT ) 651 return false; // If other status is SAT we lost so return false 652 653 // Toggle own status flag to allow other thread to potentially win 654 mine.status = UNSAT; 655 656 // Reset compare flag 657 cmp_status = UNSAT; 658 659 // Attempt to set own status flag back to PENDING to retry 660 if ( ! CAS( mine.clause_status, &cmp_status, PENDING ) ) 661 return false; // If we fail then we lost so return false 662 663 // Reset compare flag 664 cmp_status = UNSAT; 665 } 666 return true; 659 667 } 660 668 \end{cfa} … … 714 722 // statement completion predicate 715 723 bool check_completion( select_node * nodes ) { 716 724 return nodes[0].status && nodes[1].status || nodes[2].status; 717 725 } 718 726 if ( GA || GB || GC ) { $\C{// skip statement if all guards false}$ 719 720 721 722 723 727 select_node nodes[3]; 728 nodes[0].status = ! GA && GB; $\C{// A's status}$ 729 nodes[1].status = ! GB && GA; $\C{// B's status}$ 730 nodes[2].status = ! GC; $\C{// C's status}$ 731 // ... rest of waituntil codegen ... 724 732 } 725 733 \end{cfa} … … 772 780 773 781 if ( any when_conditions[node] are true ) { 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 782 select_nodes nodes[N]; $\C{// declare N select nodes}$ 783 try { 784 // ... set statuses for nodes with when_conditions[node] == false ... 785 786 for ( node in nodes ) $\C{// register nodes}$ 787 if ( when_conditions[node] ) 788 register_select( resource, node ); 789 790 while ( !check_completion( nodes ) ) { $\C{// check predicate}$ 791 // block 792 for ( resource in waituntil statement ) { $\C{// run true code blocks}$ 793 if ( check_completion( nodes ) ) break; 794 if ( resource is avail ) 795 try { 796 if( on_selected( resource ) ) $\C{// conditionally run block}$ 797 run code block 798 } finally $\C{// for exception safety}$ 799 unregister_select( resource, node ); $\C{// immediate unregister}$ 800 } 801 } 802 } finally { $\C{// for exception safety}$ 803 for ( registered nodes in nodes ) $\C{// deregister nodes}$ 804 if ( when_conditions[node] && unregister_select( resource, node ) 805 && on_selected( resource ) ) 806 run code block $\C{// run code block upon unregister}\CRT$ 807 } 800 808 } 801 809 \end{cfa}
Note: See TracChangeset
for help on using the changeset viewer.