summaryrefslogtreecommitdiff
path: root/python_cdll_multithread
diff options
context:
space:
mode:
Diffstat (limited to 'python_cdll_multithread')
-rw-r--r--python_cdll_multithread/Makefile8
-rwxr-xr-xpython_cdll_multithread/cdll.py49
-rw-r--r--python_cdll_multithread/libpcdlltest.c54
3 files changed, 111 insertions, 0 deletions
diff --git a/python_cdll_multithread/Makefile b/python_cdll_multithread/Makefile
new file mode 100644
index 0000000..45040bf
--- /dev/null
+++ b/python_cdll_multithread/Makefile
@@ -0,0 +1,8 @@
+CC=gcc
+CFLAGS=-O2 -march=x86-64-v3 -fPIC -shared
+
+rel:
+ $(CC) ${CFLAGS} ./libpcdlltest.c -o libpcdlltest.so -lpthread
+
+clean:
+ rm ./libpcdlltest.so
diff --git a/python_cdll_multithread/cdll.py b/python_cdll_multithread/cdll.py
new file mode 100755
index 0000000..eb76d35
--- /dev/null
+++ b/python_cdll_multithread/cdll.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python3 -u
+import sys;
+import time;
+from ctypes import *;
+
+testlib = CDLL( "./libpcdlltest.so" );
+
+print( testlib );
+
+testlib.testfunc.restype = c_void_p;
+testlib.testfunc2.restype = c_void_p;
+testlib.testfunc3.restype = c_char_p;
+testlib.cleanup_buffer.restype = c_void_p;
+
+print( "Executing first test function (simple message from C code)" );
+
+testlib.testfunc();
+
+print( "Executing test function 2 (pass python value to it)" );
+
+pvar = create_string_buffer( bytes("test message", "utf8") );
+testlib.testfunc2( pvar );
+del pvar;
+
+print( "Executing test function 3 (return value from C code)" );
+
+test_value = testlib.testfunc3().decode("utf8");
+print( f"Returned value: {test_value}" );
+del test_value;
+
+print( "Executing buffer cleanup functiion" );
+
+testlib.cleanup_buffer();
+
+print( "Starting C thread" );
+
+testlib.threadstarter();
+
+print( "Printing garbage from python code" );
+
+for pcount in range(0, 40):
+ print( f"Count is now in python {pcount}" );
+ time.sleep( 1 );
+
+print( "All tests done" );
+
+del testlib;
+
+sys.exit( 0 );
diff --git a/python_cdll_multithread/libpcdlltest.c b/python_cdll_multithread/libpcdlltest.c
new file mode 100644
index 0000000..6bc10ea
--- /dev/null
+++ b/python_cdll_multithread/libpcdlltest.c
@@ -0,0 +1,54 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <time.h>
+#include <pthread.h>
+
+char *t3_buffer = NULL;
+
+__attribute__((visibility("default"))) extern void *testfunc(){
+ fputs( "Yes, it worked, testfunc executed\n", stdout );
+ return NULL;
+}
+
+__attribute__((visibility("default"))) extern void *testfunc2( char *testvalue ){
+ fprintf( stdout, "Value passed from python: %s\n", (const char*)testvalue );
+ return NULL;
+}
+
+__attribute__((visibility("default"))) extern char *testfunc3(){
+ t3_buffer = (char*)calloc( strlen("test") + 1, sizeof(char) );
+ assert( t3_buffer != NULL );
+ snprintf( t3_buffer, (strlen("test") * sizeof(char)) + 1, "test" );
+ return t3_buffer;
+}
+
+__attribute__((visibility("default"))) extern void *cleanup_buffer(){
+ if ( t3_buffer == NULL )
+ return NULL;
+ free( t3_buffer );
+ t3_buffer = NULL;
+ fputs( "Buffer freed\n", stdout );
+ return NULL;
+}
+
+__attribute__((visibility("hidden"))) extern void *thread_func( void *uptr ){
+ uint32_t loop_counter = 0;
+ for ( ; loop_counter < 30; loop_counter++ ){
+ fprintf( stdout, "Loop counter is now %i\n", loop_counter );
+ sleep( 1 );
+ }
+ return NULL;
+}
+
+__attribute__((visibility("default"))) extern void *threadstarter(){
+ pthread_t tid = 0;
+ if ( pthread_create(&tid, NULL, &thread_func, NULL) == -1 )
+ return NULL;
+ return NULL;
+}