Author: Armin Rigo <[email protected]>
Branch: static-callback-embedding
Changeset: r2517:652f66e41c7b
Date: 2016-01-02 15:55 +0100
http://bitbucket.org/cffi/cffi/changeset/652f66e41c7b/
Log: A test checking that thread-local values are saved, even though
there is no underlying official Python thread
diff --git a/testing/embedding/test_tlocal.py b/testing/embedding/test_tlocal.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/test_tlocal.py
@@ -0,0 +1,10 @@
+from testing.embedding.test_basic import EmbeddingTests
+
+
+class TestThreadLocal(EmbeddingTests):
+ def test_thread_local(self):
+ self.prepare_module('tlocal')
+ self.compile('tlocal-test', ['_tlocal_cffi'], ['-pthread'])
+ for i in range(50):
+ output = self.execute('tlocal-test')
+ assert output == "done\n"
diff --git a/testing/embedding/thread1-test.c b/testing/embedding/thread1-test.c
--- a/testing/embedding/thread1-test.c
+++ b/testing/embedding/thread1-test.c
@@ -13,7 +13,7 @@
static void *start_routine(void *arg)
{
- int x, y, status;
+ int x, status;
x = add1(40, 2);
assert(x == 42);
diff --git a/testing/embedding/tlocal-test.c b/testing/embedding/tlocal-test.c
new file mode 100644
--- /dev/null
+++ b/testing/embedding/tlocal-test.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <assert.h>
+
+#define NTHREADS 10
+
+
+extern int add1(int, int);
+
+static sem_t done;
+
+
+static void *start_routine(void *arg)
+{
+ int i, x, expected, status;
+
+ expected = add1(40, 2);
+ assert((expected % 1000) == 42);
+
+ for (i=0; i<10; i++) {
+ x = add1(40, 2);
+ assert(x == expected);
+ }
+
+ status = sem_post(&done);
+ assert(status == 0);
+
+ return arg;
+}
+
+int main(void)
+{
+ pthread_t th;
+ int i, status = sem_init(&done, 0, 0);
+ assert(status == 0);
+
+ for (i = 0; i < NTHREADS; i++) {
+ status = pthread_create(&th, NULL, start_routine, NULL);
+ assert(status == 0);
+ }
+ for (i = 0; i < NTHREADS; i++) {
+ status = sem_wait(&done);
+ assert(status == 0);
+ }
+ printf("done\n");
+ return 0;
+}
diff --git a/testing/embedding/tlocal.py b/testing/embedding/tlocal.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/tlocal.py
@@ -0,0 +1,26 @@
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+ extern "Python" int add1(int, int);
+""", dllexport=True)
+
+ffi.embedding_init_code(r"""
+ import thread, itertools
+ tloc = thread._local()
+ g_seen = itertools.count()
+
+ @ffi.def_extern()
+ def add1(x, y):
+ try:
+ num = tloc.num
+ except AttributeError:
+ num = tloc.num = g_seen.next() * 1000
+ return x + y + num
+""")
+
+ffi.set_source("_tlocal_cffi", """
+""")
+
+ffi.compile(verbose=True)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit