]>
Commit | Line | Data |
---|---|---|
3efd9988 FG |
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" | |
b32b8144 | 18 | #include "common/LogClient.h" |
3efd9988 FG |
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 | ||
b32b8144 FG |
40 | LogChannelRef clog; |
41 | ||
3efd9988 FG |
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_, | |
b32b8144 FG |
61 | const SafeThreadState &pMyThreadState_, |
62 | LogChannelRef clog_) | |
3efd9988 FG |
63 | : |
64 | module_name(module_name_), | |
65 | pClass(pClass_), pMyThreadState(pMyThreadState_), | |
b32b8144 | 66 | clog(clog_), |
3efd9988 FG |
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 |