]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: move Gil implementation into .cc
authorJohn Spray <john.spray@redhat.com>
Tue, 22 Aug 2017 15:38:25 +0000 (11:38 -0400)
committerJohn Spray <john.spray@redhat.com>
Mon, 16 Oct 2017 11:06:23 +0000 (07:06 -0400)
The inclusion of Python.h in the .h was awkward
for other files including Gil.h.

Signed-off-by: John Spray <john.spray@redhat.com>
src/CMakeLists.txt
src/mgr/Gil.cc [new file with mode: 0644]
src/mgr/Gil.h

index 16ea6876ec97eda1a2754eb4ddfc3d9b77fcc2c5..62a6ecc5d6eeed90d48df17fcdfa2015f1df99e0 100644 (file)
@@ -720,6 +720,7 @@ if (WITH_MGR)
       mgr/MgrPyModule.cc
       mgr/MgrStandby.cc
       mgr/Mgr.cc
+      mgr/Gil.cc
       mgr/mgr_commands.cc)
   add_executable(ceph-mgr ${mgr_srcs}
                  $<TARGET_OBJECTS:heap_profiler_objs>)
diff --git a/src/mgr/Gil.cc b/src/mgr/Gil.cc
new file mode 100644 (file)
index 0000000..53ecf99
--- /dev/null
@@ -0,0 +1,72 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2017 SUSE LLC
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+
+#include "Python.h"
+
+#include "common/debug.h"
+
+#define dout_context g_ceph_context
+#define dout_subsys ceph_subsys_mgr
+#undef dout_prefix
+#define dout_prefix *_dout << "mgr " << __func__ << " "
+
+#include "Gil.h"
+
+Gil::Gil(PyThreadState *ts, bool new_thread) : pThreadState(ts)
+{
+  assert(pThreadState != nullptr);
+
+  // Acquire the GIL, set the current thread state
+  PyEval_RestoreThread(pThreadState);
+  dout(25) << "GIL acquired for thread state " << pThreadState << dendl;
+
+  //
+  // If called from a separate OS thread (i.e. a thread not created
+  // by Python, that does't already have a python thread state that
+  // was created when that thread was active), we need to manually
+  // create and switch to a python thread state specifically for this
+  // OS thread.
+  //
+  // Note that instead of requring the caller to set new_thread == true
+  // when calling this from a separate OS thread, we could figure out
+  // if this was necessary automatically, as follows:
+  //
+  //   if (pThreadState->thread_id != PyThread_get_thread_ident()) {
+  //
+  // However, this means we're accessing pThreadState->thread_id, but
+  // the Python C API docs say that "The only public data member is
+  // PyInterpreterState *interp", i.e. doing this would violate
+  // something that's meant to be a black box.
+  //
+  if (new_thread) {
+    pNewThreadState = PyThreadState_New(pThreadState->interp);
+    PyThreadState_Swap(pNewThreadState);
+    dout(20) << "Switched to new thread state " << pNewThreadState << dendl;
+  }
+}
+
+Gil::~Gil()
+{
+  if (pNewThreadState != nullptr) {
+    dout(20) << "Destroying new thread state " << pNewThreadState << dendl;
+    PyThreadState_Swap(pThreadState);
+    PyThreadState_Clear(pNewThreadState);
+    PyThreadState_Delete(pNewThreadState);
+  }
+  // Release the GIL, reset the thread state to NULL
+  PyEval_SaveThread();
+  dout(25) << "GIL released for thread state " << pThreadState << dendl;
+}
+
index 0c401c0a912347f53fe61d7fdcbeae6d2b935872..28ec8a5f389704566a5e5769e93d03104799eeb5 100644 (file)
  *
  */
 
-#ifndef GIL_H_
-#define GIL_H_
+#pragma once
 
-#include "Python.h"
+struct _ts;
+typedef struct _ts PyThreadState;
 
-#include "common/debug.h"
-
-#define dout_context g_ceph_context
-#define dout_subsys ceph_subsys_mgr
-#undef dout_prefix
-#define dout_prefix *_dout << "mgr " << __func__ << " "
-
-//
 // Use one of these in any scope in which you need to hold Python's
 // Global Interpreter Lock.
 //
 // If in doubt, explicitly put a scope around the block of code you
 // know you need the GIL in.
 //
-// See the comment below for when to set new_thread == true
+// See the comment in Gil::Gil for when to set new_thread == true
 //
 class Gil {
 public:
   Gil(const Gil&) = delete;
   Gil& operator=(const Gil&) = delete;
 
-  Gil(PyThreadState *ts, bool new_thread = false) : pThreadState(ts)
-  {
-    assert(pThreadState != nullptr);
-
-    // Acquire the GIL, set the current thread state
-    PyEval_RestoreThread(pThreadState);
-    dout(25) << "GIL acquired for thread state " << pThreadState << dendl;
-
-    //
-    // If called from a separate OS thread (i.e. a thread not created
-    // by Python, that does't already have a python thread state that
-    // was created when that thread was active), we need to manually
-    // create and switch to a python thread state specifically for this
-    // OS thread.
-    //
-    // Note that instead of requring the caller to set new_thread == true
-    // when calling this from a separate OS thread, we could figure out
-    // if this was necessary automatically, as follows:
-    //
-    //   if (pThreadState->thread_id != PyThread_get_thread_ident()) {
-    //
-    // However, this means we're accessing pThreadState->thread_id, but
-    // the Python C API docs say that "The only public data member is
-    // PyInterpreterState *interp", i.e. doing this would violate
-    // something that's meant to be a black box.
-    //
-    if (new_thread) {
-      pNewThreadState = PyThreadState_New(pThreadState->interp);
-      PyThreadState_Swap(pNewThreadState);
-      dout(20) << "Switched to new thread state " << pNewThreadState << dendl;
-    }
-  }
-
-  ~Gil()
-  {
-    if (pNewThreadState != nullptr) {
-      dout(20) << "Destroying new thread state " << pNewThreadState << dendl;
-      PyThreadState_Swap(pThreadState);
-      PyThreadState_Clear(pNewThreadState);
-      PyThreadState_Delete(pNewThreadState);
-    }
-    // Release the GIL, reset the thread state to NULL
-    PyEval_SaveThread();
-    dout(25) << "GIL released for thread state " << pThreadState << dendl;
-  }
+  Gil(PyThreadState *ts, bool new_thread = false);
+  ~Gil();
 
 private:
   PyThreadState *pThreadState;
   PyThreadState *pNewThreadState = nullptr;
 };
 
-#endif
-