swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: copy protocol and object ravioli


From: Manor Askenazi
Subject: Re: copy protocol and object ravioli
Date: Thu, 13 Feb 1997 16:21:25 -0800

   Clone: Is the Ravioli really that bad?
   --------------------------------------

I may be underestimating the problem here but wouldn't the following
simple heuristic do the trick?

If object A is asked to clone itself it makes a copy of its basic size
(i.e. something to hold its instance variables etc.). Then, for every
instance variable V we ask the simple question:

                 *** The 'Parmesan' Principle ***

   << http://www.epicurious.com/db/dictionary/terms/p/parmesan.html >>

   Do I own V (am I the only one who has a reference to this) or not?
   [OMT even has special notation for this relation... I think its called
   unique containment...]

If A is the only owner, then recursively call V's copy method and put the
result in the clone's (A') slot. If not, then the shared object V **must**
have some standard mechanism for observer tracking (which may be implicit
in the case of garbage collecting systems but never mind that right now).
In which case tell A', when it is made, that it should register itself
with the instance variable somehow:

  //Note: this is 'real' Java ... Cloneable is an actual JDK interface...

  public class AClass implements Cloneable {

    private UniqueWidget v1 ;
    private SharedWidget v2 ;

    public AClass clone() {
      AClass A' = new AClass() ;

      A'.v1 = this.v1.clone() ;   //I assume for the sake of clarity that
                                  //v1's clone() always succeeds... But v1
                                  //could contain shared objects etc.

      try{
                                       //   *** Register with v2 ***
        A'.attemptToRegisterWith(v2) ; //Just because I can talk to v2 duznt
                                       //mean A' will be given the same priv...
      } catch(FailedRegistrationException e) {
        //If A' can't make its own v2 it will whine and we need to whine too!
        cry_a_little() ;
        throw e ;
      }
    }
  }

Voila!!! In fact, with appropriate notation this sequence could be
compiler generated... [Insert the usual compiler-writer excitement
here.... :->] BTW Java does support this approach somewhat in that,
the run time system throws a CloneNotSupportedException if you send
a clone() message to an object which does not implement clone()
(i.e. you are strongly expected to implement a clone() implementation
for all of your objects)...

In any case, what I'm trying to say boils down to this:

If an object doesn't know, at compile time, what subset of its contents
is shared (in principle), then the programmer is not thinking things
through - which is negligent in the old fashioned single-threaded world,
but is downright criminal when you have threads.

...Well, there must be some major source of complexity that I'm overlooking
here, right? So, what am I forgetting?

Manor.

PS> I'm forgetting something in the sauce, right? :*)

-- 
Manor Askenazi


reply via email to

[Prev in Thread] Current Thread [Next in Thread]