| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | |
|
| | 9 | | static class Fx |
| | 10 | | { |
| | 11 | | static ExceptionUtility exceptionUtility; |
| | 12 | |
|
| | 13 | | public static ExceptionUtility Exception |
| | 14 | | { |
| | 15 | | get |
| | 16 | | { |
| 8 | 17 | | if (exceptionUtility == null) |
| | 18 | | { |
| 2 | 19 | | exceptionUtility = new ExceptionUtility(); |
| | 20 | | } |
| | 21 | |
|
| 8 | 22 | | return exceptionUtility; |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static class Tag |
| | 27 | | { |
| | 28 | | public enum CacheAttrition |
| | 29 | | { |
| | 30 | | None, |
| | 31 | | ElementOnTimer, |
| | 32 | |
|
| | 33 | | // A finalizer/WeakReference based cache, where the elements are held by WeakReferences (or hold an |
| | 34 | | // inner object by a WeakReference), and the weakly-referenced object has a finalizer which cleans the |
| | 35 | | // item from the cache. |
| | 36 | | ElementOnGC, |
| | 37 | |
|
| | 38 | | // A cache that provides a per-element token, delegate, interface, or other piece of context that can |
| | 39 | | // be used to remove the element (such as IDisposable). |
| | 40 | | ElementOnCallback, |
| | 41 | |
|
| | 42 | | FullPurgeOnTimer, |
| | 43 | | FullPurgeOnEachAccess, |
| | 44 | | PartialPurgeOnTimer, |
| | 45 | | PartialPurgeOnEachAccess, |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public enum Location |
| | 49 | | { |
| | 50 | | InProcess, |
| | 51 | | OutOfProcess, |
| | 52 | | LocalSystem, |
| | 53 | | LocalOrRemoteSystem, // as in a file that might live on a share |
| | 54 | | RemoteSystem, |
| | 55 | | } |
| | 56 | |
|
| | 57 | | public enum SynchronizationKind |
| | 58 | | { |
| | 59 | | LockStatement, |
| | 60 | | MonitorWait, |
| | 61 | | MonitorExplicit, |
| | 62 | | InterlockedNoSpin, |
| | 63 | | InterlockedWithSpin, |
| | 64 | |
|
| | 65 | | // Same as LockStatement if the field type is object. |
| | 66 | | FromFieldType, |
| | 67 | | } |
| | 68 | |
|
| | 69 | | [Flags] |
| | 70 | | public enum BlocksUsing |
| | 71 | | { |
| | 72 | | MonitorEnter, |
| | 73 | | MonitorWait, |
| | 74 | | ManualResetEvent, |
| | 75 | | AutoResetEvent, |
| | 76 | | AsyncResult, |
| | 77 | | IAsyncResult, |
| | 78 | | PInvoke, |
| | 79 | | InputQueue, |
| | 80 | | ThreadNeutralSemaphore, |
| | 81 | | PrivatePrimitive, |
| | 82 | | OtherInternalPrimitive, |
| | 83 | | OtherFrameworkPrimitive, |
| | 84 | | OtherInterop, |
| | 85 | | Other, |
| | 86 | |
|
| | 87 | | NonBlocking, // For use by non-blocking SynchronizationPrimitives such as IOThreadScheduler |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public static class Strings |
| | 91 | | { |
| | 92 | | internal const string ExternallyManaged = "externally managed"; |
| | 93 | | internal const string AppDomain = "AppDomain"; |
| | 94 | | internal const string DeclaringInstance = "instance of declaring class"; |
| | 95 | | internal const string Unbounded = "unbounded"; |
| | 96 | | internal const string Infinite = "infinite"; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | [AttributeUsage( |
| | 100 | | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Constructor, |
| | 101 | | AllowMultiple = true, |
| | 102 | | Inherited = false)] |
| | 103 | | [Conditional("CODE_ANALYSIS")] |
| | 104 | | public sealed class ExternalResourceAttribute : Attribute |
| | 105 | | { |
| | 106 | | readonly Location location; |
| | 107 | | readonly string description; |
| | 108 | |
|
| 0 | 109 | | public ExternalResourceAttribute(Location location, string description) |
| | 110 | | { |
| 0 | 111 | | this.location = location; |
| 0 | 112 | | this.description = description; |
| 0 | 113 | | } |
| | 114 | |
|
| 0 | 115 | | public Location Location => this.location; |
| | 116 | |
|
| 0 | 117 | | public string Description => this.description; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | [AttributeUsage(AttributeTargets.Field)] |
| | 121 | | [Conditional("CODE_ANALYSIS")] |
| | 122 | | public sealed class CacheAttribute : Attribute |
| | 123 | | { |
| | 124 | | readonly Type elementType; |
| | 125 | | readonly CacheAttrition cacheAttrition; |
| | 126 | |
|
| 0 | 127 | | public CacheAttribute(Type elementType, CacheAttrition cacheAttrition) |
| | 128 | | { |
| 0 | 129 | | this.Scope = Strings.DeclaringInstance; |
| 0 | 130 | | this.SizeLimit = Strings.Unbounded; |
| 0 | 131 | | this.Timeout = Strings.Infinite; |
| | 132 | |
|
| 0 | 133 | | if (elementType == null) |
| | 134 | | { |
| 0 | 135 | | throw Fx.Exception.ArgumentNull(nameof(elementType)); |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | this.elementType = elementType; |
| 0 | 139 | | this.cacheAttrition = cacheAttrition; |
| 0 | 140 | | } |
| | 141 | |
|
| 0 | 142 | | public Type ElementType => this.elementType; |
| | 143 | |
|
| 0 | 144 | | public CacheAttrition CacheAttrition => this.cacheAttrition; |
| | 145 | |
|
| 0 | 146 | | public string Scope { get; set; } |
| | 147 | |
|
| 0 | 148 | | public string SizeLimit { get; set; } |
| | 149 | |
|
| 0 | 150 | | public string Timeout { get; set; } |
| | 151 | | } |
| | 152 | |
|
| | 153 | | [AttributeUsage(AttributeTargets.Field)] |
| | 154 | | [Conditional("CODE_ANALYSIS")] |
| | 155 | | public sealed class QueueAttribute : Attribute |
| | 156 | | { |
| | 157 | | readonly Type elementType; |
| | 158 | |
|
| 0 | 159 | | public QueueAttribute(Type elementType) |
| | 160 | | { |
| 0 | 161 | | this.Scope = Strings.DeclaringInstance; |
| 0 | 162 | | this.SizeLimit = Strings.Unbounded; |
| | 163 | |
|
| 0 | 164 | | if (elementType == null) |
| | 165 | | { |
| 0 | 166 | | throw Fx.Exception.ArgumentNull(nameof(elementType)); |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | this.elementType = elementType; |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | public Type ElementType => this.elementType; |
| | 173 | |
|
| 0 | 174 | | public string Scope { get; set; } |
| | 175 | |
|
| 0 | 176 | | public string SizeLimit { get; set; } |
| | 177 | |
|
| 0 | 178 | | public bool StaleElementsRemovedImmediately { get; set; } |
| | 179 | |
|
| 0 | 180 | | public bool EnqueueThrowsIfFull { get; set; } |
| | 181 | | } |
| | 182 | |
|
| | 183 | | // Set on a class when that class uses lock (this) - acts as though it were on a field |
| | 184 | | // object this; |
| | 185 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false)] |
| | 186 | | [Conditional("CODE_ANALYSIS")] |
| | 187 | | public sealed class SynchronizationObjectAttribute : Attribute |
| | 188 | | { |
| 0 | 189 | | public SynchronizationObjectAttribute() |
| | 190 | | { |
| 0 | 191 | | this.Blocking = true; |
| 0 | 192 | | this.Scope = Strings.DeclaringInstance; |
| 0 | 193 | | this.Kind = SynchronizationKind.FromFieldType; |
| 0 | 194 | | } |
| | 195 | |
|
| 0 | 196 | | public bool Blocking { get; set; } |
| | 197 | |
|
| 0 | 198 | | public string Scope { get; set; } |
| | 199 | |
|
| 0 | 200 | | public SynchronizationKind Kind { get; set; } |
| | 201 | | } |
| | 202 | |
|
| | 203 | | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)] |
| | 204 | | [Conditional("CODE_ANALYSIS")] |
| | 205 | | public sealed class SynchronizationPrimitiveAttribute : Attribute |
| | 206 | | { |
| | 207 | | readonly BlocksUsing blocksUsing; |
| | 208 | |
|
| 0 | 209 | | public SynchronizationPrimitiveAttribute(BlocksUsing blocksUsing) |
| | 210 | | { |
| 0 | 211 | | this.blocksUsing = blocksUsing; |
| 0 | 212 | | } |
| | 213 | |
|
| 0 | 214 | | public BlocksUsing BlocksUsing => this.blocksUsing; |
| | 215 | |
|
| 0 | 216 | | public bool SupportsAsync { get; set; } |
| | 217 | |
|
| 0 | 218 | | public bool Spins { get; set; } |
| | 219 | |
|
| 0 | 220 | | public string ReleaseMethod { get; set; } |
| | 221 | |
|
| | 222 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] |
| | 223 | | [Conditional("CODE_ANALYSIS")] |
| | 224 | | public sealed class BlockingAttribute : Attribute |
| | 225 | | { |
| 0 | 226 | | public string CancelMethod { get; set; } |
| | 227 | |
|
| 0 | 228 | | public Type CancelDeclaringType { get; set; } |
| | 229 | |
|
| 0 | 230 | | public string Conditional { get; set; } |
| | 231 | | } |
| | 232 | |
|
| | 233 | | // Sometime a method will call a conditionally-blocking method in such a way that it is guaranteed |
| | 234 | | // not to block (i.e. the condition can be Asserted false). Such a method can be marked as |
| | 235 | | // GuaranteeNonBlocking as an assertion that the method doesn't block despite calling a blocking method. |
| | 236 | | // |
| | 237 | | // Methods that don't call blocking methods and aren't marked as Blocking are assumed not to block, so |
| | 238 | | // they do not require this attribute. |
| | 239 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] |
| | 240 | | [Conditional("CODE_ANALYSIS")] |
| | 241 | | public sealed class GuaranteeNonBlockingAttribute : Attribute |
| | 242 | | { |
| | 243 | | } |
| | 244 | |
|
| | 245 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] |
| | 246 | | [Conditional("CODE_ANALYSIS")] |
| | 247 | | public sealed class NonThrowingAttribute : Attribute |
| | 248 | | { |
| | 249 | | } |
| | 250 | |
|
| | 251 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = true, Inherited |
| | 252 | | [Conditional("CODE_ANALYSIS")] |
| | 253 | | public class ThrowsAttribute : Attribute |
| | 254 | | { |
| | 255 | | readonly Type exceptionType; |
| | 256 | | readonly string diagnosis; |
| | 257 | |
|
| 0 | 258 | | public ThrowsAttribute(Type exceptionType, string diagnosis) |
| | 259 | | { |
| 0 | 260 | | if (exceptionType == null) |
| | 261 | | { |
| 0 | 262 | | throw Fx.Exception.ArgumentNull(nameof(exceptionType)); |
| | 263 | | } |
| 0 | 264 | | if (string.IsNullOrEmpty(diagnosis)) |
| | 265 | | { |
| | 266 | | ////throw Fx.Exception.ArgumentNullOrEmpty("diagnosis"); |
| 0 | 267 | | throw new ArgumentNullException(nameof(diagnosis)); |
| | 268 | | } |
| | 269 | |
|
| 0 | 270 | | this.exceptionType = exceptionType; |
| 0 | 271 | | this.diagnosis = diagnosis; |
| 0 | 272 | | } |
| | 273 | |
|
| 0 | 274 | | public Type ExceptionType => this.exceptionType; |
| | 275 | |
|
| 0 | 276 | | public string Diagnosis => this.diagnosis; |
| | 277 | | } |
| | 278 | |
|
| | 279 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] |
| | 280 | | [Conditional("CODE_ANALYSIS")] |
| | 281 | | public sealed class InheritThrowsAttribute : Attribute |
| | 282 | | { |
| 0 | 283 | | public Type FromDeclaringType { get; set; } |
| | 284 | |
|
| 0 | 285 | | public string From { get; set; } |
| | 286 | | } |
| | 287 | |
|
| | 288 | | [AttributeUsage( |
| | 289 | | AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | |
| | 290 | | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Me |
| | 291 | | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Inter |
| | 292 | | AttributeTargets.Delegate, AllowMultiple = false, |
| | 293 | | Inherited = false)] |
| | 294 | | [Conditional("CODE_ANALYSIS")] |
| | 295 | | public sealed class SecurityNoteAttribute : Attribute |
| | 296 | | { |
| 0 | 297 | | public string Critical { get; set; } |
| | 298 | |
|
| 0 | 299 | | public string Safe { get; set; } |
| | 300 | |
|
| 0 | 301 | | public string Miscellaneous { get; set; } |
| | 302 | | } |
| | 303 | | } |
| | 304 | | } |
| | 305 | | } |
| | 306 | | } |