Commit 31f710bc authored by Mat's avatar Mat
Browse files

Add wrappers for msync(2), mlock(2), and munlock(2)

The wrappers are accessible both as module functions and as methods of
the MapOwner class which is the base class of the numpy array. The two
examples below are identical in behavious:
    >>> SharedArray.msync(array, SharedArray.MS_ASYNC)
    >>> array.base.msync(SharedArray.MS_ASYNC)
parent bf97f8e2
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -30,6 +30,25 @@ static void do_dealloc(PyMapOwnerObject *self)
		PyErr_SetFromErrno(PyExc_RuntimeError);
}

/*
 * List of methods
 */
static PyMethodDef methods[] = {
	{ "msync", (PyCFunction) map_owner_msync,
	  METH_VARARGS | METH_KEYWORDS,
	  "Synchronise a file with a memory map (msync(2) wrapper)" },

	{ "mlock", (PyCFunction) map_owner_mlock,
	  METH_VARARGS,
	  "Lock the array in memory (mlock(2) wrapper)" },

	{ "munlock", (PyCFunction) map_owner_munlock,
	  METH_VARARGS,
	  "Unlock the array in memory (munlock(2) wrapper)" },

	{ NULL, NULL, 0, NULL }
};

/*
 * MapOwner type definition
 */
@@ -61,7 +80,7 @@ PyTypeObject PyMapOwner_Type = {
	0,					/* tp_weaklistoffset	*/
	0,					/* tp_iter		*/
	0,					/* tp_iternext		*/
	0,					/* tp_methods		*/
	methods,				/* tp_methods		*/
	0,					/* tp_members		*/
	0,					/* tp_getset		*/
	0,					/* tp_base		*/
+5 −0
Original line number Diff line number Diff line
@@ -31,6 +31,11 @@ typedef struct {
/* Class type definition */
extern PyTypeObject PyMapOwner_Type;

/* Methods */
extern PyObject *map_owner_msync(PyMapOwnerObject *self, PyObject *args, PyObject *kwds);
extern PyObject *map_owner_mlock(PyMapOwnerObject *self, PyObject *args);
extern PyObject *map_owner_munlock(PyMapOwnerObject *self, PyObject *args);

/* C API */
#define PyMapOwner_Check(op)	PyObject_TypeCheck(op, &PyMapOwner_Type)

src/map_owner_mlock.c

0 → 100644
+30 −0
Original line number Diff line number Diff line
/* 
 * This file is part of SharedArray.
 * Copyright (C) 2014-2016 Mathieu Mirmont <mat@parad0x.org>
 * 
 * SharedArray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * SharedArray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SharedArray.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/mman.h>
#include <Python.h>
#include "map_owner.h"

PyObject *map_owner_mlock(PyMapOwnerObject *self, PyObject *args)
{
	/* Call mlock(2) */
	if (mlock(self->map_addr, self->map_size) < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	Py_RETURN_NONE;
}

src/map_owner_msync.c

0 → 100644
+37 −0
Original line number Diff line number Diff line
/* 
 * This file is part of SharedArray.
 * Copyright (C) 2014-2016 Mathieu Mirmont <mat@parad0x.org>
 * 
 * SharedArray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * SharedArray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SharedArray.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/mman.h>
#include <Python.h>
#include "map_owner.h"

PyObject *map_owner_msync(PyMapOwnerObject *self, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = { "flags", NULL };
	int flags;

	/* Parse the arguments */
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags))
		return NULL;

	/* Call msync(2) */
	if (msync(self->map_addr, self->map_size, flags) < 0)
		return PyErr_SetFromErrno(PyExc_OSError);
	
	Py_RETURN_NONE;
}
+30 −0
Original line number Diff line number Diff line
/* 
 * This file is part of SharedArray.
 * Copyright (C) 2014-2016 Mathieu Mirmont <mat@parad0x.org>
 * 
 * SharedArray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * SharedArray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SharedArray.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/mman.h>
#include <Python.h>
#include "map_owner.h"

PyObject *map_owner_munlock(PyMapOwnerObject *self, PyObject *args)
{
	/* Call munlock(2) */
	if (munlock(self->map_addr, self->map_size) < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	Py_RETURN_NONE;
}
Loading