Add some C code for exposing the sem_* functions in Linux necessary
for using named semaphores. There have been several occasions where
something like (a cross-process semaphore) this would've been useful
(e.g. limiting the total number of SSHHost.__copy_file methods running
from a single server) and so I wrote up a C extension to expose the
necessary functions.
This ended up being unnecessary, however since they may end up being
useful in the future anyway I'm going to put this into the utils/
for now, rather than just throwing it away. It still needs some work
before being integrated into the autotest library.
From: John Admanski <jadmanski@google.com>
Signed-off-by: Martin Bligh <mbligh@google.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@2474 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/utils/named_semaphore/README b/utils/named_semaphore/README
new file mode 100644
index 0000000..a668d8b
--- /dev/null
+++ b/utils/named_semaphore/README
@@ -0,0 +1,11 @@
+A simple module to expose the standard sem_* functions in Linux needed for
+using named semaphores. To compile, run:
+
+python setup.py build
+
+This will produce a namedsem.so file; put this file somewhere on your python
+import path and you should be able to import it as a module called namedsem.
+
+Before actually using this in python code, it really should be wrapped in
+something like the threading.Semaphore class. The module simply exposes the raw
+C functions with very little error checking.
diff --git a/utils/named_semaphore/namedsem.c b/utils/named_semaphore/namedsem.c
new file mode 100644
index 0000000..cce81a2
--- /dev/null
+++ b/utils/named_semaphore/namedsem.c
@@ -0,0 +1,106 @@
+#include <Python.h>
+#include <semaphore.h>
+
+
+static int
+parse_sem_t(PyObject *object, void *address)
+{
+ *((sem_t **)address) = PyLong_AsVoidPtr(object);
+ return 1;
+}
+
+
+static PyObject *
+namedsem_sem_open(PyObject *self, PyObject *args)
+{
+ const char *name;
+ int oflag;
+ unsigned int value;
+ sem_t *result;
+
+ PyArg_ParseTuple(args, "siI", &name, &oflag, &value);
+ result = sem_open(name, oflag, 0600, value);
+
+ return PyLong_FromVoidPtr(result);
+}
+
+static PyObject *
+namedsem_sem_close(PyObject *self, PyObject *args)
+{
+ sem_t *sem;
+ int result;
+
+ PyArg_ParseTuple(args, "O&", &parse_sem_t, &sem);
+ result = sem_close(sem);
+
+ return Py_BuildValue("i", result);
+}
+
+static PyObject *
+namedsem_sem_unlink(PyObject *self, PyObject *args)
+{
+ const char *name;
+ int result;
+
+ PyArg_ParseTuple(args, "s", &name);
+ result = sem_unlink(name);
+
+ return Py_BuildValue("i", result);
+}
+
+static PyObject *
+namedsem_sem_wait(PyObject *self, PyObject *args)
+{
+ sem_t *sem;
+ int result;
+
+ PyArg_ParseTuple(args, "O&", &parse_sem_t, &sem);
+ result = sem_wait(sem);
+
+ return Py_BuildValue("i", result);
+}
+
+static PyObject *
+namedsem_sem_post(PyObject *self, PyObject *args)
+{
+ sem_t *sem;
+ int result;
+
+ PyArg_ParseTuple(args, "O&", &parse_sem_t, &sem);
+ result = sem_post(sem);
+
+ return Py_BuildValue("i", result);
+}
+
+
+static PyObject *
+namedsem_sem_getvalue(PyObject *self, PyObject *args)
+{
+ sem_t *sem;
+ int sval;
+
+ PyArg_ParseTuple(args, "O&", &parse_sem_t, &sem);
+ sem_getvalue(sem, &sval);
+
+ return Py_BuildValue("i", sval);
+}
+
+
+
+static PyMethodDef NamedsemMethods[] = {
+ {"sem_open", namedsem_sem_open, METH_VARARGS, "Execute sem_open()."},
+ {"sem_close", namedsem_sem_close, METH_VARARGS, "Execute sem_close()."},
+ {"sem_unlink", namedsem_sem_unlink, METH_VARARGS, "Execute sem_unlink()."},
+ {"sem_wait", namedsem_sem_wait, METH_VARARGS, "Execute sem_wait()."},
+ {"sem_post", namedsem_sem_post, METH_VARARGS, "Execute sem_post()."},
+ {"sem_getvalue", namedsem_sem_getvalue, METH_VARARGS, "Execute sem_getvalue()."},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC
+initnamedsem(void) {
+ PyObject *module;
+
+ module = Py_InitModule("namedsem", NamedsemMethods);
+ PyModule_AddIntConstant(module, "SEM_FAILED", (long)SEM_FAILED);
+}
diff --git a/utils/named_semaphore/setup.py b/utils/named_semaphore/setup.py
new file mode 100644
index 0000000..30bd7c3
--- /dev/null
+++ b/utils/named_semaphore/setup.py
@@ -0,0 +1,8 @@
+import distutils.core
+
+
+module = distutils.core.Extension("namedsem", sources=["namedsem.c"])
+
+distutils.core.setup(name="namedsem", version="1.0",
+ description="Named semaphore functions",
+ ext_modules=[module])