1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include <openssl/crypto.h>
- #if OPENSSL_VERSION_NUMBER < 0x10100000L
- #include <pthread.h>
- static pthread_mutex_t *locks;
- static void
- locking_cb(int mode, int type, const char *file, int line)
- {
- if (mode & CRYPTO_LOCK)
- pthread_mutex_lock(&(locks[type]));
- else
- pthread_mutex_unlock(&(locks[type]));
- }
- static void
- thread_id_cb(CRYPTO_THREADID *tid)
- {
- CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
- }
- static void __attribute__((constructor))
- locking_setup(void)
- {
- int i;
-
- if (CRYPTO_get_locking_callback())
- return;
- locks = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
- if (!locks)
- return;
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- if (pthread_mutex_init(&(locks[i]), NULL) != 0) {
- for (int n = 0; n < i; n++)
- pthread_mutex_destroy(&(locks[n]));
- OPENSSL_free(locks);
- locks = NULL;
- return;
- }
- }
- CRYPTO_set_locking_callback(locking_cb);
- CRYPTO_THREADID_set_callback(thread_id_cb);
- }
- #endif
|