Changeset d5f5eb7


Ignore:
Timestamp:
Aug 1, 2023, 10:52:16 PM (11 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
d54ede6
Parents:
210c737
Message:

fussed with Figure 7.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/colby_parsons_MMAth/text/waituntil.tex

    r210c737 rd5f5eb7  
    175175
    176176\begin{lrbox}{\myboxA}
    177 \begin{lstlisting}[language=go,literate=]
     177\begin{lstlisting}[language=go,literate=,{moredelim={**[is][\color{red}]{@}{@}}}]
    178178insert := make( chan int )
    179179remove := make( chan * int )
     
    182182count := 0
    183183func in_buf( int val ) {
    184     buffer <- val
    185     count++
     184        buffer <- val
     185        count++
    186186}
    187187func out_buf( int * ptr ) {
    188     *ptr := <-buffer
    189     count--
     188        *ptr := <-buffer
     189        count--
    190190}
    191191func BoundedBuffer {
    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
     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
    212212}
    213213func main() {
    214214        go BoundedBuffer() // start administrator
    215215}
    216 
    217 
    218 
    219 
    220216\end{lstlisting}
    221217\end{lrbox}
     
    223219\begin{lrbox}{\myboxB}
    224220\begin{lstlisting}[language=uC++,{moredelim={**[is][\color{red}]{@}{@}}}]
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
    225233_Task BoundedBuffer {
    226234        int * buffer;
    227235        int front = back = count = 0;
    228236  public:
    229     // ... constructor implementation
     237        // ... constructor implementation
    230238        void insert( int elem ) {
    231239                buffer[front] = elem;
    232         front = ( front + 1 ) % Size;
     240                front = ( front + 1 ) % Size;
    233241                count += 1;
    234242        }
    235243        int remove() {
    236244                int ret = buffer[back];
    237         back = ( back + 1 ) % Size;
     245                back = ( back + 1 ) % Size;
    238246                count -= 1;
    239         return ret;
     247                return ret;
    240248        }
    241249  private:
     
    636644\begin{cfa}
    637645bool pending_set_other( select_node & other, select_node & mine ) {
    638     unsigned long int cmp_status = UNSAT;
    639 
    640     // Try to set other status, if we succeed break and return true
    641     while( ! CAS( other.clause_status, &cmp_status, SAT ) ) {
    642         if ( cmp_status == SAT )
    643             return false; // If other status is SAT we lost so return false
    644 
    645         // Toggle own status flag to allow other thread to potentially win
    646         mine.status = UNSAT;
    647 
    648         // Reset compare flag
    649         cmp_status = UNSAT;
    650 
    651         // Attempt to set own status flag back to PENDING to retry
    652         if ( ! CAS( mine.clause_status, &cmp_status, PENDING ) )
    653             return false; // If we fail then we lost so return false
    654        
    655         // Reset compare flag
    656         cmp_status = UNSAT;
    657     }
    658     return true;
     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;
    659667}
    660668\end{cfa}
     
    714722// statement completion predicate
    715723bool check_completion( select_node * nodes ) {
    716     return nodes[0].status && nodes[1].status || nodes[2].status;
     724        return nodes[0].status && nodes[1].status || nodes[2].status;
    717725}
    718726if ( GA || GB || GC ) {                         $\C{// skip statement if all guards false}$
    719     select_node nodes[3];
    720     nodes[0].status = ! GA && GB;       $\C{// A's status}$
    721     nodes[1].status = ! GB && GA;       $\C{// B's status}$
    722     nodes[2].status = ! GC;                     $\C{// C's status}$
    723     // ... rest of waituntil codegen ...
     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 ...
    724732}
    725733\end{cfa}
     
    772780
    773781if ( any when_conditions[node] are true ) {
    774     select_nodes nodes[N];                                                                      $\C{// declare N select nodes}$
    775     try {
    776         // ... set statuses for nodes with when_conditions[node] == false ...
    777 
    778         for ( node in nodes )                                                           $\C{// register nodes}$
    779             if ( when_conditions[node] )
    780                 register_select( resource, node );
    781 
    782         while ( !check_completion( nodes ) ) {  $\C{// check predicate}$
    783             // block
    784             for ( resource in waituntil statement ) {   $\C{// run true code blocks}$
    785                 if ( check_completion( nodes ) ) break;
    786                 if ( resource is avail )
    787                     try {
    788                         if( on_selected( resource ) )   $\C{// conditionally run block}$
    789                             run code block
    790                     } finally                                                   $\C{// for exception safety}$
    791                         unregister_select( resource, node ); $\C{// immediate unregister}$
    792             }
    793         }
    794     } finally {                                                                                 $\C{// for exception safety}$
    795         for ( registered nodes in nodes )                                       $\C{// deregister nodes}$
    796             if ( when_conditions[node] && unregister_select( resource, node )
    797                     && on_selected( resource ) )
    798                 run code block                                                  $\C{// run code block upon unregister}\CRT$
    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        }
    800808}
    801809\end{cfa}
Note: See TracChangeset for help on using the changeset viewer.