source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Physics2DDotNet/Collections/AdvReaderWriterLock.cs @ 4507

Revision 4507, 3.6 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26using System;
27using System.Collections.Generic;
28using System.Threading;
29namespace Physics2DDotNet.Collections
30{
31    public sealed class AdvReaderWriterLock
32    {
33        public sealed class WriterLock : IDisposable
34        {
35            AdvReaderWriterLock rwLock;
36            public WriterLock(AdvReaderWriterLock rwLock)
37            {
38                this.rwLock = rwLock;
39                rwLock.EnterWrite();
40            }
41            public void Dispose()
42            {
43                rwLock.ExitWrite();
44            }
45        }
46        public sealed class ReaderLock : IDisposable
47        {
48            AdvReaderWriterLock rwLock;
49            public ReaderLock(AdvReaderWriterLock rwLock)
50            {
51                this.rwLock = rwLock;
52                rwLock.EnterRead();
53            }
54            public void Dispose()
55            {
56                rwLock.ExitRead();
57            }
58        }
59
60#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
61        ReaderWriterLock rwLock = new ReaderWriterLock();
62#else
63        object syncRoot = new object();
64#endif
65        public WriterLock Write
66        {
67            get
68            {
69                return new WriterLock(this);
70            }
71        }
72        public ReaderLock Read
73        {
74            get
75            {
76                return new ReaderLock(this);
77            }
78        }
79
80        public void EnterRead()
81        {
82#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
83            rwLock.AcquireReaderLock(Timeout.Infinite);
84#else
85            Monitor.Enter(syncRoot);
86#endif
87        }
88        public void EnterWrite()
89        {
90#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
91            rwLock.AcquireWriterLock(Timeout.Infinite);
92#else
93            Monitor.Enter(syncRoot);
94#endif
95        }
96        public void ExitRead()
97        {
98#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
99            rwLock.ReleaseReaderLock();
100#else
101            Monitor.Exit(syncRoot);
102#endif
103        }
104        public void ExitWrite()
105        {
106#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
107            rwLock.ReleaseWriterLock();
108#else
109            Monitor.Exit(syncRoot);
110#endif
111        }
112    }
113
114}
Note: See TracBrowser for help on using the repository browser.