]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2017 SUSE LLC
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.
18 #include "common/debug.h"
20 #define dout_context g_ceph_context
21 #define dout_subsys ceph_subsys_mgr
23 #define dout_prefix *_dout << "mgr " << __func__ << " "
27 SafeThreadState::SafeThreadState(PyThreadState
*ts_
)
30 ceph_assert(ts
!= nullptr);
31 thread
= pthread_self();
34 Gil::Gil(SafeThreadState
&ts
, bool new_thread
) : pThreadState(ts
)
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
;
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
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:
51 // if (pThreadState->thread_id != PyThread_get_thread_ident()) {
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.
59 pNewThreadState
= PyThreadState_New(pThreadState
.ts
->interp
);
60 PyThreadState_Swap(pNewThreadState
);
61 dout(20) << "Switched to new thread state " << pNewThreadState
<< dendl
;
63 ceph_assert(pthread_self() == pThreadState
.thread
);
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
);
75 // Release the GIL, reset the thread state to NULL
77 dout(25) << "GIL released for thread state " << pThreadState
.ts
<< dendl
;
80 without_gil_t::without_gil_t()
82 assert(PyGILState_Check());
86 without_gil_t::~without_gil_t()
93 void without_gil_t::release_gil()
95 save
= PyEval_SaveThread();
98 void without_gil_t::acquire_gil()
101 PyEval_RestoreThread(save
);
105 with_gil_t::with_gil_t(without_gil_t
& allow_threads
)
106 : allow_threads
{allow_threads
}
108 allow_threads
.acquire_gil();
111 with_gil_t::~with_gil_t()
113 allow_threads
.release_gil();