HDK
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
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_Thread.h
>
24
25
class
PY_API
PY_InterpreterAutoLock
26
{
27
public
:
28
PY_InterpreterAutoLock
();
29
~
PY_InterpreterAutoLock
();
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
// }
70
class
PY_API
PY_InterpreterAutoUnlock
71
{
72
public
:
73
PY_InterpreterAutoUnlock
();
74
~
PY_InterpreterAutoUnlock
();
75
76
private
:
77
void
*mySavedThreadState;
78
bool
myReleasedImportLock;
79
};
80
81
// Return true if we've been initialized
82
PY_API
bool
PYisPythonInitialized
();
83
// Calling this method is only handy if you need to initialize the PY CPython
84
// portion but not initialize hou. ONLY call this method with
85
// `ensure_hou_is_initialized` false if you know exactly what you are doing!!!
86
// If you are running this method with `ensure_hou_is_initialized` True
87
// DO NOT CALL THIS METHOD as you are doing something wrong!!! The locks above
88
// will do the initialization for you if it hasnt already be done.
89
PY_API
void
PYmakeSurePythonIsInitialized
(
bool
ensure_hou_is_initialized);
90
91
#endif
PY_InterpreterAutoLock
Definition:
PY_InterpreterAutoLock.h:25
PY_API.h
PY_InterpreterAutoUnlock
Definition:
PY_InterpreterAutoLock.h:70
PYisPythonInitialized
PY_API bool PYisPythonInitialized()
PY_API
#define PY_API
Definition:
PY_API.h:10
PYmakeSurePythonIsInitialized
PY_API void PYmakeSurePythonIsInitialized(bool ensure_hou_is_initialized)
UT_Thread.h
PY
PY_InterpreterAutoLock.h
Generated on Fri May 27 2022 03:18:27 for HDK by
1.8.6