Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
LockMechanisms.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Threading;
8 
9 namespace SiliconStudio.Paradox.Threading
10 {
11  public static class Locks
12  {
13  public static ILockMechanism Default;
14  public static readonly ILockMechanism Standard = new StandardLock();
15  public static readonly ILockMechanism Global = new GlobalLock();
16 
17  private static ILockMechanism _originalDefaultLockMechanism;
18 
19  static Locks()
20  {
21  SetDefaultLockMechanism(Standard);
22  _originalDefaultLockMechanism = Default;
23  }
24 
25  public static void SetDefaultLockMechanism(ILockMechanism defaultLockMechanism)
26  {
27  if (defaultLockMechanism == null)
28  throw new ArgumentNullException("defaultLockMechanism");
29 
30  Default = defaultLockMechanism;
31  }
32 
34  {
35  ILockMechanism previous = Default;
36  Default = _originalDefaultLockMechanism;
37  return previous;
38  }
39  }
40 
42  {
43  public object OnBegin(object syncRoot, Action action)
44  {
45  Monitor.Enter(syncRoot);
46  return syncRoot;
47  }
48 
49  public void OnEnd(object syncRoot)
50  {
51  Monitor.Exit(syncRoot);
52  }
53  }
54 
55  public class GlobalLock : ILockMechanism
56  {
57  private static readonly object _globalLock = new object();
58 
59  public object OnBegin(object syncRoot, Action action)
60  {
61  Monitor.Enter(_globalLock);
62  return null;
63  }
64 
65  public void OnEnd(object syncRoot)
66  {
67  Monitor.Exit(_globalLock);
68  }
69  }
70 }
static void SetDefaultLockMechanism(ILockMechanism defaultLockMechanism)
object OnBegin(object syncRoot, Action action)
static ILockMechanism RestoreDefaultLockMechanism()
object OnBegin(object syncRoot, Action action)