HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EXPR_Lock.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: EXPR_Lock.h (EXPR Library, C++)
7  *
8  * COMMENTS: This header allows us to enable/disable locking when evaluating
9  * parameters and hscript expressions.
10  */
11 
12 #ifndef __EXPR_LOCK_H_INCLUDED__
13 #define __EXPR_LOCK_H_INCLUDED__
14 
15 #include "EXPR_API.h"
16 
17 #include <UT/UT_LockUtil.h>
18 #include <UT/UT_TaskLock.h>
19 #include <UT/UT_TBBSpinLock.h>
20 #include <SYS/SYS_AtomicInt.h>
21 #include <SYS/SYS_Types.h>
22 
23 // Modify this to enable/disable parm expr locking
24 #define EXPR_LOCKS 1
25 
26 #if H_BUILD_PARALLEL_COOK_MODE
27 class EXPR_API EXPR_StaticLock : private UT_TaskLock
28 {
29 public:
30 
31  /// Performs the functor F while inside this lock scope in UT_TaskArena.
32  /// This method allows the optimization that if we're calling this while
33  /// the lock is already held in the same task scope, then we can avoid
34  /// creating an unnecessary task arena.
35  template <typename F>
36  void
37  lockedExecute(const F &functor)
38  {
39  theStaticCounter.add(1);
40 
41  if (theIsDisabled)
42  {
43  functor();
44  }
45  else
46  {
47  bool was_first = false;
48  (void) UT_TaskLock::lock(was_first);
49  if (!was_first)
50  {
51  functor();
52  }
53  else
54  {
55  UT_TaskArena arena;
56  arena.execute(functor);
57  }
59  }
60  }
61 
62  /// Performs the functor F while inside this lock scope OUTSIDE an arena.
63  /// @note Only do this if you know functor will never spawn tasks!
64  template <typename F>
65  void
66  lockedExecuteWithoutArena(const F &functor)
67  {
68  theStaticCounter.add(1);
69 
70  if (theIsDisabled)
71  {
72  functor();
73  }
74  else
75  {
77  functor();
79  }
80  }
81 
83 
84  static SYS_AtomicCounter theStaticCounter;
85  static bool theIsDisabled;
86 };
87 #else
89 #endif
90 
91 #if EXPR_LOCKS
94 #else
95  typedef UT_NullLock EXPR_Lock;
97 #endif
98 
100 
101 /// The global evaluation lock is a global mutex for protecting any
102 /// non-threadsafe evaluation (both expressions and node) code. This is
103 /// necessary to guard against code that might access global objects.
105 
106 #endif // __EXPR_LOCK_H_INCLUDED__
EXPR_StaticLock EXPR_GlobalStaticLock
Definition: EXPR_Lock.h:93
void execute(F &functor)
Definition: UT_TaskArena.h:37
UT_TBBSpinLock EXPR_Lock
Definition: EXPR_Lock.h:92
EXPR_Lock::Scope EXPR_AutoLock
Definition: EXPR_Lock.h:99
bool hasLock()
Definition: UT_TaskLock.h:259
void
Definition: png.h:1083
UT_TaskLockWithArena EXPR_StaticLock
Definition: EXPR_Lock.h:88
UT_TaskLock that avoids deadlocks when used with TBB task scheduling.
Definition: UT_TaskLock.h:286
void unlock()
Definition: UT_TaskLock.h:254
EXPR_API EXPR_GlobalStaticLock & ev_GlobalEvalLock()
#define EXPR_API
Definition: EXPR_API.h:10