summaryrefslogtreecommitdiff
path: root/python_cdll_multithread/libpcdlltest.c
blob: 6bc10ea7f4633e815bebbc0ab0ce6861fecf41df (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
}