// ############################################################################################# // Constructors // ############################################################################################# /** ******************************************************************************************** * Create a ThreadLock that allows recursion. A warning will be given (ALIB Error) when the * given recursion level is reached (and each multiple of it). In addition the lock can be * initialized to be unsafe, which means the locking critical sections is disabled. * * @param lockMode (Optional) Flag if recursion support is on (the default). * If not, nested locks are not counted. * @param safeness (Optional) Defaults to \c Safeness.Safe. * See #SetSafeness for more information. **********************************************************************************************/ public ThreadLock(LockMode lockMode = LockMode.Recursive, Safeness safeness = Safeness.Safe) { // save parameters this.lockMode = lockMode; // set safeness SetSafeness(safeness); }
/** ******************************************************************************************** * If parameter is true, the whole locking system is disabled. The only objective here is to * to gain execution speed, as thread synchronization causes relatively expensive system calls. * Use this method only if you are 100% sure that your (otherwise) critical section are executed * in a single threaded environment. And: "relative expensive" means: they are not really * expensive. This is provided only for the rare case that your critical section is very, * very frequently executed. * * @param safeness Determines if this object should use a mutex (\c Safeness.Safe) * or just do nothing (\c Safeness.Unsafe). *************************************************************************************************/ public void SetSafeness(Safeness safeness) { // are we in unsafe mode? if (mutex == null) { // already locked? ALIB Error if (lockCount != 0) { ALIB.ERROR("Cannot switch safeness mode while already locked. Current mode: unsafe, requested mode: " + safeness.ToString()); return; } // switch on? if (safeness == Safeness.Safe) { mutex = new Object(); } // end of unsafe version of this method return; } // synchronize on mutex lock ( mutex ) { // already locked? ALIB Error if (owner != null) { ALIB.ERROR("Cannot switch safeness mode while already locked. Current mode: safe, requested mode: " + safeness.ToString()); return; } // switch off? if (safeness == Safeness.Unsafe) { mutex = null; } } }
/** ******************************************************************************************** * If parameter is true, the whole locking system is disabled. The only objective here is to * to gain execution speed, as thread synchronization causes relatively expensive system calls. * Use this method only if you are 100% sure that your (otherwise) critical section are executed * in a single threaded environment. And: "relative expensive" means: they are not really * expensive. This is provided only for the rare case that your critical section is very, * very frequently executed. * * @param safeness Determines if this object should use a mutex (\c Safeness.Safe) * or just do nothing (\c Safeness.Unsafe). *************************************************************************************************/ public void SetSafeness( Safeness safeness ) { // are we in unsafe mode? if ( mutex == null ) { // already locked? ALIB Error if( lockCount != 0 ) { ALIB.ERROR( "Cannot switch safeness mode while already locked. Current mode: unsafe, requested mode: " + safeness.ToString() ); return; } // switch on? if( safeness == Safeness.Safe ) mutex= new Object(); // end of unsafe version of this method return; } // synchronize on mutex lock ( mutex ) { // already locked? ALIB Error if( owner != null ) { ALIB.ERROR( "Cannot switch safeness mode while already locked. Current mode: safe, requested mode: " + safeness.ToString() ); return; } // switch off? if( safeness == Safeness.Unsafe ) mutex= null; } }
// ############################################################################################# // Constructors // ############################################################################################# /** ******************************************************************************************** * Create a ThreadLock that allows recursion. A warning will be given (ALIB Error) when the * given recursion level is reached (and each multiple of it). In addition the lock can be * initialized to be unsafe, which means the locking critical sections is disabled. * * @param lockMode (Optional) Flag if recursion support is on (the default). * If not, nested locks are not counted. * @param safeness (Optional) Defaults to \c Safeness.Safe. * See #SetSafeness for more information. **********************************************************************************************/ public ThreadLock( LockMode lockMode= LockMode.Recursive, Safeness safeness= Safeness.Safe ) { // save parameters this.lockMode= lockMode; // set safeness SetSafeness( safeness ); }