was re: Coordination, composition, and coupling: Polyphonic C#

jbone at place.org jbone at place.org
Tue Oct 28 14:01:21 PST 2003


Compare and contrast.  From one of the aforementioned papers.  Complete 
implementations, totally thread-safe, with orchestration.

= with chords =

class Token {
    public Token(int initial tokens ) {
       for (int i = 0; i < initial tokens ; i++) Release();
    }
    public int Grab(int id ) & public async Release() {
       return id ;
    }
}

= without chords =

class Token {

    private const int mGrab = 1 << 0;
    private const int mRelease = 1 << 1;
    private threadQ GrabQ = new threadQ ();
    private voidQ ReleaseQ = new voidQ ();
    private const int mGrabRelease = mGrab | mRelease;
    private BitMask s = new BitMask ();
    private object mlock = GrabQ ;

    private void scan() {
       if (s .match(mGrabRelease)) GrabQ .wakeup();
    }

    public Token(int initial tokens ) {
       for (int i = 0; i < initial tokens ; i++) Release();
    }

    [OneWay] public void Release() {
       lock(mlock ) {
          ReleaseQ .add ();
          if (! s .match(mRelease)) {
             s . set (mRelease);
          scan ();
          }
       }
    }

    public int Grab(int id ) {
       Monitor .Enter (mlock );
       if (! s .match(mGrab)) goto now ;
       later :
          GrabQ .yield (mlock ); if (GrabQ .empty) s.clear (mGrab);
       now :
          if (s .match(mRelease)) {
             ReleaseQ .get (); if (ReleaseQ .empty) s.clear (mRelease);
             scan ();
             Monitor .Exit (mlock );
             {
                return id ; // source code for the chord
             }
          }else{
             s . set (mGrab); goto later ;
          }
       }
    }


NB:  I imagine the latter is slightly nastier than it needs to be --- 
don't shoot the messenger, I didn't write it --- but only slightly.  
Consider the expressivity of the former vs. the latter --- they are 
functionally equivalent.



More information about the FoRK mailing list