1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System.Threading;
23 
24 namespace Lucene.Net.Support
25 {
26     /// <summary>
27     /// Abstract base class that provides a synchronization interface
28     /// for derived lock types
29     /// </summary>
30     public abstract class ThreadLock
31     {
Enter(object obj)32         public abstract void Enter(object obj);
Exit(object obj)33         public abstract void Exit(object obj);
34 
35         private static readonly ThreadLock _nullLock = new NullThreadLock();
36         private static readonly ThreadLock _monitorLock = new MonitorThreadLock();
37 
38         /// <summary>
39         /// A ThreadLock class that actually does no locking
40         /// Used in ParallelMultiSearcher/MultiSearcher
41         /// </summary>
42         public static ThreadLock NullLock
43         {
44             get { return _nullLock; }
45         }
46 
47         /// <summary>
48         /// Wrapper class for the Monitor Enter/Exit methods
49         /// using the <see cref="ThreadLock"/> interface
50         /// </summary>
51         public static ThreadLock MonitorLock
52         {
53             get { return _monitorLock; }
54         }
55 
56         private sealed class NullThreadLock : ThreadLock
57         {
Enter(object obj)58             public override void Enter(object obj)
59             {
60                 // Do nothing
61             }
62 
Exit(object obj)63             public override void Exit(object obj)
64             {
65                 // Do nothing
66             }
67         }
68 
69         private sealed class MonitorThreadLock : ThreadLock
70         {
Enter(object obj)71             public override void Enter(object obj)
72             {
73                 Monitor.Enter(obj);
74             }
75 
Exit(object obj)76             public override void Exit(object obj)
77             {
78                 Monitor.Exit(obj);
79             }
80         }
81     }
82 }
83