static void semlock_dealloc(SemLockObject* self) { if (self->handle != SEM_FAILED) SEM_CLOSE(self->handle); PyObject_Del(self); }
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; }
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; }
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; }