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
- ISO2022Encoding.cs
- TabletCollection.cs
- ObjectItemConventionAssemblyLoader.cs
- XmlSchemaNotation.cs
- HttpGetProtocolImporter.cs
- TextServicesContext.cs
- XomlCompiler.cs
- OptimalBreakSession.cs
- ResourceAssociationSet.cs
- CodeDelegateInvokeExpression.cs
- PartitionedStream.cs
- UnmanagedMemoryAccessor.cs
- Vertex.cs
- BasicExpandProvider.cs
- IPCCacheManager.cs
- CodeRemoveEventStatement.cs
- Ray3DHitTestResult.cs
- AlphaSortedEnumConverter.cs
- DataServiceQueryOfT.cs
- NotEqual.cs
- TextWriterTraceListener.cs
- DodSequenceMerge.cs
- CheckPair.cs
- _DomainName.cs
- HtmlInputPassword.cs
- HandleCollector.cs
- SourceItem.cs
- TagMapInfo.cs
- ShapingEngine.cs
- SecUtil.cs
- ContainerParagraph.cs
- CodeCommentStatementCollection.cs
- ScriptingScriptResourceHandlerSection.cs
- SkipQueryOptionExpression.cs
- ResetableIterator.cs
- RawStylusInputCustomDataList.cs
- PropertyDescriptorCollection.cs
- ListViewAutomationPeer.cs
- DebugInfoExpression.cs
- RuleRef.cs
- EventManager.cs
- Convert.cs
- SafeEventLogWriteHandle.cs
- StreamWriter.cs
- CqlBlock.cs
- ContractNamespaceAttribute.cs
- SafeSecurityHelper.cs
- EntityContainerAssociationSet.cs
- XmlEncoding.cs
- WindowsMenu.cs
- OptionUsage.cs
- WebPartUtil.cs
- TcpConnectionPoolSettings.cs
- WindowsTokenRoleProvider.cs
- HashRepartitionEnumerator.cs
- PixelFormatConverter.cs
- MobileControlsSection.cs
- StaticDataManager.cs
- HtmlMobileTextWriter.cs
- ObjectDataSource.cs
- GridItemPatternIdentifiers.cs
- InstanceStoreQueryResult.cs
- LinqDataSourceValidationException.cs
- LinqDataSourceUpdateEventArgs.cs
- RectangleConverter.cs
- XamlContextStack.cs
- XmlReaderSettings.cs
- ExclusiveHandle.cs
- TagPrefixCollection.cs
- ConditionalAttribute.cs
- AllMembershipCondition.cs
- ClosureBinding.cs
- SpecialTypeDataContract.cs
- ObjectContextServiceProvider.cs
- PropertyNames.cs
- UpdatePanelControlTrigger.cs
- VersionedStream.cs
- FrameworkRichTextComposition.cs
- WebPartDescription.cs
- wgx_exports.cs
- Normalizer.cs
- TileBrush.cs
- GlobalizationAssembly.cs
- C14NUtil.cs
- ActivityPreviewDesigner.cs
- BlurEffect.cs
- WindowsButton.cs
- safex509handles.cs
- ToolStripItemTextRenderEventArgs.cs
- UrlMappingsSection.cs
- ProjectionCamera.cs
- DataGridToolTip.cs
- ClientRolePrincipal.cs
- CompositionAdorner.cs
- HostExecutionContextManager.cs
- ResourceKey.cs
- CalendarDataBindingHandler.cs
- PointUtil.cs
- StylusCollection.cs
- StringValueSerializer.cs