关闭

Using libssh2 in a multi-threaded program/library

139人阅读 评论(0) 收藏 举报
Whether you use libssh2 when it is compiled against OpenSSL or gcrypt, there's a known issue with multi-threaded programs, even if each session is correctly created and used within a single thread. Specifically, the multi-threading "glue" hooks in the support library need to be managed by the application.

The gcrypt library is documented at http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html

For OpenSSL, the call is CRYPTO_set_locking_callback. An example for Win32 (lifted out of the OpenSSL source file crypto/threads/mttest.c):

/***************************/
/* t h r e a d _ s e t u p */
/***************************/
void thread_setup(void)
{
        int i;

        lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));

        for (i=0; i<CRYPTO_num_locks(); i++)
        {
                lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
        }

        CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))win32_locking_callback);
}

/*******************************/
/* t h r e a d _ c l e a n u p */
/*******************************/
void thread_cleanup(void)
{
        int i;

        CRYPTO_set_locking_callback(NULL);

        for (i=0; i<CRYPTO_num_locks(); i++)
        {
                CloseHandle(lock_cs[i]);
        }
        OPENSSL_free(lock_cs);
}

/***********************************************/
/* w i n 3 2 _ l o c k i n g _ c a l l b a c k */
/***********************************************/
void win32_locking_callback(int mode, int type, char *file, int line)
{
        if (mode & CRYPTO_LOCK)
        {
                WaitForSingleObject(lock_cs[type],INFINITE);
        }
        else
        {
                ReleaseMutex(lock_cs[type]);
        }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:704811次
    • 积分:8215
    • 等级:
    • 排名:第2432名
    • 原创:19篇
    • 转载:581篇
    • 译文:0篇
    • 评论:54条
    最新评论