]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/PyModuleRunner.h
update sources to v12.2.3
[ceph.git] / ceph / src / mgr / PyModuleRunner.h
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) 2016 John Spray <john.spray@redhat.com>
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 #pragma once
16
17 #include "common/Thread.h"
18 #include "common/LogClient.h"
19 #include "mgr/Gil.h"
20
21 /**
22 * Implement the pattern of calling serve() on a module in a thread,
23 * until shutdown() is called.
24 */
25 class PyModuleRunner
26 {
27 protected:
28 const std::string module_name;
29
30 // Passed in by whoever loaded our python module and looked up
31 // the symbols in it.
32 PyObject *pClass = nullptr;
33
34 // Passed in by whoever created our subinterpreter for us
35 SafeThreadState pMyThreadState = nullptr;
36
37 // Populated when we construct our instance of pClass in load()
38 PyObject *pClassInstance = nullptr;
39
40 LogChannelRef clog;
41
42 class PyModuleRunnerThread : public Thread
43 {
44 PyModuleRunner *mod;
45
46 public:
47 PyModuleRunnerThread(PyModuleRunner *mod_)
48 : mod(mod_) {}
49
50 void *entry() override;
51 };
52
53 public:
54 int serve();
55 void shutdown();
56 void log(int level, const std::string &record);
57
58 PyModuleRunner(
59 const std::string &module_name_,
60 PyObject *pClass_,
61 const SafeThreadState &pMyThreadState_,
62 LogChannelRef clog_)
63 :
64 module_name(module_name_),
65 pClass(pClass_), pMyThreadState(pMyThreadState_),
66 clog(clog_),
67 thread(this)
68 {
69 assert(pClass != nullptr);
70 assert(pMyThreadState.ts != nullptr);
71 assert(!module_name.empty());
72 }
73
74 ~PyModuleRunner();
75
76 PyModuleRunnerThread thread;
77
78 std::string const &get_name() const { return module_name; }
79 };
80
81