Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
thread.c File Reference
#include "Python.h"
#include <stdio.h>
#include <stdlib.h>
#include "pythread.h"

Go to the source code of this file.

Classes

struct  key

Defines

#define dprintf(args)
#define d2printf(args)

Functions

static void PyThread__init_thread (void)
void PyThread_init_thread (void)
size_t PyThread_get_stacksize (void)
int PyThread_set_stacksize (size_t size)
static struct keyfind_key (int key, void *value)
int PyThread_create_key (void)
void PyThread_delete_key (int key)
int PyThread_set_key_value (int key, void *value)
voidPyThread_get_key_value (int key)
void PyThread_delete_key_value (int key)
void PyThread_ReInitTLS (void)

Variables

static int initialized
static size_t _pythread_stacksize = 0
static struct keykeyhead = NULL
static PyThread_type_lock keymutex = NULL
static int nkeys = 0

Class Documentation

struct key

Definition at line 213 of file thread.c.

Collaboration diagram for key:
Class Members
long id
int key
struct key * next
void * value

Define Documentation

#define d2printf (   args)

Definition at line 65 of file thread.c.

#define dprintf (   args)

Definition at line 64 of file thread.c.


Function Documentation

static struct key* find_key ( int  key,
void value 
) [static, read]

Definition at line 251 of file thread.c.

{
    struct key *p, *prev_p;
    long id = PyThread_get_thread_ident();

    if (!keymutex)
        return NULL;
    PyThread_acquire_lock(keymutex, 1);
    prev_p = NULL;
    for (p = keyhead; p != NULL; p = p->next) {
        if (p->id == id && p->key == key)
            goto Done;
        /* Sanity check.  These states should never happen but if
         * they do we must abort.  Otherwise we'll end up spinning in
         * in a tight loop with the lock held.  A similar check is done
         * in pystate.c tstate_delete_common().  */
        if (p == prev_p)
            Py_FatalError("tls find_key: small circular list(!)");
        prev_p = p;
        if (p->next == keyhead)
            Py_FatalError("tls find_key: circular list(!)");
    }
    if (value == NULL) {
        assert(p == NULL);
        goto Done;
    }
    p = (struct key *)malloc(sizeof(struct key));
    if (p != NULL) {
        p->id = id;
        p->key = key;
        p->value = value;
        p->next = keyhead;
        keyhead = p;
    }
 Done:
    PyThread_release_lock(keymutex);
    return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void PyThread__init_thread ( void  ) [static]

Here is the caller graph for this function:

Definition at line 295 of file thread.c.

{
    /* All parts of this function are wrong if it's called by multiple
     * threads simultaneously.
     */
    if (keymutex == NULL)
        keymutex = PyThread_allocate_lock();
    return ++nkeys;
}

Here is the call graph for this function:

Definition at line 307 of file thread.c.

{
    struct key *p, **q;

    PyThread_acquire_lock(keymutex, 1);
    q = &keyhead;
    while ((p = *q) != NULL) {
        if (p->key == key) {
            *q = p->next;
            free((void *)p);
            /* NB This does *not* free p->value! */
        }
        else
            q = &p->next;
    }
    PyThread_release_lock(keymutex);
}

Here is the call graph for this function:

Definition at line 360 of file thread.c.

{
    long id = PyThread_get_thread_ident();
    struct key *p, **q;

    PyThread_acquire_lock(keymutex, 1);
    q = &keyhead;
    while ((p = *q) != NULL) {
        if (p->key == key && p->id == id) {
            *q = p->next;
            free((void *)p);
            /* NB This does *not* free p->value! */
            break;
        }
        else
            q = &p->next;
    }
    PyThread_release_lock(keymutex);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 348 of file thread.c.

{
    struct key *p = find_key(key, NULL);

    if (p == NULL)
        return NULL;
    else
        return p->value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 146 of file thread.c.

{
    return _pythread_stacksize;
}

Here is the caller graph for this function:

Definition at line 73 of file thread.c.

{
#ifdef Py_DEBUG
    char *p = Py_GETENV("PYTHONTHREADDEBUG");

    if (p) {
        if (*p)
            thread_debug = atoi(p);
        else
            thread_debug = 1;
    }
#endif /* Py_DEBUG */
    if (initialized)
        return;
    initialized = 1;
    dprintf(("PyThread_init_thread called\n"));
    PyThread__init_thread();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 386 of file thread.c.

{
    long id = PyThread_get_thread_ident();
    struct key *p, **q;

    if (!keymutex)
        return;

    /* As with interpreter_lock in PyEval_ReInitThreads()
       we just create a new lock without freeing the old one */
    keymutex = PyThread_allocate_lock();

    /* Delete all keys which do not match the current thread id */
    q = &keyhead;
    while ((p = *q) != NULL) {
        if (p->id != id) {
            *q = p->next;
            free((void *)p);
            /* NB This does *not* free p->value! */
        }
        else
            q = &p->next;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyThread_set_key_value ( int  key,
void value 
)

Definition at line 332 of file thread.c.

{
    struct key *p;

    assert(value != NULL);
    p = find_key(key, value);
    if (p == NULL)
        return -1;
    else
        return 0;
}

Here is the call graph for this function:

Definition at line 157 of file thread.c.

{
#if defined(THREAD_SET_STACKSIZE)
    return THREAD_SET_STACKSIZE(size);
#else
    return -2;
#endif
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 95 of file thread.c.

int initialized [static]

Definition at line 68 of file thread.c.

struct key* keyhead = NULL [static]

Definition at line 225 of file thread.c.

Definition at line 226 of file thread.c.

int nkeys = 0 [static]

Definition at line 227 of file thread.c.