Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix singletons #2529

Merged
merged 1 commit into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xmrstak/backend/cpu/crypto/cryptonight.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct cryptonight_ctx
union {
extra_ctx_r cn_r_ctx;
};

};

struct alloc_msg
Expand Down
6 changes: 5 additions & 1 deletion xmrstak/backend/globalStates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ struct globalStates
{
auto& env = environment::inst();
if(env.pglobalStates == nullptr)
env.pglobalStates = new globalStates;
{
std::unique_lock<std::mutex> lck(env.update);
if(env.pglobalStates == nullptr)
env.pglobalStates = new globalStates;
}
return *env.pglobalStates;
}

Expand Down
6 changes: 5 additions & 1 deletion xmrstak/jconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class jconf
{
auto& env = xmrstak::environment::inst();
if(env.pJconfConfig == nullptr)
env.pJconfConfig = new jconf;
{
std::unique_lock<std::mutex> lck(env.update);
if(env.pJconfConfig == nullptr)
env.pJconfConfig = new jconf;
}
return env.pJconfConfig;
};

Expand Down
6 changes: 5 additions & 1 deletion xmrstak/misc/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ class printer
{
auto& env = xmrstak::environment::inst();
if(env.pPrinter == nullptr)
env.pPrinter = new printer;
{
std::unique_lock<std::mutex> lck(env.update);
if(env.pPrinter == nullptr)
env.pPrinter = new printer;
}
return env.pPrinter;
};

Expand Down
19 changes: 19 additions & 0 deletions xmrstak/misc/environment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "environment.hpp"

#include "xmrstak/misc/console.hpp"
#include "xmrstak/backend/cpu/crypto/cryptonight.h"
#include "xmrstak/params.hpp"
#include "xmrstak/misc/executor.hpp"
#include "xmrstak/jconf.hpp"

namespace xmrstak
{
void environment::init_singeltons()
{
printer::inst();
globalStates::inst();
jconf::inst();
executor::inst();
params::inst();
}
}
10 changes: 10 additions & 0 deletions xmrstak/misc/environment.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <mutex>

class printer;
class jconf;
class executor;
Expand All @@ -19,7 +21,10 @@ struct environment
if(env == nullptr)
{
if(init == nullptr)
{
env = new environment;
env->init_singeltons();
}
else
env = init;
}
Expand All @@ -36,6 +41,11 @@ struct environment
jconf* pJconfConfig = nullptr;
executor* pExecutor = nullptr;
params* pParams = nullptr;

std::mutex update;

private:
void init_singeltons();
};

} // namespace xmrstak
6 changes: 5 additions & 1 deletion xmrstak/misc/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class executor
{
auto& env = xmrstak::environment::inst();
if(env.pExecutor == nullptr)
env.pExecutor = new executor;
{
std::unique_lock<std::mutex> lck(env.update);
if(env.pExecutor == nullptr)
env.pExecutor = new executor;
}
return env.pExecutor;
};

Expand Down
6 changes: 5 additions & 1 deletion xmrstak/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ struct params
{
auto& env = environment::inst();
if(env.pParams == nullptr)
env.pParams = new params;
{
std::unique_lock<std::mutex> lck(env.update);
if(env.pParams == nullptr)
env.pParams = new params;
}
return *env.pParams;
}

Expand Down