Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created April 19, 2012 21:43
Show Gist options
  • Save dcolish/2424404 to your computer and use it in GitHub Desktop.
Save dcolish/2424404 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#include "Python.h"
#define atomic_int_add(ptr, val) __sync_add_and_fetch(&ptr, val)
static PyObject*
int_add(PyObject *self, PyObject *args)
{
int avalue, bvalue;
if (!PyArg_ParseTuple(args, "ii", &avalue, &bvalue))
return NULL;
return Py_BuildValue("i", atomic_int_add(avalue, bvalue));
}
static PyMethodDef AtomicMethods[] = {
{"int_add", int_add, METH_VARARGS, "Atomically increment an integer"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initatomic(void)
{
Py_InitModule("atomic", AtomicMethods);
}
from setuptools import setup, Extension
atomic = Extension('atomic',
sources=['atomic_py.c'])
setup(name='AtomicPy',
version='dev',
ext_modules=[atomic]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment