openssl 线程安全 Windows

openssl 线程安全

在初始化的时候 调用 thread_setup,在结束的时候调用thread_cleanup。代码摘自crypto/threads/mttest.c

static unsigned long _stdcall thread_id(void)

{
unsigned long ret;


ret=(unsigned long)GetCurrentThreadId();
return ret;
}


void thread_setup(void)
{
int i;


m_hLock_csArray = (HANDLE*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
for (i = 0; i < CRYPTO_num_locks(); i++) {
m_hLock_csArray[i] = CreateMutex(NULL, FALSE, NULL);
}


CRYPTO_set_locking_callback((void (*)(int, int, const char *, int))win32_locking_callback);
CRYPTO_set_id_callback((unsigned long (*)())thread_id);
}


void thread_cleanup(void)
{
int i;


CRYPTO_set_locking_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++)
CloseHandle(m_hLock_csArray[i]);
OPENSSL_free(m_hLock_csArray);
}


static void _stdcall win32_locking_callback(int mode, int type, const char *file, int line)
{
if (mode & CRYPTO_LOCK) {
WaitForSingleObject(m_hLock_csArray[type], INFINITE);
} else {
ReleaseMutex(m_hLock_csArray[type]);
}
}
发布了35 篇原创文章 · 获赞 3 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览