X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fmgr%2FPyModuleRunner.h;fp=src%2Fceph%2Fsrc%2Fmgr%2FPyModuleRunner.h;h=cb51df3eaaff1132726aa0672612a916fa095ff0;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/mgr/PyModuleRunner.h b/src/ceph/src/mgr/PyModuleRunner.h new file mode 100644 index 0000000..cb51df3 --- /dev/null +++ b/src/ceph/src/mgr/PyModuleRunner.h @@ -0,0 +1,76 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2016 John Spray + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + */ + + +#pragma once + +#include "common/Thread.h" +#include "mgr/Gil.h" + +/** + * Implement the pattern of calling serve() on a module in a thread, + * until shutdown() is called. + */ +class PyModuleRunner +{ +protected: + const std::string module_name; + + // Passed in by whoever loaded our python module and looked up + // the symbols in it. + PyObject *pClass = nullptr; + + // Passed in by whoever created our subinterpreter for us + SafeThreadState pMyThreadState = nullptr; + + // Populated when we construct our instance of pClass in load() + PyObject *pClassInstance = nullptr; + + class PyModuleRunnerThread : public Thread + { + PyModuleRunner *mod; + + public: + PyModuleRunnerThread(PyModuleRunner *mod_) + : mod(mod_) {} + + void *entry() override; + }; + +public: + int serve(); + void shutdown(); + void log(int level, const std::string &record); + + PyModuleRunner( + const std::string &module_name_, + PyObject *pClass_, + const SafeThreadState &pMyThreadState_) + : + module_name(module_name_), + pClass(pClass_), pMyThreadState(pMyThreadState_), + thread(this) + { + assert(pClass != nullptr); + assert(pMyThreadState.ts != nullptr); + assert(!module_name.empty()); + } + + ~PyModuleRunner(); + + PyModuleRunnerThread thread; + + std::string const &get_name() const { return module_name; } +}; + +