Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mgr / Gil.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2017 SUSE LLC
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15
16 #include "Python.h"
17
18 #include "common/debug.h"
19
20 #define dout_context g_ceph_context
21 #define dout_subsys ceph_subsys_mgr
22 #undef dout_prefix
23 #define dout_prefix *_dout << "mgr " << __func__ << " "
24
25 #include "Gil.h"
26
27 SafeThreadState::SafeThreadState(PyThreadState *ts_)
28     : ts(ts_)
29 {
30   assert(ts != nullptr);
31   thread = pthread_self();
32 }
33
34 Gil::Gil(SafeThreadState &ts, bool new_thread) : pThreadState(ts)
35 {
36   // Acquire the GIL, set the current thread state
37   PyEval_RestoreThread(pThreadState.ts);
38   dout(25) << "GIL acquired for thread state " << pThreadState.ts << dendl;
39
40   //
41   // If called from a separate OS thread (i.e. a thread not created
42   // by Python, that does't already have a python thread state that
43   // was created when that thread was active), we need to manually
44   // create and switch to a python thread state specifically for this
45   // OS thread.
46   //
47   // Note that instead of requring the caller to set new_thread == true
48   // when calling this from a separate OS thread, we could figure out
49   // if this was necessary automatically, as follows:
50   //
51   //   if (pThreadState->thread_id != PyThread_get_thread_ident()) {
52   //
53   // However, this means we're accessing pThreadState->thread_id, but
54   // the Python C API docs say that "The only public data member is
55   // PyInterpreterState *interp", i.e. doing this would violate
56   // something that's meant to be a black box.
57   //
58   if (new_thread) {
59     pNewThreadState = PyThreadState_New(pThreadState.ts->interp);
60     PyThreadState_Swap(pNewThreadState);
61     dout(20) << "Switched to new thread state " << pNewThreadState << dendl;
62   } else {
63     assert(pthread_self() == pThreadState.thread);
64   }
65 }
66
67 Gil::~Gil()
68 {
69   if (pNewThreadState != nullptr) {
70     dout(20) << "Destroying new thread state " << pNewThreadState << dendl;
71     PyThreadState_Swap(pThreadState.ts);
72     PyThreadState_Clear(pNewThreadState);
73     PyThreadState_Delete(pNewThreadState);
74   }
75   // Release the GIL, reset the thread state to NULL
76   PyEval_SaveThread();
77   dout(25) << "GIL released for thread state " << pThreadState.ts << dendl;
78 }
79