Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / Microsoft / Win32 / SafeHandles / SafeWaitHandle.cs / 1305376 / SafeWaitHandle.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: SafeWaitHandle ** ** ** A wrapper for Win32 events (mutexes, auto reset events, and ** manual reset events). Used by WaitHandle. ** ** ===========================================================*/ using System; using System.Security; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.Versioning; using Microsoft.Win32; using System.Threading; namespace Microsoft.Win32.SafeHandles { [System.Security.SecurityCritical] // auto-generated_required public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid { // Special case flags for Mutexes enables workaround for known OS bug at // http://support.microsoft.com/default.aspx?scid=kb;en-us;889318 // One machine-wide mutex serializes all OpenMutex and CloseHandle operations. // bIsMutex: if true, we need to grab machine-wide mutex before doing any Close ops. // Initialized to false by the runtime. private bool bIsMutex; // bIsMutex: if true, we need to avoid grabbing the machine-wide mutex before Close ops, // since that mutex is, of course, this very handle. // Initialized to false by the runtime. private bool bIsReservedMutex; // Called by P/Invoke marshaler private SafeWaitHandle() : base(true) { } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle) { SetHandle(existingHandle); } [System.Security.SecurityCritical] [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] override protected bool ReleaseHandle() { #if !FEATURE_CORECLR if (!bIsMutex || Environment.HasShutdownStarted) return Win32Native.CloseHandle(handle); bool bReturn = false; bool bMutexObtained = false; try { if (!bIsReservedMutex) { Mutex.AcquireReservedMutex(ref bMutexObtained); } bReturn = Win32Native.CloseHandle(handle); } finally { if (bMutexObtained) Mutex.ReleaseReservedMutex(); } return bReturn; #else return Win32Native.CloseHandle(handle); #endif } internal void SetAsMutex() { bIsMutex = true; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal void SetAsReservedMutex() { bIsReservedMutex = true; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: SafeWaitHandle ** ** ** A wrapper for Win32 events (mutexes, auto reset events, and ** manual reset events). Used by WaitHandle. ** ** ===========================================================*/ using System; using System.Security; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.Versioning; using Microsoft.Win32; using System.Threading; namespace Microsoft.Win32.SafeHandles { [System.Security.SecurityCritical] // auto-generated_required public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid { // Special case flags for Mutexes enables workaround for known OS bug at // http://support.microsoft.com/default.aspx?scid=kb;en-us;889318 // One machine-wide mutex serializes all OpenMutex and CloseHandle operations. // bIsMutex: if true, we need to grab machine-wide mutex before doing any Close ops. // Initialized to false by the runtime. private bool bIsMutex; // bIsMutex: if true, we need to avoid grabbing the machine-wide mutex before Close ops, // since that mutex is, of course, this very handle. // Initialized to false by the runtime. private bool bIsReservedMutex; // Called by P/Invoke marshaler private SafeWaitHandle() : base(true) { } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle) { SetHandle(existingHandle); } [System.Security.SecurityCritical] [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] override protected bool ReleaseHandle() { #if !FEATURE_CORECLR if (!bIsMutex || Environment.HasShutdownStarted) return Win32Native.CloseHandle(handle); bool bReturn = false; bool bMutexObtained = false; try { if (!bIsReservedMutex) { Mutex.AcquireReservedMutex(ref bMutexObtained); } bReturn = Win32Native.CloseHandle(handle); } finally { if (bMutexObtained) Mutex.ReleaseReservedMutex(); } return bReturn; #else return Win32Native.CloseHandle(handle); #endif } internal void SetAsMutex() { bIsMutex = true; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal void SetAsReservedMutex() { bIsReservedMutex = true; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- KeyPressEvent.cs
- DesignerForm.cs
- Variable.cs
- DiagnosticStrings.cs
- TransactionProtocol.cs
- SpellerHighlightLayer.cs
- PixelShader.cs
- CollectionView.cs
- DoubleAnimationUsingPath.cs
- Inflater.cs
- Configuration.cs
- ExpressionBuilder.cs
- ObjectItemCollection.cs
- UniqueID.cs
- FactoryGenerator.cs
- AutoScrollExpandMessageFilter.cs
- FirstMatchCodeGroup.cs
- SmtpReplyReaderFactory.cs
- BinaryNode.cs
- dataprotectionpermissionattribute.cs
- XamlReaderHelper.cs
- AxisAngleRotation3D.cs
- InternalCache.cs
- HyperLinkField.cs
- SqlTriggerAttribute.cs
- MissingManifestResourceException.cs
- RichTextBoxConstants.cs
- TextBox.cs
- TableAdapterManagerGenerator.cs
- RoleManagerModule.cs
- OutputScopeManager.cs
- IPPacketInformation.cs
- DataSourceExpression.cs
- LinqMaximalSubtreeNominator.cs
- PtsHost.cs
- ToolboxItemAttribute.cs
- WaitHandleCannotBeOpenedException.cs
- TextStore.cs
- ClientFormsIdentity.cs
- SQLBinaryStorage.cs
- MessagePropertyAttribute.cs
- TcpProcessProtocolHandler.cs
- BlurBitmapEffect.cs
- ConfigXmlText.cs
- SafeEventLogWriteHandle.cs
- UserControlCodeDomTreeGenerator.cs
- SerializableAttribute.cs
- AnnotationResourceCollection.cs
- DriveInfo.cs
- SelectionRangeConverter.cs
- ValidationVisibilityAttribute.cs
- BaseDataList.cs
- EntityTypeBase.cs
- XmlAttributes.cs
- ItemContainerProviderWrapper.cs
- EffectiveValueEntry.cs
- DebugHandleTracker.cs
- DecoderNLS.cs
- DesignerForm.cs
- PropertyEmitterBase.cs
- MissingMemberException.cs
- CodeExpressionStatement.cs
- Int16Storage.cs
- CustomPopupPlacement.cs
- TreeIterators.cs
- TreeNodeCollection.cs
- SafeCoTaskMem.cs
- DoubleLinkListEnumerator.cs
- TypeConstant.cs
- XmlMessageFormatter.cs
- BaseTemplateBuildProvider.cs
- HebrewNumber.cs
- SafeReversePInvokeHandle.cs
- WindowsHyperlink.cs
- Image.cs
- StorageTypeMapping.cs
- ViewCellSlot.cs
- SmtpAuthenticationManager.cs
- DockingAttribute.cs
- WebServiceEnumData.cs
- SpecularMaterial.cs
- NullableIntSumAggregationOperator.cs
- AttachedAnnotationChangedEventArgs.cs
- XmlSchemaValidationException.cs
- MetadataItem_Static.cs
- ParallelTimeline.cs
- DbProviderFactoriesConfigurationHandler.cs
- PropertyMapper.cs
- BitmapCodecInfoInternal.cs
- ServiceDescription.cs
- HttpException.cs
- SHA384.cs
- ConnectionAcceptor.cs
- SkinBuilder.cs
- ZipIORawDataFileBlock.cs
- VerificationAttribute.cs
- SubMenuStyle.cs
- LayoutManager.cs
- ContentPropertyAttribute.cs
- CheckBox.cs