From 88d3eeb9508b21528279d5e2b4acd10399d47193 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 16 Aug 2017 06:10:20 +0900 Subject: blsInit is thread safe --- include/bls/bls.h | 14 ++++++++------ src/bls_c.cpp | 24 ++++++++++++++++-------- 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 g_Qcoeff; // precomputed Q static const G2& getQ() { return g_Q; } static const std::vector& 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 -static std::mutex g_mutex; -static int g_curve = -1; + #define USE_STD_MUTEX +#else +#include +#endif -int blsInitThreadSafe(int curve, int maxUnitSize) +int blsInit(int curve, int maxUnitSize) { int ret = 0; - std::lock_guard lock(g_mutex); +#ifdef USE_STD_MUTEX + static std::mutex m; + std::lock_guard 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 +#include #include #include @@ -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 #include struct Thread { @@ -86,7 +87,7 @@ CYBOZU_TEST_AUTO(multipleInit) { std::vector 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 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); -- cgit v1.2.3