HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_NonCopyable.h>
24 #include <UT/UT_Thread.h>
25 
26 class UT_WorkArgs;
27 
28 // NB: All code should be using PY_InterpeterAutoUnlock unless in the rare case
29 // they dont want to use a license (eg. hwebserver and haio)
31 {
32 protected:
33  PY_BaseInterpreterAutoLock(bool ensure_hou_is_initialized);
36 private:
37  int myGlblInterpLockState;
38 };
39 
41 {
42 public:
44  PY_BaseInterpreterAutoLock(true /*ensure_hou_is_initialized*/)
45  {}
46 };
47 
48 // A PY_InterpreterAutoUnlock lets you release the Python GIL, if it was
49 // held, to let other Python threads run while you make a time-consuming or
50 // blocking call. It's ok to use an auto unlock in code that's not being
51 // called from the Python interpreter. For example, HOMF code is usually
52 // invoked from the Python interpreter, but it may be invoked via C++ from
53 // other places in Houdini, and it's safe to use an auto unlock in both cases.
54 //
55 // Using this class is roughly equivalent to the Py_BEGIN_ALLOW_THREADS and
56 // Py_END_ALLOW_THREADS macros, except it's safe to use it when not invoked
57 // from Python. It also ensures that the GIL is reacquired if an exception
58 // is thrown.
59 //
60 // Note that this class does not release the HOM lock; use HOM_AutoUnlock in
61 // combination with this class to release them both. Be sure to create
62 // the HOM_AutoUnlock *after* the PY_InterpreterAutoUnlock, so that when
63 // the auto locks are destructed the HOM lock is reacquired before the Python
64 // GIL. Otherwise, it will grab the GIL and then try to grab the HOM lock,
65 // and if another thread with the HOM lock tries to run Python we'll end up
66 // with deadlock.
67 //
68 // A sample use of this class is:
69 //
70 // {
71 // PY_InterpreterAutoLock interpreter_lock;
72 // ... make Python API calls ...
73 //
74 // {
75 // PY_InterpreterAutoUnlock interpreter_unlock;
76 // // Other Python threads are now free to run.
77 // ... make a blocking I/O call ...
78 // }
79 //
80 // ... continue to make Python API calls ...
81 // }
82 //
83 //
84 // NB: All code should be using PY_InterpeterAutoUnlock unless in the rare case
85 // they dont want to use a license (eg. hwebserver and haio)
87 {
88 protected:
89  PY_BaseInterpreterAutoUnlock(bool ensure_hou_is_initialized);
91 
93 
94 private:
95  void *mySavedThreadState;
96  bool myReleasedImportLock;
97 };
98 
100 {
101 public:
103  PY_BaseInterpreterAutoUnlock(true /*ensure_hou_is_initialized*/)
104  {}
105 };
106 
107 // Return true if we've been initialized
109 
110 // Set the command line arguments in Python. The arguments are passed
111 // to Python at initialization time. This function has no impact if called
112 // after Python initializes. Note that `args` must exist for the entirety that
113 // Python runs. Ownership of `args` remains with the caller.
115 
116 // Calling this method is only handy if you need to initialize the PY CPython
117 // portion but not initialize hou. ONLY call this method with
118 // `ensure_hou_is_initialized` false if you know exactly what you are doing!!!
119 // If you are running this method with `ensure_hou_is_initialized` True
120 // DO NOT CALL THIS METHOD as you are doing something wrong!!! The locks above
121 // will do the initialization for you if it hasnt already be done.
122 PY_API void PYmakeSurePythonIsInitialized(bool ensure_hou_is_initialized);
123 
124 #endif
PY_API bool PYisPythonInitialized()
PY_API void PYsetArgvForPythonInitialization(const UT_WorkArgs *args)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define PY_API
Definition: PY_API.h:10
**If you just want to fire and args
Definition: thread.h:609
PY_API void PYmakeSurePythonIsInitialized(bool ensure_hou_is_initialized)