That thread_specific_ptr was rubbish...

This commit is contained in:
lotodore
2007-03-02 00:34:50 +00:00
parent 991ec9e5e6
commit cb3c17573d
+34 -37
View File
@@ -16,21 +16,20 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef _WIN32 #ifndef _WIN32
#error This source code is Win32 only. #error This source code is Win32 only.
#endif #endif
#include <windows.h>
#include <wincrypt.h>
#include <boost/thread.hpp> #include <windows.h>
#include <core/rand.h> #include <wincrypt.h>
class CryptData #include <core/rand.h>
{
public: class CryptData
CryptData() {
{ public:
CryptData()
{
if (!::CryptAcquireContext( if (!::CryptAcquireContext(
&cryptProvider, &cryptProvider,
NULL, NULL,
@@ -40,27 +39,25 @@ public:
{ {
cryptProvider = 0; cryptProvider = 0;
} }
} }
~CryptData() ~CryptData()
{ {
if (cryptProvider) CryptReleaseContext(cryptProvider, 0); if (cryptProvider)
} CryptReleaseContext(cryptProvider, 0);
}
HCRYPTPROV cryptProvider;
}; HCRYPTPROV cryptProvider;
};
void
RandomBytes(unsigned char *buf, unsigned size) static CryptData g_cryptData;
{
static boost::thread_specific_ptr<CryptData> m_cryptData; void
RandomBytes(unsigned char *buf, unsigned size)
if (!m_cryptData.get()) {
m_cryptData.reset(new CryptData); HCRYPTPROV provider = g_cryptData.cryptProvider;
HCRYPTPROV provider = m_cryptData.get()->cryptProvider; if (provider)
::CryptGenRandom(provider, size, buf);
if (provider) }
::CryptGenRandom(provider, size, buf);
}