Thread: [cgkit-commits] SF.net SVN: cgkit: [45] maya/trunk/maya_wrapper/src_aux/aux_threading.h
Brought to you by:
mbaas
From: <mb...@us...> - 2006-09-22 16:49:43
|
Revision: 45 http://svn.sourceforge.net/cgkit/?rev=45&view=rev Author: mbaas Date: 2006-09-22 09:49:36 -0700 (Fri, 22 Sep 2006) Log Message: ----------- Added support code for acquiring/releasing the Python GIL. Added Paths: ----------- maya/trunk/maya_wrapper/src_aux/aux_threading.h Added: maya/trunk/maya_wrapper/src_aux/aux_threading.h =================================================================== --- maya/trunk/maya_wrapper/src_aux/aux_threading.h (rev 0) +++ maya/trunk/maya_wrapper/src_aux/aux_threading.h 2006-09-22 16:49:36 UTC (rev 45) @@ -0,0 +1,107 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is the Python Computer Graphics Kit. + * + * The Initial Developer of the Original Code is Matthias Baas. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _THREADING_H +#define _THREADING_H + +/** + \file aux_threading.h + Contains code for supporting multi-threading. +*/ + +#include <python.h> + + +/** + This class should be used for acquiring/releasing the Python GIL. + + */ +class GILGuard +{ + private: + /// Counts how often ensure() has been called. + unsigned int numlocks; + + PyGILState_STATE gstate; + + public: + /** + Constructor. + */ + GILGuard() + : numlocks(0), gstate(0) + { + } + + /** + Destructor. + + Releases the GIL if it was acquired. + */ + ~GILGuard() + { + while (numlocks>0) + release(); + } + + /** + Acquire the GIL. + + This method corresponds to the PyGILState_Ensure() function + from the Python/C API. + It can be called several times but must be matched by an + equivalent number of calls to release(). + */ + void ensure() + { + if (numlocks==0) + { + gstate = PyGILState_Ensure(); + } + numlocks++; + } + + /** + Release the GIL. + + This method corresponds to the PyGILState_Release() function + from the Python/C API. + It must be called as many times as ensure() was called. + + If it is called without that ensure() has been called, the + method returns immediately. + */ + void release() + { + if (numlocks==0) + return; + + numlocks--; + if (numlocks==0) + { + PyGILState_Release(gstate); + } + } + +}; + +#endif Property changes on: maya/trunk/maya_wrapper/src_aux/aux_threading.h ___________________________________________________________________ Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mb...@us...> - 2006-09-22 16:54:50
|
Revision: 47 http://svn.sourceforge.net/cgkit/?rev=47&view=rev Author: mbaas Date: 2006-09-22 09:54:44 -0700 (Fri, 22 Sep 2006) Log Message: ----------- Don't initialize gstate in the constructor (it is an opaque type, and it gets initialized in ensure()). Modified Paths: -------------- maya/trunk/maya_wrapper/src_aux/aux_threading.h Modified: maya/trunk/maya_wrapper/src_aux/aux_threading.h =================================================================== --- maya/trunk/maya_wrapper/src_aux/aux_threading.h 2006-09-22 16:50:31 UTC (rev 46) +++ maya/trunk/maya_wrapper/src_aux/aux_threading.h 2006-09-22 16:54:44 UTC (rev 47) @@ -48,7 +48,7 @@ Constructor. */ GILGuard() - : numlocks(0), gstate(0) + : numlocks(0) { } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |