]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/Gil.cc
import ceph pacific 16.2.5
[ceph.git] / 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 ceph_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 ceph_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
80 without_gil_t::without_gil_t()
81 {
82 assert(PyGILState_Check());
83 release_gil();
84 }
85
86 without_gil_t::~without_gil_t()
87 {
88 if (save) {
89 acquire_gil();
90 }
91 }
92
93 void without_gil_t::release_gil()
94 {
95 save = PyEval_SaveThread();
96 }
97
98 void without_gil_t::acquire_gil()
99 {
100 assert(save);
101 PyEval_RestoreThread(save);
102 save = nullptr;
103 }
104
105 with_gil_t::with_gil_t(without_gil_t& allow_threads)
106 : allow_threads{allow_threads}
107 {
108 allow_threads.acquire_gil();
109 }
110
111 with_gil_t::~with_gil_t()
112 {
113 allow_threads.release_gil();
114 }