MXS-2008 Enforce right initialization of maxbase

Now maxbase can only be initialized via maxbase::init().
This commit is contained in:
Johan Wikman
2018-08-21 10:12:55 +03:00
parent 24ab3c099c
commit e2ba7151b7
3 changed files with 66 additions and 49 deletions

View File

@ -18,7 +18,6 @@
namespace maxbase namespace maxbase
{ {
class MaxBase;
class MessageQueue; class MessageQueue;
class Worker; class Worker;
@ -176,9 +175,8 @@ public:
*/ */
Worker* remove_from_worker(); Worker* remove_from_worker();
public: private:
// TODO: Make private once all callers have been modified. friend class Initer;
friend class MaxBase;
static bool init(); static bool init();
static void finish(); static void finish();

View File

@ -974,8 +974,8 @@ protected:
*/ */
static void resolve_poll_error(int fd, int err, int op); static void resolve_poll_error(int fd, int err, int op);
public: private:
// TODO: Make private once all callers have beed modified. friend class Initer;
static bool init(); static bool init();
static void finish(); static void finish();

View File

@ -16,34 +16,77 @@
#include <maxbase/messagequeue.hh> #include <maxbase/messagequeue.hh>
#include <maxbase/worker.hh> #include <maxbase/worker.hh>
namespace namespace maxbase
{ {
using namespace maxbase; class Initer
{
Initer() = delete;
Initer(const Initer&) = delete;
Initer& operator=(const Initer&) = delete;
public:
static bool init()
{
bool rv = false;
int i;
for (i = 0; i < s_nComponents; ++i)
{
if (!s_components[i].init())
{
break;
}
}
if (i == s_nComponents)
{
rv = true;
}
else if (i != 0)
{
// We need to finalize in reverse order the components that
// successfully were initialized.
for (int j = i - 1; j >= 0; --j)
{
s_components[j].finish();
}
}
return rv;
}
static void finish()
{
for (int i = s_nComponents - 1; i >= 0; --i)
{
s_components[i].finish();
}
}
private:
typedef bool (*init_function_t)(); typedef bool (*init_function_t)();
typedef void (*finish_function_t)(); typedef void (*finish_function_t)();
#define MAXBASE_COMPONENT(X) { &X::init, &X::finish } struct component_t
struct maxbase_component_t
{ {
init_function_t init; init_function_t init;
finish_function_t finish; finish_function_t finish;
}; };
const maxbase_component_t maxbase_components[] = static component_t s_components[];
static int s_nComponents;
};
#define MAXBASE_COMPONENT(X) { &X::init, &X::finish }
Initer::component_t Initer::s_components[] =
{ {
MAXBASE_COMPONENT(MessageQueue), MAXBASE_COMPONENT(MessageQueue),
MAXBASE_COMPONENT(Worker), MAXBASE_COMPONENT(Worker),
}; };
const int N_COMPONENTS = sizeof(maxbase_components)/sizeof(maxbase_components[0]); int Initer::s_nComponents = sizeof(Initer::s_components) / sizeof(Initer::s_components[0]);
}
namespace maxbase
{
MaxBase::MaxBase(const char* zIdent, MaxBase::MaxBase(const char* zIdent,
const char* zLogdir, const char* zLogdir,
@ -101,28 +144,7 @@ bool init()
if (log_exists) if (log_exists)
{ {
int i; rv = Initer::init();
for (i = 0; i < N_COMPONENTS; ++i)
{
if (!maxbase_components[i].init())
{
break;
}
}
if (i == N_COMPONENTS)
{
rv = true;
}
else if (i != 0)
{
// We need to finalize in reverse order the components that
// successfully were initialized.
for (int j = i - 1; j >= 0; --j)
{
maxbase_components[j].finish();
}
}
} }
if (log_inited_locally) if (log_inited_locally)
@ -136,10 +158,7 @@ bool init()
void finish() void finish()
{ {
for (int i = N_COMPONENTS - 1; i >= 0; --i) Initer::finish();
{
maxbase_components[i].finish();
}
} }
} }