Tomographer  v5.2
Tomographer C++ Framework Documentation
pygil.h
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 
29 #ifndef TOMOGRAPHER_PY_PYGIL_H
30 #define TOMOGRAPHER_PY_PYGIL_H
31 
32 #include <vector>
33 #include <algorithm>
34 
35 #include <tomographerpy/common.h>
36 
39 
40 #include <tomographerpy/pylogger.h>
41 
42 
43 #define TPY_EXPR_WITH_GIL( x ) \
44  [&]() { py::gil_scoped_acquire _tpy_gil; return (x); } ()
45 
46 
47 
48 
49 namespace tpy {
50  class GilProtectedPyLogger; // forward declaration
51 } // namespace tpy
52 namespace Tomographer { namespace Logger {
53  // Traits for PyLogger
54  template<>
55  struct TOMOGRAPHER_EXPORT LoggerTraits<tpy::GilProtectedPyLogger> : public DefaultLoggerTraits
56  {
57  enum {
58  // This logger is thread-safe because it makes sure that it acquires the GIL as
59  // needed.
60  IsThreadSafe = 1
61  };
62  };
63 } } // namespaces Tomographer::Logger
64 
65 
66 namespace tpy {
67 
105 class TOMOGRAPHER_EXPORT GilProtectedPyLogger
106  : public Tomographer::Logger::LoggerBase<GilProtectedPyLogger>
107 {
108 public:
115  inline GilProtectedPyLogger(PyLogger & logger_, bool require_gil_acquisition_ = true)
116  : LoggerBase<GilProtectedPyLogger>(logger_.level()), // freeze to current level
117  logger(logger_),
118  require_gil_acquisition(require_gil_acquisition_)
119  {
120  }
121 
123  inline PyLogger & getLogger() const { return logger; }
124 
126  inline bool getRequireGilAcquisition() const { return require_gil_acquisition; }
127 
129  inline void requireGilAcquisition(bool value) {
130  tomographer_assert(require_gil_acquisition != value);
131  require_gil_acquisition = value;
132  }
133 
135  inline void emitLog(int level, const char * origin, const std::string & msg)
136  {
137  if (require_gil_acquisition) {
138  py::gil_scoped_acquire gil_acquire;
139  logger.emitLog(level, origin, msg);
140  } else {
141  logger.emitLog(level, origin, msg);
142  }
143  }
144 
145 private:
146  PyLogger & logger;
147  bool require_gil_acquisition;
148 };
149 
150 } // namespace tpy
151 
152 
153 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:45
Logger providing transparent integration with Python&#39;s logging module.
Definition: pylogger.h:92
Base logger class.
Definition: loggers.h:444
STL class.
void emitLog(int level, const char *origin, const std::string &msg)
The callback function for the logger, you shouldn&#39;t call this directly.
Definition: pygil.h:135
PyLogger & getLogger() const
The PyLogger which we relay messages to.
Definition: pygil.h:123
void requireGilAcquisition(bool value)
Instruct to acquire (true) or not (false) the GIL when emitting messages.
Definition: pygil.h:129
Some C++ utilities, with a tad of C++11 tricks.
GilProtectedPyLogger(PyLogger &logger_, bool require_gil_acquisition_=true)
Constructor.
Definition: pygil.h:115
Default traits for Logger implementations.
Definition: loggers.h:288
Traits template struct to be specialized for specific Logger implementations.
Definition: loggers.h:351
VarValueDecoder< T >::RetType value(const Var &var)
Access the value of the given variable, as a C++ type.
Definition: ezmatio.h:878
bool getRequireGilAcquisition() const
Whether or not we are set to acquire the GIL for emitting messages at this point. ...
Definition: pygil.h:126
C++ Classes and Utilities for Python Modules.
Definition: common.h:87
Logger type that relays to (wraps around) a PyLogger while protecting the call with GIL acquisition...
Definition: pygil.h:105
#define tomographer_assert(...)
Assertion test macro.
Definition: cxxdefs.h:84
Utilities for logging messages.