Imported Upstream version 4.3.2.467

Former-commit-id: 9c2cb47f45fa221e661ab616387c9cda183f283d
This commit is contained in:
Xamarin Public Jenkins
2016-02-22 11:00:01 -05:00
parent f302175246
commit f3e3aab35a
4097 changed files with 122406 additions and 82300 deletions

View File

@@ -0,0 +1,123 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Security;
namespace System.Threading
{
//
// AsyncLocal<T> represents "ambient" data that is local to a given asynchronous control flow, such as an
// async method. For example, say you want to associate a culture with a given async flow:
//
// static AsyncLocal<Culture> s_currentCulture = new AsyncLocal<Culture>();
//
// static async Task SomeOperationAsync(Culture culture)
// {
// s_currentCulture.Value = culture;
//
// await FooAsync();
// }
//
// static async Task FooAsync()
// {
// PrintStringWithCulture(s_currentCulture.Value);
// }
//
// AsyncLocal<T> also provides optional notifications when the value associated with the current thread
// changes, either because it was explicitly changed by setting the Value property, or implicitly changed
// when the thread encountered an "await" or other context transition. For example, we might want our
// current culture to be communicated to the OS as well:
//
// static AsyncLocal<Culture> s_currentCulture = new AsyncLocal<Culture>(
// args =>
// {
// NativeMethods.SetThreadCulture(args.CurrentValue.LCID);
// });
//
public sealed class AsyncLocal<T> : IAsyncLocal
{
[SecurityCritical] // critical because this action will terminate the process if it throws.
private readonly Action<AsyncLocalValueChangedArgs<T>> m_valueChangedHandler;
//
// Constructs an AsyncLocal<T> that does not receive change notifications.
//
public AsyncLocal()
{
}
//
// Constructs an AsyncLocal<T> with a delegate that is called whenever the current value changes
// on any thread.
//
[SecurityCritical]
public AsyncLocal(Action<AsyncLocalValueChangedArgs<T>> valueChangedHandler)
{
m_valueChangedHandler = valueChangedHandler;
}
public T Value
{
[SecuritySafeCritical]
get
{
#if MONO
throw new NotImplementedException ();
#else
object obj = ExecutionContext.GetLocalValue(this);
return (obj == null) ? default(T) : (T)obj;
#endif
}
[SecuritySafeCritical]
set
{
#if MONO
throw new NotImplementedException ();
#else
ExecutionContext.SetLocalValue(this, value, m_valueChangedHandler != null);
#endif
}
}
[SecurityCritical]
void IAsyncLocal.OnValueChanged(object previousValueObj, object currentValueObj, bool contextChanged)
{
Contract.Assert(m_valueChangedHandler != null);
T previousValue = previousValueObj == null ? default(T) : (T)previousValueObj;
T currentValue = currentValueObj == null ? default(T) : (T)currentValueObj;
m_valueChangedHandler(new AsyncLocalValueChangedArgs<T>(previousValue, currentValue, contextChanged));
}
}
//
// Interface to allow non-generic code in ExecutionContext to call into the generic AsyncLocal<T> type.
//
internal interface IAsyncLocal
{
[SecurityCritical]
void OnValueChanged(object previousValue, object currentValue, bool contextChanged);
}
public struct AsyncLocalValueChangedArgs<T>
{
public T PreviousValue { get; private set; }
public T CurrentValue { get; private set; }
//
// If the value changed because we changed to a different ExecutionContext, this is true. If it changed
// because someone set the Value property, this is false.
//
public bool ThreadContextChanged { get; private set; }
internal AsyncLocalValueChangedArgs(T previousValue, T currentValue, bool contextChanged)
: this()
{
PreviousValue = previousValue;
CurrentValue = currentValue;
ThreadContextChanged = contextChanged;
}
}
}

View File

@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
namespace System.Runtime.CompilerServices
{
using System;
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
public sealed class DisablePrivateReflectionAttribute : Attribute
{
public DisablePrivateReflectionAttribute() {}
}
}

View File

@@ -0,0 +1,136 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace System.Text
{
using System;
using System.Collections;
using System.Collections.Generic;
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class EncodingProvider
{
public EncodingProvider() { }
public abstract Encoding GetEncoding(string name);
public abstract Encoding GetEncoding(int codepage);
// GetEncoding should return either valid encoding or null. shouldn't throw any exception except on null name
public virtual Encoding GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
Encoding enc = GetEncoding(name);
if (enc != null)
{
enc = (Encoding)GetEncoding(name).Clone();
enc.EncoderFallback = encoderFallback;
enc.DecoderFallback = decoderFallback;
}
return enc;
}
public virtual Encoding GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
Encoding enc = GetEncoding(codepage);
if (enc != null)
{
enc = (Encoding)GetEncoding(codepage).Clone();
enc.EncoderFallback = encoderFallback;
enc.DecoderFallback = decoderFallback;
}
return enc;
}
internal static void AddProvider(EncodingProvider provider)
{
if (provider == null)
throw new ArgumentNullException("provider");
lock (s_InternalSyncObject)
{
if (s_providers == null)
{
s_providers = new EncodingProvider[1] { provider };
return;
}
if (Array.IndexOf(s_providers, provider) >= 0)
{
return;
}
var providers = new EncodingProvider[s_providers.Length + 1];
Array.Copy(s_providers, providers, s_providers.Length);
providers[providers.Length - 1] = provider;
s_providers = providers;
}
}
internal static Encoding GetEncodingFromProvider(int codepage)
{
if (s_providers == null)
return null;
var providers = s_providers;
foreach (EncodingProvider provider in providers)
{
Encoding enc = provider.GetEncoding(codepage);
if (enc != null)
return enc;
}
return null;
}
internal static Encoding GetEncodingFromProvider(string encodingName)
{
if (s_providers == null)
return null;
var providers = s_providers;
foreach (EncodingProvider provider in providers)
{
Encoding enc = provider.GetEncoding(encodingName);
if (enc != null)
return enc;
}
return null;
}
internal static Encoding GetEncodingFromProvider(int codepage, EncoderFallback enc, DecoderFallback dec)
{
if (s_providers == null)
return null;
var providers = s_providers;
foreach (EncodingProvider provider in providers)
{
Encoding encing = provider.GetEncoding(codepage, enc, dec);
if (encing != null)
return encing;
}
return null;
}
internal static Encoding GetEncodingFromProvider(string encodingName, EncoderFallback enc, DecoderFallback dec)
{
if (s_providers == null)
return null;
var providers = s_providers;
foreach (EncodingProvider provider in providers)
{
Encoding encoding = provider.GetEncoding(encodingName, enc, dec);
if (encoding != null)
return encoding;
}
return null;
}
private static Object s_InternalSyncObject = new Object();
private static volatile EncodingProvider[] s_providers;
}
}

View File

@@ -0,0 +1,80 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/*============================================================
**
** Class: FormattableString
**
**
** Purpose: implementation of the FormattableString
** class.
**
===========================================================*/
namespace System
{
/// <summary>
/// A composite format string along with the arguments to be formatted. An instance of this
/// type may result from the use of the C# or VB language primitive "interpolated string".
/// </summary>
public abstract class FormattableString : IFormattable
{
/// <summary>
/// The composite format string.
/// </summary>
public abstract string Format { get; }
/// <summary>
/// Returns an object array that contains zero or more objects to format. Clients should not
/// mutate the contents of the array.
/// </summary>
public abstract object[] GetArguments();
/// <summary>
/// The number of arguments to be formatted.
/// </summary>
public abstract int ArgumentCount { get; }
/// <summary>
/// Returns one argument to be formatted from argument position <paramref name="index"/>.
/// </summary>
public abstract object GetArgument(int index);
/// <summary>
/// Format to a string using the given culture.
/// </summary>
public abstract string ToString(IFormatProvider formatProvider);
string IFormattable.ToString(string ignored, IFormatProvider formatProvider)
{
return ToString(formatProvider);
}
/// <summary>
/// Format the given object in the invariant culture. This static method may be
/// imported in C# by
/// <code>
/// using static System.FormattableString;
/// </code>.
/// Within the scope
/// of that import directive an interpolated string may be formatted in the
/// invariant culture by writing, for example,
/// <code>
/// Invariant($"{{ lat = {latitude}; lon = {longitude} }}")
/// </code>
/// </summary>
public static string Invariant(FormattableString formattable)
{
if (formattable == null)
{
throw new ArgumentNullException("formattable");
}
return formattable.ToString(Globalization.CultureInfo.InvariantCulture);
}
public override string ToString()
{
return ToString(Globalization.CultureInfo.CurrentCulture);
}
}
}

View File

@@ -0,0 +1,57 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/*============================================================
**
** Class: FormattableStringFactory
**
**
** Purpose: implementation of the FormattableStringFactory
** class.
**
===========================================================*/
namespace System.Runtime.CompilerServices
{
/// <summary>
/// A factory type used by compilers to create instances of the type <see cref="FormattableString"/>.
/// </summary>
public static class FormattableStringFactory
{
/// <summary>
/// Create a <see cref="FormattableString"/> from a composite format string and object
/// array containing zero or more objects to format.
/// </summary>
public static FormattableString Create(string format, params object[] arguments)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
if (arguments == null)
{
throw new ArgumentNullException("arguments");
}
return new ConcreteFormattableString(format, arguments);
}
private sealed class ConcreteFormattableString : FormattableString
{
private readonly string _format;
private readonly object[] _arguments;
internal ConcreteFormattableString(string format, object[] arguments)
{
_format = format;
_arguments = arguments;
}
public override string Format { get { return _format; } }
public override object[] GetArguments() { return _arguments; }
public override int ArgumentCount { get { return _arguments.Length; } }
public override object GetArgument(int index) { return _arguments[index]; }
public override string ToString(IFormatProvider formatProvider) { return string.Format(formatProvider, _format, _arguments); }
}
}
}

View File

@@ -0,0 +1,45 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.Win32.SafeHandles;
using System.Security;
namespace System.Threading
{
public static class WaitHandleExtensions
{
/// <summary>
/// Gets the native operating system handle.
/// </summary>
/// <param name="waitHandle">The <see cref="System.Threading.WaitHandle"/> to operate on.</param>
/// <returns>A <see cref="System.Runtime.InteropServices.SafeHandle"/> representing the native operating system handle.</returns>
[SecurityCritical]
public static SafeWaitHandle GetSafeWaitHandle(this WaitHandle waitHandle)
{
if (waitHandle == null)
{
throw new ArgumentNullException("waitHandle");
}
return waitHandle.SafeWaitHandle;
}
/// <summary>
/// Sets the native operating system handle
/// </summary>
/// <param name="waitHandle">The <see cref="System.Threading.WaitHandle"/> to operate on.</param>
/// <param name="value">A <see cref="System.Runtime.InteropServices.SafeHandle"/> representing the native operating system handle.</param>
[SecurityCritical]
public static void SetSafeWaitHandle(this WaitHandle waitHandle, SafeWaitHandle value)
{
if (waitHandle == null)
{
throw new ArgumentNullException("waitHandle");
}
waitHandle.SafeWaitHandle = value;
}
}
}