File: lock.c

package info (click to toggle)
jose 11-2%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,896 kB
  • sloc: ansic: 9,976; javascript: 987; sh: 579; makefile: 9
file content (68 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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;

    /* Check if somebody else has set a locking callback already. */
    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 /* OpenSSL < 1.1.0 */