PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/director_thread.i

#
Swig | 102 lines | 86 code | 14 blank | 2 comment | 0 complexity | 3436dc0bce438f667b56b2399fc09fd6 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This testcase was in the python subdirectory
  2. #if defined(SWIGPYTHON)
  3. // Is "threads" really needed for Python? It seems to work without it.
  4. %module(directors="1",threads="1") director_thread
  5. #else
  6. %module(directors="1") director_thread
  7. #endif
  8. %{
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #include <process.h>
  12. #else
  13. #include <pthread.h>
  14. #include <signal.h>
  15. #include <unistd.h>
  16. #endif
  17. #include <iostream>
  18. class Foo;
  19. extern "C" {
  20. #ifdef _WIN32
  21. unsigned int __stdcall working(void* t);
  22. unsigned int thread_id(0);
  23. #else
  24. void* working(void* t);
  25. pthread_t thread;
  26. #endif
  27. static int thread_terminate = 0;
  28. }
  29. %}
  30. %director Foo;
  31. %inline {
  32. static void MilliSecondSleep(int milliseconds) {
  33. %#ifdef _WIN32
  34. Sleep(milliseconds);
  35. %#else
  36. usleep(milliseconds*1000);
  37. %#endif
  38. }
  39. class Foo {
  40. public:
  41. int val;
  42. Foo() : val(0) {
  43. }
  44. virtual ~Foo() {
  45. }
  46. void stop() {
  47. thread_terminate = 1;
  48. %#ifdef _WIN32
  49. /*TODO(bhy) what to do for win32? */
  50. %#else
  51. pthread_join(thread, NULL);
  52. %#endif
  53. }
  54. void run() {
  55. %#ifdef _WIN32
  56. _beginthreadex(NULL,0,working,this,0,&thread_id);
  57. %#else
  58. pthread_create(&thread,NULL,working,this);
  59. %#endif
  60. MilliSecondSleep(500);
  61. }
  62. virtual void do_foo() {
  63. val += 1;
  64. }
  65. };
  66. }
  67. %{
  68. extern "C" {
  69. #ifdef _WIN32
  70. unsigned int __stdcall working(void* t)
  71. #else
  72. void* working(void* t)
  73. #endif
  74. {
  75. Foo* f = static_cast<Foo*>(t);
  76. while ( ! thread_terminate ) {
  77. MilliSecondSleep(50);
  78. f->do_foo();
  79. }
  80. #ifdef _WIN32
  81. /* TODO(bhy) what's the corresponding of pthread_exit in win32? */
  82. #else
  83. pthread_exit(0);
  84. #endif
  85. return 0;
  86. }
  87. }
  88. %}