Package pyplusplus :: Package code_repository :: Module gil_guard

# Source Code for Module pyplusplus.code_repository.gil_guard

     1  # Copyright 2004-2008 Roman Yakovenko.
     2  # Distributed under the Boost Software License, Version 1.0. (See
     3  # accompanying file LICENSE_1_0.txt or copy at
     4  # http://www.boost.org/LICENSE_1_0.txt)
     5
     6  """
     7  This file contains C++ code to acquire/release the GIL.
     8  """
     9
    10  file_name = "__gil_guard.pypp.hpp"
    11
    12  code = \
    13  """// Copyright 2004-2008 Roman Yakovenko.
    14  // Distributed under the Boost Software License, Version 1.0. (See
    15  // accompanying file LICENSE_1_0.txt or copy at
    16  // http://www.boost.org/LICENSE_1_0.txt)
    17
    18  #ifndef __gil_guard_pyplusplus_hpp__
    19  #define __gil_guard_pyplusplus_hpp__
    20
    21  namespace pyplusplus{ namespace threading {
    22
    23  class gil_guard_t
    24  {
    25      public:
    26      gil_guard_t( bool lock=false )
    27       : m_locked( false )
    28      {
    29          if( lock )
    30              ensure();
    31      }
    32
    33      ~gil_guard_t() {
    34          release();
    35      }
    36
    37      void ensure() {
    38          if( !m_locked )
    39          {
    40               m_gstate = PyGILState_Ensure();
    41               m_locked = true;
    42          }
    43      }
    44
    45      void release() {
    46          if( m_locked )
    47          {
    48               PyGILState_Release(m_gstate);
    49               m_locked = false;
    50          }
    51      }
    52
    53      private:
    54      bool m_locked;
    55      PyGILState_STATE m_gstate;
    56  };
    57
    58  } /* threading */ } /* pyplusplus*/
    59
    60
    61  #endif//__gil_guard_pyplusplus_hpp__
    62  """
    63