summaryrefslogtreecommitdiff
path: root/Examples/test-suite/director_thread.i
blob: 4f4e55cfe0df67e79026f118bcebe4375433a2ba (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This testcase was in the python subdirectory

#if defined(SWIGPYTHON)
// Is "threads" really needed for Python? It seems to work without it.
%module(directors="1",threads="1") director_thread
#else
%module(directors="1") director_thread
#endif

%{
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#include <unistd.h>
#endif

#include <iostream>

class Foo;  
extern "C" {
#ifdef _WIN32
  unsigned int __stdcall working(void* t);
  unsigned int thread_id(0);
#else
  void* working(void* t);
  pthread_t thread;
#endif
}
%}

%director Foo;

%inline {
  static void MilliSecondSleep(int milliseconds) {
  %#ifdef _WIN32
    Sleep(milliseconds);
  %#else
    usleep(milliseconds*1000);
  %#endif
  }

  class Foo {
  public:
    int val;
    
    Foo() : val(0) {
    }
    
    virtual ~Foo()  {
    }

    void run() {
%#ifdef _WIN32
      _beginthreadex(NULL,0,working,this,0,&thread_id);
%#else
      pthread_create(&thread,NULL,working,this);
%#endif
      MilliSecondSleep(500);
    }
    
    virtual void do_foo() {
      val += 1;
    }
  };
}

%{
extern "C" {
#ifdef _WIN32
  unsigned int __stdcall working(void* t)
#else
  void* working(void* t)
#endif
  {
    Foo* f = static_cast<Foo*>(t);
    while (1) {
      MilliSecondSleep(50);
      f->do_foo();
    }
    return 0;
  }
}
%}