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 | |
parent | ad144ad2e4220dcbf678c515e4670c094413ea6d (diff) | |
download | dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.gz dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.tar.zst dexon-bls-88d3eeb9508b21528279d5e2b4acd10399d47193.zip |
blsInit is thread safe
-rw-r--r-- | include/bls/bls.h | 14 | ||||
-rw-r--r-- | src/bls_c.cpp | 24 | ||||
-rw-r--r-- | test/bls_c384_test.cpp | 7 |
3 files changed, 28 insertions, 17 deletions
diff --git a/include/bls/bls.h b/include/bls/bls.h index 2a9df84..6b4caa4 100644 --- a/include/bls/bls.h +++ b/include/bls/bls.h @@ -45,15 +45,17 @@ typedef struct { /* initialize this library - call this once before using the other method + call this once before using the other functions + @param curve [in] enum value defined in mcl/bn.h + @param maxUnitSize [in] MCLBN_FP_UNIT_SIZE (fixed) return 0 if success - @note init() is not thread safe + @note blsInit() is thread safe and serialized if it is called simultaneously + but don't call it while using other functions. */ BLS_DLL_API int blsInit(int curve, int maxUnitSize); -/* - wait for the finish of the first call blsInit() and do nothing -*/ -BLS_DLL_API int blsInitThreadSafe(int curve, int maxUnitSize); + +// not thread safe version (old blsInit) +BLS_DLL_API int blsInitNotThreadSafe(int curve, int maxUnitSize); BLS_DLL_API size_t blsGetOpUnitSize(void); // return strlen(buf) if success else 0 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; } diff --git a/test/bls_c384_test.cpp b/test/bls_c384_test.cpp index 2e61e2c..21612da 100644 --- a/test/bls_c384_test.cpp +++ b/test/bls_c384_test.cpp @@ -1,4 +1,5 @@ #include <cybozu/test.hpp> +#include <cybozu/inttype.hpp> #include <bls/bls.h> #include <string.h> @@ -62,7 +63,7 @@ void blsOrderTest(const char *curveOrder, const char *fieldOrder) CYBOZU_TEST_EQUAL(buf, fieldOrder); } -#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 +#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 #include <thread> #include <vector> struct Thread { @@ -86,7 +87,7 @@ CYBOZU_TEST_AUTO(multipleInit) { std::vector<Thread> vt(n); for (size_t i = 0; i < n; i++) { - vt[i].run(blsInitThreadSafe, mclBn_CurveFp254BNb, MCLBN_FP_UNIT_SIZE); + vt[i].run(blsInit, mclBn_CurveFp254BNb, MCLBN_FP_UNIT_SIZE); } } CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 4u); @@ -94,7 +95,7 @@ CYBOZU_TEST_AUTO(multipleInit) { std::vector<Thread> vt(n); for (size_t i = 0; i < n; i++) { - vt[i].run(blsInitThreadSafe, mclBn_CurveFp382_1, MCLBN_FP_UNIT_SIZE); + vt[i].run(blsInit, mclBn_CurveFp382_1, MCLBN_FP_UNIT_SIZE); } } CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 6u); |