Skip to content

Instantly share code, notes, and snippets.

@timotta
Created November 22, 2012 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timotta/fc4a84ade91284b8cf2d to your computer and use it in GitHub Desktop.
Save timotta/fc4a84ade91284b8cf2d to your computer and use it in GitHub Desktop.
Extensão de python criada para testar liberação do GIL, descrição completa: http://programandosemcafeina.blogspot.com.br/2012/11/liberando-o-gil-do-python-para.html
#include <Python.h>
#include <stdio.h>
static int antigil_calculos(int soma, int nova) {
int i;
for(i=0; i<nova; i++) {
soma += i;
}
char numero [5000];
sprintf(numero, "%d", soma);
return strlen(numero);
}
static int reduce_com_gil(int max, int (*f)(int x, int y)) {
int retorno = 0;
int i;
for(i=0; i<max; i++) {
retorno = (*f)(retorno, i);
}
return retorno;
}
static int reduce_sem_gil(int max, int (*f)(int x, int y)) {
int retorno = 0;
Py_BEGIN_ALLOW_THREADS
int i;
for(i=0; i<max; i++) {
retorno = (*f)(retorno, i);
}
Py_END_ALLOW_THREADS
return retorno;
}
static PyObject *antigil_calcular_com_gil(PyObject *self) {
int valor = reduce_com_gil(100*1000, *antigil_calculos);
char numero [5000];
sprintf(numero, "%d", valor );
return Py_BuildValue("s", numero);
}
static PyObject *antigil_calcular_sem_gil(PyObject *self) {
int valor = reduce_sem_gil(100*1000, *antigil_calculos);
char numero [5000];
sprintf(numero, "%d", valor );
return Py_BuildValue("s", numero);
}
static PyMethodDef antigil_funcs[] = {
{ "calcular_com_gil", (PyCFunction)antigil_calcular_com_gil, METH_NOARGS, "teste" },
{ "calcular_sem_gil", (PyCFunction)antigil_calcular_sem_gil, METH_NOARGS, "teste" },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initantigil(void) {
(void) Py_InitModule("antigil", antigil_funcs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment