Back to index

python3.2  3.2.2
Defines | Functions | Variables
ceval_gil.h File Reference
#include <stdlib.h>
#include <errno.h>

Go to the source code of this file.

Defines

#define DEFAULT_INTERVAL   5000
#define INTERVAL   (gil_interval >= 1 ? gil_interval : 1)
#define FORCE_SWITCHING

Functions

static int gil_created (void)
static void create_gil (void)
static void destroy_gil (void)
static void recreate_gil (void)
static void drop_gil (PyThreadState *tstate)
static void take_gil (PyThreadState *tstate)
void _PyEval_SetSwitchInterval (unsigned long microseconds)
unsigned long _PyEval_GetSwitchInterval ()

Variables

static unsigned long gil_interval = DEFAULT_INTERVAL
static _Py_atomic_int gil_locked = {-1}
static unsigned long gil_switch_number = 0
static _Py_atomic_address gil_last_holder = {NULL}
static COND_T gil_cond
static MUTEX_T gil_mutex
static COND_T switch_cond
static MUTEX_T switch_mutex

Define Documentation

#define DEFAULT_INTERVAL   5000

Definition at line 12 of file ceval_gil.h.

#define FORCE_SWITCHING

Definition at line 18 of file ceval_gil.h.

#define INTERVAL   (gil_interval >= 1 ? gil_interval : 1)

Definition at line 14 of file ceval_gil.h.


Function Documentation

Definition at line 430 of file ceval_gil.h.

{
    return gil_interval;
}
void _PyEval_SetSwitchInterval ( unsigned long  microseconds)

Definition at line 425 of file ceval_gil.h.

static void create_gil ( void  ) [static]

Definition at line 299 of file ceval_gil.h.

{
    MUTEX_INIT(gil_mutex);
#ifdef FORCE_SWITCHING
    MUTEX_INIT(switch_mutex);
#endif
    COND_INIT(gil_cond);
#ifdef FORCE_SWITCHING
    COND_INIT(switch_cond);
#endif
    _Py_atomic_store_relaxed(&gil_last_holder, NULL);
    _Py_ANNOTATE_RWLOCK_CREATE(&gil_locked);
    _Py_atomic_store_explicit(&gil_locked, 0, _Py_memory_order_release);
}

Here is the caller graph for this function:

static void destroy_gil ( void  ) [static]

Definition at line 314 of file ceval_gil.h.

{
    MUTEX_FINI(gil_mutex);
#ifdef FORCE_SWITCHING
    MUTEX_FINI(switch_mutex);
#endif
    COND_FINI(gil_cond);
#ifdef FORCE_SWITCHING
    COND_FINI(switch_cond);
#endif
    _Py_atomic_store_explicit(&gil_locked, -1, _Py_memory_order_release);
    _Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked);
}
static void drop_gil ( PyThreadState tstate) [static]

Definition at line 335 of file ceval_gil.h.

{
    if (!_Py_atomic_load_relaxed(&gil_locked))
        Py_FatalError("drop_gil: GIL is not locked");
    /* tstate is allowed to be NULL (early interpreter init) */
    if (tstate != NULL) {
        /* Sub-interpreter support: threads might have been switched
           under our feet using PyThreadState_Swap(). Fix the GIL last
           holder variable so that our heuristics work. */
        _Py_atomic_store_relaxed(&gil_last_holder, tstate);
    }

    MUTEX_LOCK(gil_mutex);
    _Py_ANNOTATE_RWLOCK_RELEASED(&gil_locked, /*is_write=*/1);
    _Py_atomic_store_relaxed(&gil_locked, 0);
    COND_SIGNAL(gil_cond);
    MUTEX_UNLOCK(gil_mutex);
    
#ifdef FORCE_SWITCHING
    if (_Py_atomic_load_relaxed(&gil_drop_request) && tstate != NULL) {
        MUTEX_LOCK(switch_mutex);
        /* Not switched yet => wait */
        if (_Py_atomic_load_relaxed(&gil_last_holder) == tstate) {
           RESET_GIL_DROP_REQUEST();
            /* NOTE: if COND_WAIT does not atomically start waiting when
               releasing the mutex, another thread can run through, take
               the GIL and drop it again, and reset the condition
               before we even had a chance to wait for it. */
            COND_WAIT(switch_cond, switch_mutex);
       }
        MUTEX_UNLOCK(switch_mutex);
    }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int gil_created ( void  ) [static]

Definition at line 294 of file ceval_gil.h.

Here is the caller graph for this function:

static void recreate_gil ( void  ) [static]

Definition at line 328 of file ceval_gil.h.

{
    _Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked);
    /* XXX should we destroy the old OS resources here? */
    create_gil();
}

Here is the call graph for this function:

static void take_gil ( PyThreadState tstate) [static]

Definition at line 370 of file ceval_gil.h.

{
    int err;
    if (tstate == NULL)
        Py_FatalError("take_gil: NULL tstate");

    err = errno;
    MUTEX_LOCK(gil_mutex);

    if (!_Py_atomic_load_relaxed(&gil_locked))
        goto _ready;
    
    while (_Py_atomic_load_relaxed(&gil_locked)) {
        int timed_out = 0;
        unsigned long saved_switchnum;

        saved_switchnum = gil_switch_number;
        COND_TIMED_WAIT(gil_cond, gil_mutex, INTERVAL, timed_out);
        /* If we timed out and no switch occurred in the meantime, it is time
           to ask the GIL-holding thread to drop it. */
        if (timed_out &&
            _Py_atomic_load_relaxed(&gil_locked) &&
            gil_switch_number == saved_switchnum) {
            SET_GIL_DROP_REQUEST();
        }
    }
_ready:
#ifdef FORCE_SWITCHING
    /* This mutex must be taken before modifying gil_last_holder (see drop_gil()). */
    MUTEX_LOCK(switch_mutex);
#endif
    /* We now hold the GIL */
    _Py_atomic_store_relaxed(&gil_locked, 1);
    _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil_locked, /*is_write=*/1);

    if (tstate != _Py_atomic_load_relaxed(&gil_last_holder)) {
        _Py_atomic_store_relaxed(&gil_last_holder, tstate);
        ++gil_switch_number;
    }

#ifdef FORCE_SWITCHING
    COND_SIGNAL(switch_cond);
    MUTEX_UNLOCK(switch_mutex);
#endif
    if (_Py_atomic_load_relaxed(&gil_drop_request)) {
        RESET_GIL_DROP_REQUEST();
    }
    if (tstate->async_exc != NULL) {
        _PyEval_SignalAsyncExc();
    }
    
    MUTEX_UNLOCK(gil_mutex);
    errno = err;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

COND_T gil_cond [static]

Definition at line 283 of file ceval_gil.h.

unsigned long gil_interval = DEFAULT_INTERVAL [static]

Definition at line 13 of file ceval_gil.h.

Definition at line 278 of file ceval_gil.h.

_Py_atomic_int gil_locked = {-1} [static]

Definition at line 273 of file ceval_gil.h.

MUTEX_T gil_mutex [static]

Definition at line 284 of file ceval_gil.h.

unsigned long gil_switch_number = 0 [static]

Definition at line 275 of file ceval_gil.h.

COND_T switch_cond [static]

Definition at line 289 of file ceval_gil.h.

MUTEX_T switch_mutex [static]

Definition at line 290 of file ceval_gil.h.