moveit2
The MoveIt Motion Planning Framework for ROS 2.
gil_releaser.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, RWTH Aachen University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of RWTH Aachen University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Bjarne von Horn */
36 
37 #pragma once
38 
39 #include <utility>
40 #include <Python.h>
41 
42 namespace moveit
43 {
44 namespace py_bindings_tools
45 {
55 {
56  PyThreadState* m_thread_state;
57 
58 public:
60  GILReleaser() noexcept
61  {
62  m_thread_state = PyEval_SaveThread();
63  }
65  ~GILReleaser() noexcept
66  {
67  if (m_thread_state)
68  {
69  PyEval_RestoreThread(m_thread_state);
70  m_thread_state = nullptr;
71  }
72  }
73 
74  GILReleaser(const GILReleaser&) = delete;
75  GILReleaser(GILReleaser&& other) noexcept
76  {
77  m_thread_state = other.m_thread_state;
78  other.m_thread_state = nullptr;
79  }
80 
81  GILReleaser& operator=(const GILReleaser&) = delete;
82  GILReleaser& operator=(GILReleaser&& other) noexcept
83  {
84  GILReleaser copy(std::move(other));
85  this->swap(copy);
86  return *this;
87  }
88 
89  void swap(GILReleaser& other) noexcept
90  {
91  std::swap(other.m_thread_state, m_thread_state);
92  }
93 };
94 
95 } // namespace py_bindings_tools
96 } // namespace moveit
RAII Helper to release the Global Interpreter Lock (GIL)
Definition: gil_releaser.h:55
GILReleaser(GILReleaser &&other) noexcept
Definition: gil_releaser.h:75
GILReleaser & operator=(GILReleaser &&other) noexcept
Definition: gil_releaser.h:82
~GILReleaser() noexcept
Reacquire the GIL on destruction
Definition: gil_releaser.h:65
GILReleaser(const GILReleaser &)=delete
GILReleaser & operator=(const GILReleaser &)=delete
void swap(GILReleaser &other) noexcept
Definition: gil_releaser.h:89
GILReleaser() noexcept
Release the GIL on construction
Definition: gil_releaser.h:60
Main namespace for MoveIt.
Definition: exceptions.h:43