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
{
class MaxBase;
class MessageQueue;
class Worker;
@ -176,9 +175,8 @@ public:
*/
Worker* remove_from_worker();
public:
// TODO: Make private once all callers have been modified.
friend class MaxBase;
private:
friend class Initer;
static bool init();
static void finish();

View File

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

View File

@ -16,34 +16,77 @@
#include <maxbase/messagequeue.hh>
#include <maxbase/worker.hh>
namespace
namespace maxbase
{
using namespace maxbase;
class Initer
{
Initer() = delete;
Initer(const Initer&) = delete;
Initer& operator=(const Initer&) = delete;
typedef bool (*init_function_t)();
typedef void (*finish_function_t)();
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 void (*finish_function_t)();
struct component_t
{
init_function_t init;
finish_function_t finish;
};
static component_t s_components[];
static int s_nComponents;
};
#define MAXBASE_COMPONENT(X) { &X::init, &X::finish }
struct maxbase_component_t
{
init_function_t init;
finish_function_t finish;
};
const maxbase_component_t maxbase_components[] =
Initer::component_t Initer::s_components[] =
{
MAXBASE_COMPONENT(MessageQueue),
MAXBASE_COMPONENT(Worker),
};
const int N_COMPONENTS = sizeof(maxbase_components)/sizeof(maxbase_components[0]);
}
namespace maxbase
{
int Initer::s_nComponents = sizeof(Initer::s_components) / sizeof(Initer::s_components[0]);
MaxBase::MaxBase(const char* zIdent,
const char* zLogdir,
@ -101,28 +144,7 @@ bool init()
if (log_exists)
{
int i;
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();
}
}
rv = Initer::init();
}
if (log_inited_locally)
@ -136,10 +158,7 @@ bool init()
void finish()
{
for (int i = N_COMPONENTS - 1; i >= 0; --i)
{
maxbase_components[i].finish();
}
Initer::finish();
}
}