Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / ActionMethodSelectorCache.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Threading;\r
17 \r
18     internal sealed class ActionMethodSelectorCache {\r
19 \r
20         private ReaderWriterLock _rwLock = new ReaderWriterLock();\r
21         private Dictionary<Type, ActionMethodSelector> _selectorDictionary = new Dictionary<Type, ActionMethodSelector>();\r
22 \r
23         // This method could potentially return multiple selectors for the same controllerType due to\r
24         // upgrading the lock from reader to writer, but the dictionary won't be corrupted as a result.\r
25         public ActionMethodSelector GetSelector(Type controllerType) {\r
26             ActionMethodSelector selector;\r
27 \r
28             _rwLock.AcquireReaderLock(Timeout.Infinite);\r
29             try {\r
30                 if (_selectorDictionary.TryGetValue(controllerType, out selector)) {\r
31                     return selector;\r
32                 }\r
33             }\r
34             finally {\r
35                 _rwLock.ReleaseReaderLock();\r
36             }\r
37 \r
38             // if we got this far, the selector was not in the cache\r
39             selector = new ActionMethodSelector(controllerType);\r
40             _rwLock.AcquireWriterLock(Timeout.Infinite);\r
41             try {\r
42                 _selectorDictionary[controllerType] = selector;\r
43                 return selector;\r
44             }\r
45             finally {\r
46                 _rwLock.ReleaseWriterLock();\r
47             }\r
48         }\r
49 \r
50     }\r
51 }\r