summaryrefslogtreecommitdiff
path: root/SWIG/_threads.i
blob: 2016d710fa961ba0a3eb29f4b6f81df65664f63e (plain)
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
/* Copyright (c) 1999 Ng Pheng Siong. All rights reserved. */
/* $Id: _threads.i,v 1.1 2003/06/22 17:30:52 ngps Exp $ */

%{
#include <pythread.h>
#include <openssl/crypto.h>

#ifdef THREADING
static PyThread_type_lock lock_cs[CRYPTO_NUM_LOCKS];
static long lock_count[CRYPTO_NUM_LOCKS];
#endif

void threading_locking_callback(int mode, int type, const char *file, int line) {
#ifdef THREADING
        if (mode & CRYPTO_LOCK) {
                PyThread_acquire_lock(lock_cs[type], 0);
                lock_count[type]++;
        } else {
                PyThread_release_lock(lock_cs[type]);
                lock_count[type]--;
        }
#endif
}

unsigned long threading_id_callback(void) {
#ifdef THREADING
    return (unsigned long)PyThread_get_thread_ident();
#else
    return (unsigned long)0;
#endif
}
%}

%inline %{
void threading_init(void) {
#ifdef THREADING
    int i;
    thread_mode = 1;
    /*PyThread_init_thread();*/
    for (i=0; i<CRYPTO_NUM_LOCKS; i++) {
        lock_count[i]=0;
        lock_cs[i]=PyThread_allocate_lock();
    }
    CRYPTO_set_id_callback(threading_id_callback);
    CRYPTO_set_locking_callback(threading_locking_callback);
#endif
}

void threading_cleanup(void) {
#ifdef THREADING
    int i;
    CRYPTO_set_locking_callback(NULL);
    for (i=0; i<CRYPTO_NUM_LOCKS; i++) {
        lock_count[i]=0;
        PyThread_release_lock(lock_cs[i]);
        PyThread_free_lock(lock_cs[i]);
    }
#endif
}
%}