From c1a438953e01786b7cade7cc2e10bcd7c008cf05 Mon Sep 17 00:00:00 2001 From: tworm Date: Thu, 18 Jun 1998 15:32:11 +0000 Subject: Correted formating. Cleaned up code. Changed ACE_Token to allow a different thread to relesse the thread --- TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.cpp | 166 ++++++++++----------- TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.h | 48 +++--- .../orbsvcs/Concurrency/CC_LockSetFactory.cpp | 23 +-- .../orbsvcs/Concurrency/CC_LockSetFactory.h | 10 +- 4 files changed, 116 insertions(+), 131 deletions(-) (limited to 'TAO/orbsvcs/orbsvcs/Concurrency') diff --git a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.cpp b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.cpp index 92c5d0ff56a..584faf2a210 100644 --- a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.cpp +++ b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.cpp @@ -23,12 +23,12 @@ CC_LockSet::CC_LockSet (void) { TAO_TRY { - this->Init(TAO_TRY_ENV); + this->Init (TAO_TRY_ENV); TAO_CHECK_ENV; } TAO_CATCHANY { - TAO_TRY_ENV.print_exception("CC_LockSet::CC_LockSet(void)"); + TAO_TRY_ENV.print_exception ("CC_LockSet::CC_LockSet (void)"); } TAO_ENDTRY; } @@ -40,12 +40,12 @@ CC_LockSet::CC_LockSet (CosConcurrencyControl::LockSet_ptr related) { TAO_TRY { - this->Init(TAO_TRY_ENV); + this->Init (TAO_TRY_ENV); TAO_CHECK_ENV; } TAO_CATCHANY { - TAO_TRY_ENV.print_exception("CC_LockSet::CC_LockSet(...)"); + TAO_TRY_ENV.print_exception ("CC_LockSet::CC_LockSet (...)"); } TAO_ENDTRY; } @@ -53,13 +53,8 @@ CC_LockSet::CC_LockSet (CosConcurrencyControl::LockSet_ptr related) // Initialization. void -CC_LockSet::Init(CORBA::Environment &_env) +CC_LockSet::Init (CORBA::Environment &_env) { - // Create the lock for serialization - _env.clear(); - _env.exception(new CORBA::NO_MEMORY (CORBA::COMPLETED_NO)); - ACE_NEW(mlock_, ACE_Thread_Mutex); - _env.clear(); // Set the mode of the statically allocated locks lock_[CC_IR] = 0; lock_[CC_R] = 0; @@ -68,7 +63,7 @@ CC_LockSet::Init(CORBA::Environment &_env) lock_[CC_W] = 0; // Acquire the semaphore in order to be able to put requests on hold - if(semaphore_.acquire()==-1) + if (semaphore_.acquire () == -1) TAO_THROW (CORBA::INTERNAL (CORBA::COMPLETED_NO)); } @@ -76,25 +71,17 @@ CC_LockSet::Init(CORBA::Environment &_env) CC_LockSet::~CC_LockSet (void) { - if(this->mlock_!=0) - delete mlock_; } // Returns true if the requested lock mode is compatible with the // modes held. False otherwise. -CORBA::Boolean CC_LockSet::compatible(CC_LockModeEnum mr) -{ - int i=CC_IR; - while(i<=CC_W) - { - if(this->lock_[i]>0) - if(this->compatible_[i][mr]==CORBA::B_FALSE) - { - return CORBA::B_FALSE; - } - i++; - } +CORBA::Boolean CC_LockSet::compatible (CC_LockModeEnum mr) +{ + for (size_t i = CC_IR; i <= CC_W; i++) + if (this->lock_[i] > 0) + if (this->compatible_[i][mr] == CORBA::B_FALSE) + return CORBA::B_FALSE; return CORBA::B_TRUE; } @@ -107,13 +94,15 @@ CC_LockSet::lock (CosConcurrencyControl::lock_mode mode, { ACE_DEBUG ((LM_DEBUG, "CC_LockSet::lock\n")); - CC_LockModeEnum lm = lmconvert(mode); + CC_LockModeEnum lm = lmconvert (mode); - // Check to see if the requested mode is compatible with the - // modes held so far. If not put the request on hold. + // Check to see if the requested mode is compatible with the modes + // held so far. If not put the request on hold. - if(this->lock_d(lm)==1) - if(semaphore_.acquire()==-1) + // @@ It's important to document somewhere that this code relies on + // the FIFO properties of ACE_Token! + if (this->lock_i (lm) == 1) + if (semaphore_.acquire () == -1) TAO_THROW (CORBA::INTERNAL (CORBA::COMPLETED_NO)); } @@ -123,11 +112,12 @@ CORBA::Boolean CC_LockSet::try_lock (CosConcurrencyControl::lock_mode mode, CORBA::Environment &_env) { - CC_LockModeEnum lm = lmconvert(mode); + CC_LockModeEnum lm = lmconvert (mode); ACE_DEBUG ((LM_DEBUG, "CC_LockSet::try_lock\n")); - if(this->try_lock_d(lm)==0) + + if (this->try_lock_i (lm) == 0) return CORBA::B_FALSE; else return CORBA::B_TRUE; @@ -137,9 +127,9 @@ CC_LockSet::try_lock (CosConcurrencyControl::lock_mode mode, // enum. CC_LockModeEnum -CC_LockSet::lmconvert(CosConcurrencyControl::lock_mode mode) +CC_LockSet::lmconvert (CosConcurrencyControl::lock_mode mode) { - switch(mode) + switch (mode) { case CosConcurrencyControl::intention_read: return CC_IR; @@ -165,33 +155,37 @@ CC_LockSet::unlock (CosConcurrencyControl::lock_mode mode, ACE_DEBUG ((LM_DEBUG, "CC_LockSet::unlock\n")); - CC_LockModeEnum lm = lmconvert(mode); + CC_LockModeEnum lm = lmconvert (mode); - ACE_GUARD (ACE_Thread_Mutex, ace_mon, *this->mlock_); + ACE_GUARD (ACE_Thread_Mutex, ace_mon, this->mlock_); TAO_TRY { - if(lock_[lm]==0) // This lock is not held. + if (lock_[lm] == 0) // This lock is not held. TAO_THROW (CosConcurrencyControl::LockNotHeld); else lock_[lm]--; + TAO_CHECK_ENV; + // If we do not have a lock held in a weaker mode than the // strongest held and we have requests on the semaphore signal // the semaphore. - while(lock_queue_.size()>0) + while (lock_queue_.size () > 0) { CC_LockModeEnum lock_on_queue = CC_EM; - lock_queue_.dequeue_head(lock_on_queue); - if(compatible(lock_on_queue)==CORBA::B_TRUE) + + lock_queue_.dequeue_head (lock_on_queue); + + if (compatible (lock_on_queue) == CORBA::B_TRUE) { - if(semaphore_.release()==-1) + if (semaphore_.release () == -1) TAO_THROW (CORBA::INTERNAL (CORBA::COMPLETED_NO)); lock_[lock_on_queue]++; } else { - lock_queue_.enqueue_head(lock_on_queue); + lock_queue_.enqueue_head (lock_on_queue); break; } } @@ -201,7 +195,7 @@ CC_LockSet::unlock (CosConcurrencyControl::lock_mode mode, TAO_RETHROW; } TAO_ENDTRY; - this->dump(); + this->dump (); } // Changes the mode of a held lock. @@ -213,22 +207,20 @@ CC_LockSet::change_mode (CosConcurrencyControl::lock_mode held_mode, { ACE_DEBUG ((LM_DEBUG, "CC_LockSet::change_mode\n")); - CC_LockModeEnum lm_held = lmconvert(held_mode); - CC_LockModeEnum lm_new = lmconvert(new_mode); + CC_LockModeEnum lm_held = lmconvert (held_mode); + CC_LockModeEnum lm_new = lmconvert (new_mode); TAO_TRY { - if(this->lock_held(lm_held) == 0) // This lock is not held + if (this->lock_held (lm_held) == 0) // This lock is not held TAO_THROW (CosConcurrencyControl::LockNotHeld); - else + else if (this->change_mode_i (lm_held, lm_new)==1) { - if(this->change_mode_d(lm_held, lm_new)==1) - { - this->unlock(held_mode, _env); - TAO_CHECK_ENV; - if(semaphore_.acquire()==-1) - TAO_THROW (CORBA::INTERNAL (CORBA::COMPLETED_NO)); - } + this->unlock (held_mode, _env); + TAO_CHECK_ENV; + + if (semaphore_.acquire () == -1) + TAO_THROW (CORBA::INTERNAL (CORBA::COMPLETED_NO)); } } TAO_CATCHANY @@ -237,93 +229,97 @@ CC_LockSet::change_mode (CosConcurrencyControl::lock_mode held_mode, } TAO_ENDTRY; - // this->dump(); + // this->dump (); } int -CC_LockSet::lock_d(CC_LockModeEnum lm) +CC_LockSet::lock_i (CC_LockModeEnum lm) { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *this->mlock_, 1); + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->mlock_, 1); // If the lock is not compatible with the locks we hold allready or // there is lock requests in the queue we cannot grant the lock and // thus we queue the request. Otherwise update the lock count. - if(compatible(lm)==CORBA::B_FALSE || lock_queue_.size()>0) + if (compatible (lm) == CORBA::B_FALSE || lock_queue_.size () > 0) { // Put the lock mode in the queue - lock_queue_.enqueue_tail(lm); - this->dump(); - return 1; // Lock the semaphore + lock_queue_.enqueue_tail (lm); + this->dump (); + return 1; // Lock the semaphore. } else - { - lock_[lm]++; - } - this->dump(); + lock_[lm]++; + + this->dump (); return 0; } int -CC_LockSet::try_lock_d(CC_LockModeEnum lm) +CC_LockSet::try_lock_i (CC_LockModeEnum lm) { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *this->mlock_, 1); + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->mlock_, 1); // If the lock we try is compatible with the locks we hold we just // opdates the count. Otherwise we return false. - if(compatible(lm)==CORBA::B_FALSE) + if (compatible (lm) == CORBA::B_FALSE) { - this->dump(); + this->dump (); return 0; } else - { - lock_[lm]++; - } - this->dump(); + lock_[lm]++; + + this->dump (); return 1; } int -CC_LockSet::change_mode_d(CC_LockModeEnum lm_held, +CC_LockSet::change_mode_i (CC_LockModeEnum lm_held, CC_LockModeEnum lm_new) { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *this->mlock_, 1); + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->mlock_, 1); // If the new mode is compatible with the modes we hold we change // the counts for the two locks. If not we must queue the new // request. We can decrement the count for the old mode without // signalling the semaphore because we know we only check modes // granted this far. + lock_[lm_held]--; - if(compatible(lm_new)==CORBA::B_TRUE) + + if (compatible (lm_new) == CORBA::B_TRUE) { lock_[lm_new]++; - this->dump(); + this->dump (); return 0; } else { lock_[lm_held]++; - lock_queue_.enqueue_tail(lm_new); - this->dump(); + lock_queue_.enqueue_tail (lm_new); + this->dump (); return 1; } } int -CC_LockSet::lock_held(CC_LockModeEnum lm) +CC_LockSet::lock_held (CC_LockModeEnum lm) { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *this->mlock_, 1); - if(lock_[lm]>0) + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->mlock_, 1); + if (lock_[lm] > 0) return 1; else return 0; } void -CC_LockSet::dump(void) +CC_LockSet::dump (void) { ACE_DEBUG ((LM_DEBUG, "waiting_calls_: %i, IR: %i, R: %i, U: %i, IW: %i, W: %i\n", - lock_queue_.size(), - lock_[CC_IR], lock_[CC_R], lock_[CC_U], lock_[CC_IW], lock_[CC_W])); + lock_queue_.size (), + lock_[CC_IR], + lock_[CC_R], + lock_[CC_U], + lock_[CC_IW], + lock_[CC_W])); } CORBA::Boolean CC_LockSet::compatible_[NUMBER_OF_LOCK_MODES][NUMBER_OF_LOCK_MODES] ={ diff --git a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.h b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.h index c5716fd0d12..ca85025d05a 100644 --- a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.h +++ b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSet.h @@ -52,6 +52,7 @@ typedef enum {CC_EM=-1, CC_IR=0, CC_R, CC_U, CC_IW, CC_W} CC_LockModeEnum; // indicate a 'non-mode' (CC_EM) class TAO_ORBSVCS_Export CC_LockSet : public POA_CosConcurrencyControl::LockSet +{ // = TITLE // CC_LockSet // @@ -60,17 +61,17 @@ class TAO_ORBSVCS_Export CC_LockSet : public POA_CosConcurrencyControl::LockSet // the CosConcurrency service. Please consult the idl file for // detailed descriptions apart from the comments in this file At // present the lock set is not really a set, but only one lock. -{ public: + // = Initialization and termination methods. - CC_LockSet(void); + CC_LockSet (void); // Default constructor - CC_LockSet(CosConcurrencyControl::LockSet_ptr related); + CC_LockSet (CosConcurrencyControl::LockSet_ptr related); // Constructor used if create_related is used to create the lock // set. - ~CC_LockSet(void); + ~CC_LockSet (void); // Destructor. // = CosConcurrencyControl methods @@ -93,72 +94,73 @@ public: // Changes the mode of this lock. // = Debugging methods - void dump(void); + void dump (void); // Dump the state of the object to stdout private: - CC_LockModeEnum lmconvert(CosConcurrencyControl::lock_mode mode); + CC_LockModeEnum lmconvert (CosConcurrencyControl::lock_mode mode); // Converts the CORBA specification's lock mode to the internal // representation - void Init(CORBA::Environment &_env); + void Init (CORBA::Environment &_env); // Initiatlizes the lock set array and acquires the initial // semaphore. - CORBA::Boolean compatible(CC_LockModeEnum mr); + CORBA::Boolean compatible (CC_LockModeEnum mr); // Returns true if the held lock and the requested lock are compatible - // The _d functions below ensures atomical access the the state data + // The _i functions below ensures atomical access the the state data // for the lock set. The functions acquires a thread lock in order // to insure consistency within the lock set. The return value // typically indicates whether the current thread should be // suspended or not (by locking the semaphore. - int lock_d(CC_LockModeEnum lm); + int lock_i (CC_LockModeEnum lm); // Locks the access to the data and decides whether to lock or // not. Returns 1 if the semaphore should be locked. - // int unlock_d(CosConcurrencyControl::lock_mode lm); + // int unlock_i (CosConcurrencyControl::lock_mode lm); // This function is not necessary because we lock access to the data // and unlocks the semaphore until an invalid lock mode is first on // the queue. Thereafter we release the lock. - int try_lock_d(CC_LockModeEnum lm); + int try_lock_i (CC_LockModeEnum lm); // Locks the access to the data and determines whether to return // true or false. Returns 1 if true should be returned. - int change_mode_d(CC_LockModeEnum lm_held, - CC_LockModeEnum lm_new); + int change_mode_i (CC_LockModeEnum lm_held, + CC_LockModeEnum lm_new); // Locks access to the data and determines if the semaphore should // be locked. Returns 1 if the semaphore should be locked. - int lock_held(CC_LockModeEnum lm); + int lock_held (CC_LockModeEnum lm); // Locks access ti the data and checks whether the lock is held. int lock_[NUMBER_OF_LOCK_MODES]; // An array of lock counters that counts how many locks of that type // that the lock set holds. - ACE_Thread_Semaphore semaphore_; - // ACE_Token semaphore_; + // ACE_Thread_Semaphore semaphore_; + ACE_Token semaphore_; // This is the semaphore for the lock set. The semaphore is used to // queue requests for locks in modes stronger than currently - // possible to grant. + // possible to grant. Note that the provides strict + // FIFO ordering of acquisition/release of the lock. CosConcurrencyControl::LockSet_ptr related_lockset_; // If this lock set is related to another lock set, this is the // pointer to the related lock set. This is a really simple // solution, but since transactions are not supported in the first // version there should be no reason to drop lock sets together. The - // LockSetCoordinator is not implemented (it has the + // is not implemented (it has the // responsibilities of dropping the locks). static CORBA::Boolean compatible_[NUMBER_OF_LOCK_MODES][NUMBER_OF_LOCK_MODES]; - // Mapping between requested and held lock modes. Used by compatible(...). - // Uses the internal enumeration as indices. + // Mapping between requested and held lock modes. Used by compatible + // (...). Uses the internal enumeration as indices. - ACE_Thread_Mutex *mlock_; - // Lock to ensure that race conditions does not occur + ACE_Thread_Mutex mlock_; + // Lock to ensure that race conditions does not occur. ACE_Unbounded_Queue lock_queue_; // Queue to hold the requested locks not yet granted. diff --git a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.cpp b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.cpp index 48707c04a2b..faffa5984f9 100644 --- a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.cpp +++ b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.cpp @@ -5,13 +5,13 @@ // // = LIBRARY // cos -// +// // = FILENAME // CC_LockSetFactory.cpp // // = AUTHOR // Torben Worm -// +// // ============================================================================ #include "CC_LockSetFactory.h" @@ -20,29 +20,20 @@ // Default constructor CC_LockSetFactory::CC_LockSetFactory (void) { - ACE_NEW (this->lock_, ACE_Lock_Adapter); - // Acquire a lock to ensure only one client will be in the factory - // at any time } // Destructor. CC_LockSetFactory::~CC_LockSetFactory (void) { - delete this->lock_; - // Delete the serialization lock } CosConcurrencyControl::LockSet_ptr -CC_LockSetFactory::create ( CORBA::Environment &_env) +CC_LockSetFactory::create (CORBA::Environment &_env) { CC_LockSet *ls = 0; - _env.clear (); - _env.exception (new CORBA::UNKNOWN (CORBA::COMPLETED_NO)); - ACE_GUARD_RETURN (ACE_Lock, ace_mon, *this->lock_, CosConcurrencyControl::LockSet::_nil ()); + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, CosConcurrencyControl::LockSet::_nil ()); - _env.clear (); - _env.exception (new CORBA::NO_MEMORY (CORBA::COMPLETED_NO)); ACE_NEW_RETURN (ls, CC_LockSet, @@ -58,12 +49,8 @@ CC_LockSetFactory::create_related (CosConcurrencyControl::LockSet_ptr which, { CC_LockSet *ls = 0; - _env.clear (); - _env.exception (new CORBA::UNKNOWN (CORBA::COMPLETED_NO)); - ACE_GUARD_RETURN (ACE_Lock, ace_mon, *this->lock_, CosConcurrencyControl::LockSet::_nil ()); + ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, CosConcurrencyControl::LockSet::_nil ()); - _env.clear (); - _env.exception (new CORBA::NO_MEMORY (CORBA::COMPLETED_NO)); ACE_NEW_RETURN (ls, CC_LockSet (which), diff --git a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.h b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.h index 12fbcfdaf57..a4e6c1fe8bc 100644 --- a/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.h +++ b/TAO/orbsvcs/orbsvcs/Concurrency/CC_LockSetFactory.h @@ -25,6 +25,7 @@ #include "orbsvcs/CosConcurrencyControlS.h" class TAO_ORBSVCS_Export CC_LockSetFactory : public POA_CosConcurrencyControl::LockSetFactory +{ // = TITLE // CC_LockSetFactory // @@ -33,13 +34,13 @@ class TAO_ORBSVCS_Export CC_LockSetFactory : public POA_CosConcurrencyControl::L // part of the CosConcurrency service. Please consult the idl // file for detailed descriptions apart from the comments in // this file. -{ public: + // = Initialization and termination methods. - CC_LockSetFactory(void); + CC_LockSetFactory (void); // Default constructor. - ~CC_LockSetFactory(void); + ~CC_LockSetFactory (void); // Destructor. virtual CosConcurrencyControl::LockSet_ptr create (CORBA::Environment &env); @@ -47,9 +48,8 @@ public: virtual CosConcurrencyControl::LockSet_ptr create_related (CosConcurrencyControl::LockSet_ptr which, CORBA::Environment &env); private: - ACE_Lock *lock_; + ACE_Thread_Mutex lock_; // Lock to serialize the access to the factory. - }; #endif /* _CC_LOCKSETFACTORY_H */ -- cgit v1.2.1