00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Side Effects Software Inc 00008 * 123 Front Street West, Suite 1401 00009 * Toronto, Ontario 00010 * Canada M5J 2M2 00011 * 416-504-9876 00012 * 00013 * COMMENTS: 00014 * Use PY_InterpreterAutoLock to automatically lock blocks of code 00015 * that call into the python interpreter. You cannot make any python API 00016 * calls without having the global interpreter lock. It's okay to have 00017 * multiple levels of locks. The HOMF_Utils functions and the methods 00018 * of PY_CompiledCode already use this class, so you don't need to 00019 * use this lock with those classes. 00020 * 00021 * Note that this header avoids including the Python headers. This 00022 * helps avoid warnings that can occur when the Python headers are not 00023 * the first headers included. 00024 */ 00025 00026 #ifndef __PY_InterpreterAutoLock_h__ 00027 #define __PY_InterpreterAutoLock_h__ 00028 00029 #include "PY_API.h" 00030 #include <UT/UT_Thread.h> 00031 00032 class PY_API PY_InterpreterAutoLock 00033 { 00034 public: 00035 PY_InterpreterAutoLock(); 00036 ~PY_InterpreterAutoLock(); 00037 00038 private: 00039 int myGlblInterpLockState; 00040 }; 00041 00042 00043 // A PY_InterpreterAutoUnlock lets you release the Python GIL, if it was 00044 // held, to let other Python threads run while you make a time-consuming or 00045 // blocking call. It's ok to use an auto unlock in code that's not being 00046 // called from the Python interpreter. For example, HOMF code is usually 00047 // invoked from the Python interpreter, but it may be invoked via C++ from 00048 // other places in Houdini, and it's safe to use an auto unlock in both cases. 00049 // 00050 // Using this class is roughly equivalent to the Py_BEGIN_ALLOW_THREADS and 00051 // Py_END_ALLOW_THREADS macros, except it's safe to use it when not invoked 00052 // from Python. It also ensures that the GIL is reacquired if an exception 00053 // is thrown. 00054 // 00055 // Note that this class does release the HOM lock; use HOM_AutoUnlock in 00056 // combination with this class to release them both. Be sure to create 00057 // the HOM_AutoUnlock *after* the PY_InterpreterAutoUnlock, so that when 00058 // the auto locks are destructed the HOM lock is reacquired before the Python 00059 // GIL. Otherwise, it will grab the GIL and then try to grab the HOM lock, 00060 // and if another thread with the HOM lock tries to run Python we'll end up 00061 // with deadlock. 00062 // 00063 // A sample use of this class is: 00064 // 00065 // { 00066 // PY_InterpreterAutoLock interpreter_lock; 00067 // ... make Python API calls ... 00068 // 00069 // { 00070 // PY_InterpreterAutoUnlock interpreter_unlock; 00071 // // Other Python threads are now free to run. 00072 // ... make a blocking I/O call ... 00073 // } 00074 // 00075 // ... continue to make Python API calls ... 00076 // } 00077 class PY_API PY_InterpreterAutoUnlock 00078 { 00079 public: 00080 PY_InterpreterAutoUnlock(); 00081 ~PY_InterpreterAutoUnlock(); 00082 00083 private: 00084 void *mySavedThreadState; 00085 }; 00086 00087 #endif
1.5.9