HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PY_InterpreterAutoLock.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  * COMMENTS:
7  * Use PY_InterpreterAutoLock to automatically lock blocks of code
8  * that call into the python interpreter. You cannot make any python API
9  * calls without having the global interpreter lock. It's okay to have
10  * multiple levels of locks. The HOMF_Utils functions and the methods
11  * of PY_CompiledCode already use this class, so you don't need to
12  * use this lock with those classes.
13  *
14  * Note that this header avoids including the Python headers. This
15  * helps avoid warnings that can occur when the Python headers are not
16  * the first headers included.
17  */
18 
19 #ifndef __PY_InterpreterAutoLock_h__
20 #define __PY_InterpreterAutoLock_h__
21 
22 #include "PY_API.h"
23 #include <UT/UT_Thread.h>
24 
26 {
27 public:
30 
31 private:
32  int myGlblInterpLockState;
33 };
34 
35 
36 // A PY_InterpreterAutoUnlock lets you release the Python GIL, if it was
37 // held, to let other Python threads run while you make a time-consuming or
38 // blocking call. It's ok to use an auto unlock in code that's not being
39 // called from the Python interpreter. For example, HOMF code is usually
40 // invoked from the Python interpreter, but it may be invoked via C++ from
41 // other places in Houdini, and it's safe to use an auto unlock in both cases.
42 //
43 // Using this class is roughly equivalent to the Py_BEGIN_ALLOW_THREADS and
44 // Py_END_ALLOW_THREADS macros, except it's safe to use it when not invoked
45 // from Python. It also ensures that the GIL is reacquired if an exception
46 // is thrown.
47 //
48 // Note that this class does not release the HOM lock; use HOM_AutoUnlock in
49 // combination with this class to release them both. Be sure to create
50 // the HOM_AutoUnlock *after* the PY_InterpreterAutoUnlock, so that when
51 // the auto locks are destructed the HOM lock is reacquired before the Python
52 // GIL. Otherwise, it will grab the GIL and then try to grab the HOM lock,
53 // and if another thread with the HOM lock tries to run Python we'll end up
54 // with deadlock.
55 //
56 // A sample use of this class is:
57 //
58 // {
59 // PY_InterpreterAutoLock interpreter_lock;
60 // ... make Python API calls ...
61 //
62 // {
63 // PY_InterpreterAutoUnlock interpreter_unlock;
64 // // Other Python threads are now free to run.
65 // ... make a blocking I/O call ...
66 // }
67 //
68 // ... continue to make Python API calls ...
69 // }
71 {
72 public:
75 
76 private:
77  void *mySavedThreadState;
78 };
79 
80 // Return true if we've been initialized
82 
83 #endif
PY_API bool PYisPythonInitialized()
#define PY_API
Definition: PY_API.h:10