Back to index

python3.2  3.2.2
Classes | Defines | Enumerations | Functions | Variables
semaphore.c File Reference
#include "multiprocessing.h"

Go to the source code of this file.

Classes

struct  SemLockObject

Defines

#define ISMINE(o)   (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
#define SEM_CLEAR_ERROR()
#define SEM_GET_LAST_ERROR()   0
#define SEM_CREATE(name, val, max)   sem_open(name, O_CREAT | O_EXCL, 0600, val)
#define SEM_CLOSE(sem)   sem_close(sem)
#define SEM_GETVALUE(sem, pval)   sem_getvalue(sem, pval)
#define SEM_UNLINK(name)   sem_unlink(name)
#define sem_unlink(name)   0
#define sem_timedwait(sem, deadline)   sem_timedwait_save(sem,deadline,_save)

Enumerations

enum  { RECURSIVE_MUTEX, SEMAPHORE }

Functions

int sem_timedwait_save (sem_t *sem, struct timespec *deadline, PyThreadState *_save)
static PyObjectsemlock_acquire (SemLockObject *self, PyObject *args, PyObject *kwds)
static PyObjectsemlock_release (SemLockObject *self, PyObject *args)
static PyObjectnewsemlockobject (PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue)
static PyObjectsemlock_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObjectsemlock_rebuild (PyTypeObject *type, PyObject *args)
static void semlock_dealloc (SemLockObject *self)
static PyObjectsemlock_count (SemLockObject *self)
static PyObjectsemlock_ismine (SemLockObject *self)
static PyObjectsemlock_getvalue (SemLockObject *self)
static PyObjectsemlock_iszero (SemLockObject *self)
static PyObjectsemlock_afterfork (SemLockObject *self)

Variables

static PyMethodDef semlock_methods []
static PyMemberDef semlock_members []
PyTypeObject SemLockType

Class Documentation

struct SemLockObject

Definition at line 13 of file semaphore.c.

Class Members
int count
PyObject_HEAD SEM_HANDLE handle
int kind
long last_tid
int maxvalue

Define Documentation

#define ISMINE (   o)    (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)

Definition at line 22 of file semaphore.c.

#define SEM_CLEAR_ERROR ( )

Definition at line 193 of file semaphore.c.

#define SEM_CLOSE (   sem)    sem_close(sem)

Definition at line 196 of file semaphore.c.

#define SEM_CREATE (   name,
  val,
  max 
)    sem_open(name, O_CREAT | O_EXCL, 0600, val)

Definition at line 195 of file semaphore.c.

#define SEM_GET_LAST_ERROR ( )    0

Definition at line 194 of file semaphore.c.

#define SEM_GETVALUE (   sem,
  pval 
)    sem_getvalue(sem, pval)

Definition at line 197 of file semaphore.c.

#define sem_timedwait (   sem,
  deadline 
)    sem_timedwait_save(sem,deadline,_save)

Definition at line 205 of file semaphore.c.

#define SEM_UNLINK (   name)    sem_unlink(name)

Definition at line 198 of file semaphore.c.

#define sem_unlink (   name)    0

Definition at line 201 of file semaphore.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
RECURSIVE_MUTEX 
SEMAPHORE 

Definition at line 11 of file semaphore.c.


Function Documentation

static PyObject* newsemlockobject ( PyTypeObject type,
SEM_HANDLE  handle,
int  kind,
int  maxvalue 
) [static]

Definition at line 402 of file semaphore.c.

{
    SemLockObject *self;

    self = PyObject_New(SemLockObject, type);
    if (!self)
        return NULL;
    self->handle = handle;
    self->kind = kind;
    self->count = 0;
    self->last_tid = 0;
    self->maxvalue = maxvalue;
    return (PyObject*)self;
}

Here is the caller graph for this function:

int sem_timedwait_save ( sem_t *  sem,
struct timespec *  deadline,
PyThreadState _save 
)

Definition at line 208 of file semaphore.c.

{
    int res;
    unsigned long delay, difference;
    struct timeval now, tvdeadline, tvdelay;

    errno = 0;
    tvdeadline.tv_sec = deadline->tv_sec;
    tvdeadline.tv_usec = deadline->tv_nsec / 1000;

    for (delay = 0 ; ; delay += 1000) {
        /* poll */
        if (sem_trywait(sem) == 0)
            return 0;
        else if (errno != EAGAIN)
            return MP_STANDARD_ERROR;

        /* get current time */
        if (gettimeofday(&now, NULL) < 0)
            return MP_STANDARD_ERROR;

        /* check for timeout */
        if (tvdeadline.tv_sec < now.tv_sec ||
            (tvdeadline.tv_sec == now.tv_sec &&
             tvdeadline.tv_usec <= now.tv_usec)) {
            errno = ETIMEDOUT;
            return MP_STANDARD_ERROR;
        }

        /* calculate how much time is left */
        difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
            (tvdeadline.tv_usec - now.tv_usec);

        /* check delay not too long -- maximum is 20 msecs */
        if (delay > 20000)
            delay = 20000;
        if (delay > difference)
            delay = difference;

        /* sleep */
        tvdelay.tv_sec = delay / 1000000;
        tvdelay.tv_usec = delay % 1000000;
        if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
            return MP_STANDARD_ERROR;

        /* check for signals */
        Py_BLOCK_THREADS
        res = PyErr_CheckSignals();
        Py_UNBLOCK_THREADS

        if (res) {
            errno = EINTR;
            return MP_EXCEPTION_HAS_BEEN_SET;
        }
    }
}

Here is the call graph for this function:

static PyObject* semlock_acquire ( SemLockObject self,
PyObject args,
PyObject kwds 
) [static]

Definition at line 268 of file semaphore.c.

{
    int blocking = 1, res;
    double timeout;
    PyObject *timeout_obj = Py_None;
    struct timespec deadline = {0};
    struct timeval now;
    long sec, nsec;

    static char *kwlist[] = {"block", "timeout", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
                                     &blocking, &timeout_obj))
        return NULL;

    if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
        ++self->count;
        Py_RETURN_TRUE;
    }

    if (timeout_obj != Py_None) {
        timeout = PyFloat_AsDouble(timeout_obj);
        if (PyErr_Occurred())
            return NULL;
        if (timeout < 0.0)
            timeout = 0.0;

        if (gettimeofday(&now, NULL) < 0) {
            PyErr_SetFromErrno(PyExc_OSError);
            return NULL;
        }
        sec = (long) timeout;
        nsec = (long) (1e9 * (timeout - sec) + 0.5);
        deadline.tv_sec = now.tv_sec + sec;
        deadline.tv_nsec = now.tv_usec * 1000 + nsec;
        deadline.tv_sec += (deadline.tv_nsec / 1000000000);
        deadline.tv_nsec %= 1000000000;
    }

    do {
        Py_BEGIN_ALLOW_THREADS
        if (blocking && timeout_obj == Py_None)
            res = sem_wait(self->handle);
        else if (!blocking)
            res = sem_trywait(self->handle);
        else
            res = sem_timedwait(self->handle, &deadline);
        Py_END_ALLOW_THREADS
        if (res == MP_EXCEPTION_HAS_BEEN_SET)
            break;
    } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());

    if (res < 0) {
        if (errno == EAGAIN || errno == ETIMEDOUT)
            Py_RETURN_FALSE;
        else if (errno == EINTR)
            return NULL;
        else
            return PyErr_SetFromErrno(PyExc_OSError);
    }

    ++self->count;
    self->last_tid = PyThread_get_thread_ident();

    Py_RETURN_TRUE;
}

Here is the call graph for this function:

static PyObject* semlock_afterfork ( SemLockObject self) [static]

Definition at line 534 of file semaphore.c.

{
    self->count = 0;
    Py_RETURN_NONE;
}
static PyObject* semlock_count ( SemLockObject self) [static]

Definition at line 482 of file semaphore.c.

{
    return PyInt_FromLong((long)self->count);
}
static void semlock_dealloc ( SemLockObject self) [static]

Definition at line 474 of file semaphore.c.

{
    if (self->handle != SEM_FAILED)
        SEM_CLOSE(self->handle);
    PyObject_Del(self);
}
static PyObject* semlock_getvalue ( SemLockObject self) [static]

Definition at line 495 of file semaphore.c.

{
#ifdef HAVE_BROKEN_SEM_GETVALUE
    PyErr_SetNone(PyExc_NotImplementedError);
    return NULL;
#else
    int sval;
    if (SEM_GETVALUE(self->handle, &sval) < 0)
        return mp_SetError(NULL, MP_STANDARD_ERROR);
    /* some posix implementations use negative numbers to indicate
       the number of waiting threads */
    if (sval < 0)
        sval = 0;
    return PyInt_FromLong((long)sval);
#endif
}

Here is the call graph for this function:

static PyObject* semlock_ismine ( SemLockObject self) [static]

Definition at line 488 of file semaphore.c.

{
    /* only makes sense for a lock */
    return PyBool_FromLong(ISMINE(self));
}

Here is the call graph for this function:

static PyObject* semlock_iszero ( SemLockObject self) [static]

Definition at line 513 of file semaphore.c.

{
#ifdef HAVE_BROKEN_SEM_GETVALUE
    if (sem_trywait(self->handle) < 0) {
        if (errno == EAGAIN)
            Py_RETURN_TRUE;
        return mp_SetError(NULL, MP_STANDARD_ERROR);
    } else {
        if (sem_post(self->handle) < 0)
            return mp_SetError(NULL, MP_STANDARD_ERROR);
        Py_RETURN_FALSE;
    }
#else
    int sval;
    if (SEM_GETVALUE(self->handle, &sval) < 0)
        return mp_SetError(NULL, MP_STANDARD_ERROR);
    return PyBool_FromLong((long)sval == 0);
#endif
}

Here is the call graph for this function:

static PyObject* semlock_new ( PyTypeObject type,
PyObject args,
PyObject kwds 
) [static]

Definition at line 418 of file semaphore.c.

{
    char buffer[256];
    SEM_HANDLE handle = SEM_FAILED;
    int kind, maxvalue, value;
    PyObject *result;
    static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
    static int counter = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist,
                                     &kind, &value, &maxvalue))
        return NULL;

    if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
        PyErr_SetString(PyExc_ValueError, "unrecognized kind");
        return NULL;
    }

    PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%d", (long)getpid(), counter++);

    SEM_CLEAR_ERROR();
    handle = SEM_CREATE(buffer, value, maxvalue);
    /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
    if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
        goto failure;

    if (SEM_UNLINK(buffer) < 0)
        goto failure;

    result = newsemlockobject(type, handle, kind, maxvalue);
    if (!result)
        goto failure;

    return result;

  failure:
    if (handle != SEM_FAILED)
        SEM_CLOSE(handle);
    mp_SetError(NULL, MP_STANDARD_ERROR);
    return NULL;
}

Here is the call graph for this function:

static PyObject* semlock_rebuild ( PyTypeObject type,
PyObject args 
) [static]

Definition at line 461 of file semaphore.c.

{
    SEM_HANDLE handle;
    int kind, maxvalue;

    if (!PyArg_ParseTuple(args, F_SEM_HANDLE "ii",
                          &handle, &kind, &maxvalue))
        return NULL;

    return newsemlockobject(type, handle, kind, maxvalue);
}

Here is the call graph for this function:

static PyObject* semlock_release ( SemLockObject self,
PyObject args 
) [static]

Definition at line 336 of file semaphore.c.

{
    if (self->kind == RECURSIVE_MUTEX) {
        if (!ISMINE(self)) {
            PyErr_SetString(PyExc_AssertionError, "attempt to "
                            "release recursive lock not owned "
                            "by thread");
            return NULL;
        }
        if (self->count > 1) {
            --self->count;
            Py_RETURN_NONE;
        }
        assert(self->count == 1);
    } else {
#ifdef HAVE_BROKEN_SEM_GETVALUE
        /* We will only check properly the maxvalue == 1 case */
        if (self->maxvalue == 1) {
            /* make sure that already locked */
            if (sem_trywait(self->handle) < 0) {
                if (errno != EAGAIN) {
                    PyErr_SetFromErrno(PyExc_OSError);
                    return NULL;
                }
                /* it is already locked as expected */
            } else {
                /* it was not locked so undo wait and raise  */
                if (sem_post(self->handle) < 0) {
                    PyErr_SetFromErrno(PyExc_OSError);
                    return NULL;
                }
                PyErr_SetString(PyExc_ValueError, "semaphore "
                                "or lock released too many "
                                "times");
                return NULL;
            }
        }
#else
        int sval;

        /* This check is not an absolute guarantee that the semaphore
           does not rise above maxvalue. */
        if (sem_getvalue(self->handle, &sval) < 0) {
            return PyErr_SetFromErrno(PyExc_OSError);
        } else if (sval >= self->maxvalue) {
            PyErr_SetString(PyExc_ValueError, "semaphore or lock "
                            "released too many times");
            return NULL;
        }
#endif
    }

    if (sem_post(self->handle) < 0)
        return PyErr_SetFromErrno(PyExc_OSError);

    --self->count;
    Py_RETURN_NONE;
}

Here is the call graph for this function:


Variable Documentation

Initial value:
 {
    {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
     ""},
    {"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
     ""},
    {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
     ""},
    {NULL}
}

Definition at line 572 of file semaphore.c.

Initial value:
 {
    {"acquire", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS,
     "acquire the semaphore/lock"},
    {"release", (PyCFunction)semlock_release, METH_NOARGS,
     "release the semaphore/lock"},
    {"__enter__", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS,
     "enter the semaphore/lock"},
    {"__exit__", (PyCFunction)semlock_release, METH_VARARGS,
     "exit the semaphore/lock"},
    {"_count", (PyCFunction)semlock_count, METH_NOARGS,
     "num of `acquire()`s minus num of `release()`s for this process"},
    {"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS,
     "whether the lock is owned by this thread"},
    {"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS,
     "get the value of the semaphore"},
    {"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS,
     "returns whether semaphore has value zero"},
    {"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS,
     ""},
    {"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS,
     "rezero the net acquisition count after fork()"},
    {NULL}
}

Definition at line 544 of file semaphore.c.

Definition at line 586 of file semaphore.c.