Example #1
0
static PyObject *
semlock_rebuild(PyTypeObject *type, PyObject *args)
{
    SEM_HANDLE handle;
    int kind, maxvalue;
    char *name, *name_copy = NULL;

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

    if (name != NULL) {
        name_copy = PyMem_Malloc(strlen(name) + 1);
        if (name_copy == NULL)
            return PyErr_NoMemory();
        strcpy(name_copy, name);
    }

#ifndef MS_WINDOWS
    if (name != NULL) {
        handle = sem_open(name, 0);
        if (handle == SEM_FAILED) {
            PyMem_Free(name_copy);
            return PyErr_SetFromErrno(PyExc_OSError);
        }
    }
#endif

    return newsemlockobject(type, handle, kind, maxvalue, name_copy);
}
Example #2
0
static PyObject *
semlock_rebuild(PyTypeObject *type, PyObject *args)
{
    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);
}
Example #3
0
static PyObject *
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    char buffer[256];
    SEM_HANDLE handle = SEM_FAILED;
    int kind, maxvalue, value;
    PyObject *result;
    static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
    int try = 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;
    }

    /* Create a semaphore with a unique name. The bytes returned by
     * _PyOS_URandom() are treated as unsigned long to ensure that the filename
     * is valid (no special characters). */
    do {
        unsigned long suffix;
        _PyOS_URandom((char *)&suffix, sizeof(suffix));
        PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%lu", (long)getpid(),
                      suffix);
        SEM_CLEAR_ERROR();
        handle = SEM_CREATE(buffer, value, maxvalue);
    } while ((handle == SEM_FAILED) && (errno == EEXIST) && (++try < 100));

    /* 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;
}
Example #4
0
static PyObject *
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    SEM_HANDLE handle = SEM_FAILED;
    int kind, maxvalue, value, unlink;
    PyObject *result;
    char *name, *name_copy = NULL;
    static char *kwlist[] = {"kind", "value", "maxvalue", "name", "unlink",
                             NULL};

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

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

    if (!unlink) {
        name_copy = PyMem_Malloc(strlen(name) + 1);
        if (name_copy == NULL)
            goto failure;
        strcpy(name_copy, name);
    }

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

    if (unlink && SEM_UNLINK(name) < 0)
        goto failure;

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

    return result;

  failure:
    if (handle != SEM_FAILED)
        SEM_CLOSE(handle);
    PyMem_Free(name_copy);
    _PyMp_SetError(NULL, MP_STANDARD_ERROR);
    return NULL;
}
Example #5
0
static PyObject *
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    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);
    _PyMp_SetError(NULL, MP_STANDARD_ERROR);
    return NULL;
}