diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-08-16 05:10:20 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-08-16 05:10:20 +0800 |
commit | 88d3eeb9508b21528279d5e2b4acd10399d47193 (patch) | |
tree | 164fc3cf64f431560c4b4925472a4af1e61f8a66 /src/bls_c.cpp | |
parent | ad144ad2e4220dcbf678c515e4670c094413ea6d (diff) | |
download | dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.gz dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.bz2 dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.lz dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.xz dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.zst dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.zip |
blsInit is thread safe
Diffstat (limited to 'src/bls_c.cpp')
-rw-r--r-- | src/bls_c.cpp | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/bls_c.cpp b/src/bls_c.cpp index 563ecc9..72074fe 100644 --- a/src/bls_c.cpp +++ b/src/bls_c.cpp @@ -25,7 +25,7 @@ static std::vector<Fp6> g_Qcoeff; // precomputed Q static const G2& getQ() { return g_Q; } static const std::vector<Fp6>& getQcoeff() { return g_Qcoeff; } -int blsInit(int curve, int maxUnitSize) +int blsInitNotThreadSafe(int curve, int maxUnitSize) try { if (mclBn_init(curve, maxUnitSize) != 0) return -1; @@ -36,22 +36,30 @@ int blsInit(int curve, int maxUnitSize) return -1; } -#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 +#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 #include <mutex> -static std::mutex g_mutex; -static int g_curve = -1; + #define USE_STD_MUTEX +#else +#include <cybozu/mutex.hpp> +#endif -int blsInitThreadSafe(int curve, int maxUnitSize) +int blsInit(int curve, int maxUnitSize) { int ret = 0; - std::lock_guard<std::mutex> lock(g_mutex); +#ifdef USE_STD_MUTEX + static std::mutex m; + std::lock_guard<std::mutex> lock(m); +#else + static cybozu::Mutex m; + cybozu::AutoLock lock(m); +#endif + static int g_curve = -1; if (g_curve != curve) { - ret = blsInit(curve, maxUnitSize); + ret = blsInitNotThreadSafe(curve, maxUnitSize); g_curve = curve; } return ret; } -#endif static inline const mclBnG1 *cast(const G1* x) { return (const mclBnG1*)x; } static inline const mclBnG2 *cast(const G2* x) { return (const mclBnG2*)x; } |