ecto
python.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #pragma once
30 //do not include this in ecto lib files, only in client modules
31 
32 #pragma push_macro("_POSIX_C_SOURCE")
33 #pragma push_macro("_XOPEN_SOURCE")
34 #undef _POSIX_C_SOURCE
35 #undef _XOPEN_SOURCE
36 #include <Python.h>
37 #include <boost/python.hpp>
38 #include <boost/thread/thread.hpp>
39 #include <map>
40 //#pragma pop_macro("_POSIX_C_SOURCE")
41 //#pragma pop_macro("_XOPEN_C_SOURCE")
42 
43 #include <boost/noncopyable.hpp>
44 
45 namespace ecto {
46  namespace py {
47 
48  struct gilstatus
49  {
50  const char* file;
51  unsigned line;
52  const char* what;
53  gilstatus(const char* f, unsigned l, const char* w);
54  };
55 
56  //
57  // used in the interpreter thread during blocking operations
58  // RIAA-style Py_BEGIN_ALLOW_THREADS
59  //
60  class scoped_gil_release : boost::noncopyable
61  {
62  static std::map<boost::thread::id, PyThreadState*> thread_states;
63  bool mine;
65  public:
66  scoped_gil_release(const char* file, unsigned line);
68  };
69 
70  //
71  // For non-python created threads
72  //
73  class scoped_call_back_to_python : boost::noncopyable
74  {
75  PyGILState_STATE gilstate;
76  bool have;
78 
79  public:
80 
81  scoped_call_back_to_python(const char* file, unsigned line);
83  };
84  }
85 }
86 
87 #define ECTO_SCOPED_GILRELEASE() ecto::py::scoped_gil_release gilrelease ## __LINE__(__FILE__, __LINE__)
88 #define ECTO_SCOPED_CALLPYTHON() ecto::py::scoped_call_back_to_python gilcall ## __LINE__(__FILE__, __LINE__)
gilstatus mystatus
Definition: python.hpp:77
PyGILState_STATE gilstate
Definition: python.hpp:75
Definition: parameters.hpp:11
unsigned line
Definition: python.hpp:51
const char * what
Definition: python.hpp:52
static std::map< boost::thread::id, PyThreadState * > thread_states
Definition: python.hpp:62
const char * file
Definition: python.hpp:50
gilstatus(const char *f, unsigned l, const char *w)
gilstatus mystatus
Definition: python.hpp:64
bool mine
Definition: python.hpp:63
Definition: python.hpp:48
Definition: python.hpp:60
bool have
Definition: python.hpp:76
Definition: python.hpp:73