summaryrefslogtreecommitdiffstats
path: root/python/sip/siplib/threads.c
blob: c4ee75ffef79701ee00fcdbcecf25b75dc689440 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Thread support for the SIP library.  This module provides the hooks for
 * C++ classes that provide a thread interface to interact properly with the
 * Python threading infrastructure.
 *
 * Copyright (c) 2007
 * 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
 * 
 * This file is part of SIP.
 * 
 * This copy of SIP is licensed for use under the terms of the SIP License
 * Agreement.  See the file LICENSE for more details.
 * 
 * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */


#include "sip.h"
#include "sipint.h"


/*
 * The data associated with pending request to wrap an object.
 */
typedef struct _pendingDef {
    void *cpp;                      /* The C/C++ object ot be wrapped. */
    sipWrapper *owner;              /* The owner of the object. */
    int flags;                      /* The flags. */
} pendingDef;


#ifdef WITH_THREAD

#include <pythread.h>


/*
 * The per thread data we need to maintain.
 */
typedef struct _threadDef {
    long thr_ident;                 /* The thread identifier. */
    pendingDef pending;             /* An object waiting to be wrapped. */
    struct _threadDef *next;        /* Next in the list. */
} threadDef;


static threadDef *threads = NULL;   /* Linked list of threads. */


static threadDef *currentThreadDef(void);

#endif


static pendingDef pending;          /* An object waiting to be wrapped. */


/*
 * Get the address of any C/C++ object waiting to be wrapped.
 */
void *sipGetPending(sipWrapper **op, int *fp)
{
    pendingDef *pp;

#ifdef WITH_THREAD
    threadDef *td;

    if ((td = currentThreadDef()) != NULL)
        pp = &td->pending;
    else
        pp = &pending;
#else
    pp = &pending;
#endif

    if (pp->cpp != NULL)
    {
        if (op != NULL)
            *op = pp->owner;

        if (fp != NULL)
            *fp = pp->flags;
    }

    return pp->cpp;
}


/*
 * Convert a new C/C++ pointer to a Python instance.
 */
PyObject *sipWrapSimpleInstance(void *cppPtr, sipWrapperType *type,
        sipWrapper *owner, int flags)
{
    static PyObject *nullargs = NULL;

    pendingDef old_pending;
    PyObject *self;
#ifdef WITH_THREAD
    threadDef *td;
#endif

    if (nullargs == NULL && (nullargs = PyTuple_New(0)) == NULL)
        return NULL;

    if (cppPtr == NULL)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }

    /*
     * Object creation can trigger the Python garbage collector which in turn
     * can execute arbitrary Python code which can then call this function
     * recursively.  Therefore we save any existing pending object before
     * setting the new one.
     */
#ifdef WITH_THREAD
    if ((td = currentThreadDef()) != NULL)
    {
        old_pending = td->pending;

        td->pending.cpp = cppPtr;
        td->pending.owner = owner;
        td->pending.flags = flags;
    }
    else
    {
        old_pending = pending;

        pending.cpp = cppPtr;
        pending.owner = owner;
        pending.flags = flags;
    }
#else
    old_pending = pending;

    pending.cpp = cppPtr;
    pending.owner = owner;
    pending.flags = flags;
#endif

    self = PyObject_Call((PyObject *)type, nullargs, NULL);

#ifdef WITH_THREAD
    if (td != NULL)
        td->pending = old_pending;
    else
        pending = old_pending;
#else
    pending = old_pending;
#endif

    return self;
}


/*
 * This is called from a newly created thread to initialise some thread local
 * storage.
 */
void sip_api_start_thread(void)
{
#ifdef WITH_THREAD
    threadDef *td;

    /* Save the thread ID.  First, find an empty slot in the list. */
    for (td = threads; td != NULL; td = td->next)
        if (td->thr_ident == 0)
            break;

    if (td == NULL)
    {
        td = sip_api_malloc(sizeof (threadDef));
        td->next = threads;
        threads = td;
    }

    if (td != NULL)
    {
        td->thr_ident = PyThread_get_thread_ident();
        td->pending.cpp = NULL;
    }
#endif
}


/*
 * Handle the termination of a thread.  The thread state should already have
 * been handled by the last call to PyGILState_Release().
 */
void sip_api_end_thread(void)
{
#ifdef WITH_THREAD
    threadDef *td;

    /* We have the GIL at this point. */
    if ((td = currentThreadDef()) != NULL)
        td->thr_ident = 0;
#endif
}


#ifdef WITH_THREAD

/*
 * Return the thread data for the current thread or NULL if it wasn't
 * recognised.
 */
static threadDef *currentThreadDef(void)
{
    threadDef *td;
    long ident = PyThread_get_thread_ident();

    for (td = threads; td != NULL; td = td->next)
        if (td->thr_ident == ident)
            break;

    return td;
}

#endif