Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Lock.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 Lock
12  {
13  public static void Do(Action action)
14  {
15  Do(null, Locks.Global, action);
16  }
17 
18  public static void Do(object syncRoot, Action action)
19  {
20  Do(syncRoot, Locks.Default, action);
21  }
22 
23  public static void Do(object syncRoot, ILockMechanism lockMechanism, Action action)
24  {
25  if (lockMechanism == null)
26  throw new ArgumentNullException("lockMechanism");
27 
28  object workingSyncRoot = null;
29 
30  try
31  {
32  workingSyncRoot = lockMechanism.OnBegin(syncRoot, action);
33  }
34  catch (Exception beginEx)
35  {
36  throw new LockMechanismException(LockMechanismStage.OnBegin, lockMechanism, beginEx);
37  }
38 
39  try
40  {
41  action();
42  }
43  finally
44  {
45  try
46  {
47  lockMechanism.OnEnd(workingSyncRoot);
48  }
49  catch (Exception endEx)
50  {
51  throw new LockMechanismException(LockMechanismStage.OnEnd, lockMechanism, endEx);
52  }
53  }
54  }
55  }
56 }
static void Do(object syncRoot, ILockMechanism lockMechanism, Action action)
Definition: Lock.cs:23
static readonly ILockMechanism Global
static void Do(object syncRoot, Action action)
Definition: Lock.cs:18
static void Do(Action action)
Definition: Lock.cs:13