~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Python/thread.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* Thread package.
 
3
   This is intended to be usable independently from Python.
 
4
   The implementation for system foobar is in a file thread_foobar.h
 
5
   which is included by this file dependent on config settings.
 
6
   Stuff shared by all thread_*.h files is collected here. */
 
7
 
 
8
#include "Python.h"
 
9
 
 
10
#ifndef _POSIX_THREADS
 
11
/* This means pthreads are not implemented in libc headers, hence the macro
 
12
   not present in unistd.h. But they still can be implemented as an external
 
13
   library (e.g. gnu pth in pthread emulation) */
 
14
# ifdef HAVE_PTHREAD_H
 
15
#  include <pthread.h> /* _POSIX_THREADS */
 
16
# endif
 
17
#endif
 
18
 
 
19
#ifndef DONT_HAVE_STDIO_H
 
20
#include <stdio.h>
 
21
#endif
 
22
 
 
23
#include <stdlib.h>
 
24
 
 
25
#include "pythread.h"
 
26
 
 
27
#ifndef _POSIX_THREADS
 
28
 
 
29
/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
 
30
   enough of the Posix threads package is implemented to support python
 
31
   threads.
 
32
 
 
33
   This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
 
34
   a check of __ia64 to verify that we're running on a ia64 system instead
 
35
   of a pa-risc system.
 
36
*/
 
37
#ifdef __hpux
 
38
#ifdef _SC_THREADS
 
39
#define _POSIX_THREADS
 
40
#endif
 
41
#endif
 
42
 
 
43
#endif /* _POSIX_THREADS */
 
44
 
 
45
 
 
46
#ifdef Py_DEBUG
 
47
static int thread_debug = 0;
 
48
#define dprintf(args)   (void)((thread_debug & 1) && printf args)
 
49
#define d2printf(args)  ((thread_debug & 8) && printf args)
 
50
#else
 
51
#define dprintf(args)
 
52
#define d2printf(args)
 
53
#endif
 
54
 
 
55
static int initialized;
 
56
 
 
57
static void PyThread__init_thread(void); /* Forward */
 
58
 
 
59
void
 
60
PyThread_init_thread(void)
 
61
{
 
62
#ifdef Py_DEBUG
 
63
    char *p = Py_GETENV("PYTHONTHREADDEBUG");
 
64
 
 
65
    if (p) {
 
66
        if (*p)
 
67
            thread_debug = atoi(p);
 
68
        else
 
69
            thread_debug = 1;
 
70
    }
 
71
#endif /* Py_DEBUG */
 
72
    if (initialized)
 
73
        return;
 
74
    initialized = 1;
 
75
    dprintf(("PyThread_init_thread called\n"));
 
76
    PyThread__init_thread();
 
77
}
 
78
 
 
79
/* Support for runtime thread stack size tuning.
 
80
   A value of 0 means using the platform's default stack size
 
81
   or the size specified by the THREAD_STACK_SIZE macro. */
 
82
static size_t _pythread_stacksize = 0;
 
83
 
 
84
#ifdef _POSIX_THREADS
 
85
#define PYTHREAD_NAME "pthread"
 
86
#include "thread_pthread.h"
 
87
#endif
 
88
 
 
89
#ifdef NT_THREADS
 
90
#define PYTHREAD_NAME "nt"
 
91
#include "thread_nt.h"
 
92
#endif
 
93
 
 
94
 
 
95
/*
 
96
#ifdef FOOBAR_THREADS
 
97
#include "thread_foobar.h"
 
98
#endif
 
99
*/
 
100
 
 
101
/* return the current thread stack size */
 
102
size_t
 
103
PyThread_get_stacksize(void)
 
104
{
 
105
    return _pythread_stacksize;
 
106
}
 
107
 
 
108
/* Only platforms defining a THREAD_SET_STACKSIZE() macro
 
109
   in thread_<platform>.h support changing the stack size.
 
110
   Return 0 if stack size is valid,
 
111
      -1 if stack size value is invalid,
 
112
      -2 if setting stack size is not supported. */
 
113
int
 
114
PyThread_set_stacksize(size_t size)
 
115
{
 
116
#if defined(THREAD_SET_STACKSIZE)
 
117
    return THREAD_SET_STACKSIZE(size);
 
118
#else
 
119
    return -2;
 
120
#endif
 
121
}
 
122
 
 
123
#ifndef Py_HAVE_NATIVE_TLS
 
124
/* If the platform has not supplied a platform specific
 
125
   TLS implementation, provide our own.
 
126
 
 
127
   This code stolen from "thread_sgi.h", where it was the only
 
128
   implementation of an existing Python TLS API.
 
129
*/
 
130
/* ------------------------------------------------------------------------
 
131
Per-thread data ("key") support.
 
132
 
 
133
Use PyThread_create_key() to create a new key.  This is typically shared
 
134
across threads.
 
135
 
 
136
Use PyThread_set_key_value(thekey, value) to associate void* value with
 
137
thekey in the current thread.  Each thread has a distinct mapping of thekey
 
138
to a void* value.  Caution:  if the current thread already has a mapping
 
139
for thekey, value is ignored.
 
140
 
 
141
Use PyThread_get_key_value(thekey) to retrieve the void* value associated
 
142
with thekey in the current thread.  This returns NULL if no value is
 
143
associated with thekey in the current thread.
 
144
 
 
145
Use PyThread_delete_key_value(thekey) to forget the current thread's associated
 
146
value for thekey.  PyThread_delete_key(thekey) forgets the values associated
 
147
with thekey across *all* threads.
 
148
 
 
149
While some of these functions have error-return values, none set any
 
150
Python exception.
 
151
 
 
152
None of the functions does memory management on behalf of the void* values.
 
153
You need to allocate and deallocate them yourself.  If the void* values
 
154
happen to be PyObject*, these functions don't do refcount operations on
 
155
them either.
 
156
 
 
157
The GIL does not need to be held when calling these functions; they supply
 
158
their own locking.  This isn't true of PyThread_create_key(), though (see
 
159
next paragraph).
 
160
 
 
161
There's a hidden assumption that PyThread_create_key() will be called before
 
162
any of the other functions are called.  There's also a hidden assumption
 
163
that calls to PyThread_create_key() are serialized externally.
 
164
------------------------------------------------------------------------ */
 
165
 
 
166
/* A singly-linked list of struct key objects remembers all the key->value
 
167
 * associations.  File static keyhead heads the list.  keymutex is used
 
168
 * to enforce exclusion internally.
 
169
 */
 
170
struct key {
 
171
    /* Next record in the list, or NULL if this is the last record. */
 
172
    struct key *next;
 
173
 
 
174
    /* The thread id, according to PyThread_get_thread_ident(). */
 
175
    long id;
 
176
 
 
177
    /* The key and its associated value. */
 
178
    int key;
 
179
    void *value;
 
180
};
 
181
 
 
182
static struct key *keyhead = NULL;
 
183
static PyThread_type_lock keymutex = NULL;
 
184
static int nkeys = 0;  /* PyThread_create_key() hands out nkeys+1 next */
 
185
 
 
186
/* Internal helper.
 
187
 * If the current thread has a mapping for key, the appropriate struct key*
 
188
 * is returned.  NB:  value is ignored in this case!
 
189
 * If there is no mapping for key in the current thread, then:
 
190
 *     If value is NULL, NULL is returned.
 
191
 *     Else a mapping of key to value is created for the current thread,
 
192
 *     and a pointer to a new struct key* is returned; except that if
 
193
 *     malloc() can't find room for a new struct key*, NULL is returned.
 
194
 * So when value==NULL, this acts like a pure lookup routine, and when
 
195
 * value!=NULL, this acts like dict.setdefault(), returning an existing
 
196
 * mapping if one exists, else creating a new mapping.
 
197
 *
 
198
 * Caution:  this used to be too clever, trying to hold keymutex only
 
199
 * around the "p->next = keyhead; keyhead = p" pair.  That allowed
 
200
 * another thread to mutate the list, via key deletion, concurrent with
 
201
 * find_key() crawling over the list.  Hilarity ensued.  For example, when
 
202
 * the for-loop here does "p = p->next", p could end up pointing at a
 
203
 * record that PyThread_delete_key_value() was concurrently free()'ing.
 
204
 * That could lead to anything, from failing to find a key that exists, to
 
205
 * segfaults.  Now we lock the whole routine.
 
206
 */
 
207
static struct key *
 
208
find_key(int key, void *value)
 
209
{
 
210
    struct key *p, *prev_p;
 
211
    long id = PyThread_get_thread_ident();
 
212
 
 
213
    if (!keymutex)
 
214
        return NULL;
 
215
    PyThread_acquire_lock(keymutex, 1);
 
216
    prev_p = NULL;
 
217
    for (p = keyhead; p != NULL; p = p->next) {
 
218
        if (p->id == id && p->key == key)
 
219
            goto Done;
 
220
        /* Sanity check.  These states should never happen but if
 
221
         * they do we must abort.  Otherwise we'll end up spinning in
 
222
         * in a tight loop with the lock held.  A similar check is done
 
223
         * in pystate.c tstate_delete_common().  */
 
224
        if (p == prev_p)
 
225
            Py_FatalError("tls find_key: small circular list(!)");
 
226
        prev_p = p;
 
227
        if (p->next == keyhead)
 
228
            Py_FatalError("tls find_key: circular list(!)");
 
229
    }
 
230
    if (value == NULL) {
 
231
        assert(p == NULL);
 
232
        goto Done;
 
233
    }
 
234
    p = (struct key *)PyMem_RawMalloc(sizeof(struct key));
 
235
    if (p != NULL) {
 
236
        p->id = id;
 
237
        p->key = key;
 
238
        p->value = value;
 
239
        p->next = keyhead;
 
240
        keyhead = p;
 
241
    }
 
242
 Done:
 
243
    PyThread_release_lock(keymutex);
 
244
    return p;
 
245
}
 
246
 
 
247
/* Return a new key.  This must be called before any other functions in
 
248
 * this family, and callers must arrange to serialize calls to this
 
249
 * function.  No violations are detected.
 
250
 */
 
251
int
 
252
PyThread_create_key(void)
 
253
{
 
254
    /* All parts of this function are wrong if it's called by multiple
 
255
     * threads simultaneously.
 
256
     */
 
257
    if (keymutex == NULL)
 
258
        keymutex = PyThread_allocate_lock();
 
259
    return ++nkeys;
 
260
}
 
261
 
 
262
/* Forget the associations for key across *all* threads. */
 
263
void
 
264
PyThread_delete_key(int key)
 
265
{
 
266
    struct key *p, **q;
 
267
 
 
268
    PyThread_acquire_lock(keymutex, 1);
 
269
    q = &keyhead;
 
270
    while ((p = *q) != NULL) {
 
271
        if (p->key == key) {
 
272
            *q = p->next;
 
273
            PyMem_RawFree((void *)p);
 
274
            /* NB This does *not* free p->value! */
 
275
        }
 
276
        else
 
277
            q = &p->next;
 
278
    }
 
279
    PyThread_release_lock(keymutex);
 
280
}
 
281
 
 
282
/* Confusing:  If the current thread has an association for key,
 
283
 * value is ignored, and 0 is returned.  Else an attempt is made to create
 
284
 * an association of key to value for the current thread.  0 is returned
 
285
 * if that succeeds, but -1 is returned if there's not enough memory
 
286
 * to create the association.  value must not be NULL.
 
287
 */
 
288
int
 
289
PyThread_set_key_value(int key, void *value)
 
290
{
 
291
    struct key *p;
 
292
 
 
293
    assert(value != NULL);
 
294
    p = find_key(key, value);
 
295
    if (p == NULL)
 
296
        return -1;
 
297
    else
 
298
        return 0;
 
299
}
 
300
 
 
301
/* Retrieve the value associated with key in the current thread, or NULL
 
302
 * if the current thread doesn't have an association for key.
 
303
 */
 
304
void *
 
305
PyThread_get_key_value(int key)
 
306
{
 
307
    struct key *p = find_key(key, NULL);
 
308
 
 
309
    if (p == NULL)
 
310
        return NULL;
 
311
    else
 
312
        return p->value;
 
313
}
 
314
 
 
315
/* Forget the current thread's association for key, if any. */
 
316
void
 
317
PyThread_delete_key_value(int key)
 
318
{
 
319
    long id = PyThread_get_thread_ident();
 
320
    struct key *p, **q;
 
321
 
 
322
    PyThread_acquire_lock(keymutex, 1);
 
323
    q = &keyhead;
 
324
    while ((p = *q) != NULL) {
 
325
        if (p->key == key && p->id == id) {
 
326
            *q = p->next;
 
327
            PyMem_RawFree((void *)p);
 
328
            /* NB This does *not* free p->value! */
 
329
            break;
 
330
        }
 
331
        else
 
332
            q = &p->next;
 
333
    }
 
334
    PyThread_release_lock(keymutex);
 
335
}
 
336
 
 
337
/* Forget everything not associated with the current thread id.
 
338
 * This function is called from PyOS_AfterFork().  It is necessary
 
339
 * because other thread ids which were in use at the time of the fork
 
340
 * may be reused for new threads created in the forked process.
 
341
 */
 
342
void
 
343
PyThread_ReInitTLS(void)
 
344
{
 
345
    long id = PyThread_get_thread_ident();
 
346
    struct key *p, **q;
 
347
 
 
348
    if (!keymutex)
 
349
        return;
 
350
 
 
351
    /* As with interpreter_lock in PyEval_ReInitThreads()
 
352
       we just create a new lock without freeing the old one */
 
353
    keymutex = PyThread_allocate_lock();
 
354
 
 
355
    /* Delete all keys which do not match the current thread id */
 
356
    q = &keyhead;
 
357
    while ((p = *q) != NULL) {
 
358
        if (p->id != id) {
 
359
            *q = p->next;
 
360
            PyMem_RawFree((void *)p);
 
361
            /* NB This does *not* free p->value! */
 
362
        }
 
363
        else
 
364
            q = &p->next;
 
365
    }
 
366
}
 
367
 
 
368
#endif /* Py_HAVE_NATIVE_TLS */
 
369
 
 
370
PyDoc_STRVAR(threadinfo__doc__,
 
371
"sys.thread_info\n\
 
372
\n\
 
373
A struct sequence holding information about the thread implementation.");
 
374
 
 
375
static PyStructSequence_Field threadinfo_fields[] = {
 
376
    {"name",    "name of the thread implementation"},
 
377
    {"lock",    "name of the lock implementation"},
 
378
    {"version", "name and version of the thread library"},
 
379
    {0}
 
380
};
 
381
 
 
382
static PyStructSequence_Desc threadinfo_desc = {
 
383
    "sys.thread_info",           /* name */
 
384
    threadinfo__doc__,           /* doc */
 
385
    threadinfo_fields,           /* fields */
 
386
    3
 
387
};
 
388
 
 
389
static PyTypeObject ThreadInfoType;
 
390
 
 
391
PyObject*
 
392
PyThread_GetInfo(void)
 
393
{
 
394
    PyObject *threadinfo, *value;
 
395
    int pos = 0;
 
396
#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
 
397
     && defined(_CS_GNU_LIBPTHREAD_VERSION))
 
398
    char buffer[255];
 
399
    int len;
 
400
#endif
 
401
 
 
402
    if (ThreadInfoType.tp_name == 0) {
 
403
        if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0)
 
404
            return NULL;
 
405
    }
 
406
 
 
407
    threadinfo = PyStructSequence_New(&ThreadInfoType);
 
408
    if (threadinfo == NULL)
 
409
        return NULL;
 
410
 
 
411
    value = PyUnicode_FromString(PYTHREAD_NAME);
 
412
    if (value == NULL) {
 
413
        Py_DECREF(threadinfo);
 
414
        return NULL;
 
415
    }
 
416
    PyStructSequence_SET_ITEM(threadinfo, pos++, value);
 
417
 
 
418
#ifdef _POSIX_THREADS
 
419
#ifdef USE_SEMAPHORES
 
420
    value = PyUnicode_FromString("semaphore");
 
421
#else
 
422
    value = PyUnicode_FromString("mutex+cond");
 
423
#endif
 
424
    if (value == NULL) {
 
425
        Py_DECREF(threadinfo);
 
426
        return NULL;
 
427
    }
 
428
#else
 
429
    Py_INCREF(Py_None);
 
430
    value = Py_None;
 
431
#endif
 
432
    PyStructSequence_SET_ITEM(threadinfo, pos++, value);
 
433
 
 
434
#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
 
435
     && defined(_CS_GNU_LIBPTHREAD_VERSION))
 
436
    value = NULL;
 
437
    len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
 
438
    if (1 < len && len < sizeof(buffer)) {
 
439
        value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
 
440
        if (value == NULL)
 
441
            PyErr_Clear();
 
442
    }
 
443
    if (value == NULL)
 
444
#endif
 
445
    {
 
446
        Py_INCREF(Py_None);
 
447
        value = Py_None;
 
448
    }
 
449
    PyStructSequence_SET_ITEM(threadinfo, pos++, value);
 
450
    return threadinfo;
 
451
}