[omniORB] Synchronized access to objects

Duncan Grisby duncan@grisby.org
Mon Mar 31 12:08:00 2003


On Thursday 27 March, Mike Mascari wrote:

[...]
>    I was hoping 
> someone would know how I might synchronize instance-specific 
> calls *before* the call is dispatched to the servant?

Unless you create one POA per object, which is probably not an
especially good idea, there is no way to do it within the ORB. The
common pattern is to implement your servant class so it has a "real"
implementation of each method, that assumes the lock is already held,
and small methods that implement the IDL interface by acquiring the
lock then calling the real method:

  // IDL
  interface Example {
    void one();
    void two();
  };

  // C++
  class Example_i : public POA_Example, public RefCountServantBase {
  public:
    void one() {
      omni_mutex_lock sync(mu_);
      real_one();
    }
    void two() {
      omni_mutex_lock sync(mu_);
      real_two();
    }
  private:
    void real_one() {
      // ...
      real_two()
      // ...
    }
    void real_two() {
      // ...
    }

    // Mutex to protect state
    omni_mutex mu_;
  };


Cheers,

Duncan.

-- 
 -- Duncan Grisby         --
  -- duncan@grisby.org     --
   -- http://www.grisby.org --