/Python/thread_wince.h

http://unladen-swallow.googlecode.com/ · C++ Header · 171 lines · 108 code · 35 blank · 28 comment · 17 complexity · 52564294cad799c29872a7ac412ad71f MD5 · raw file

  1. /* This code implemented by Mark Hammond (MHammond@skippinet.com.au) */
  2. #include <windows.h>
  3. #include <limits.h>
  4. #include <pydebug.h>
  5. long PyThread_get_thread_ident(void);
  6. /*
  7. * Change all headers to pure ANSI as no one will use K&R style on an
  8. * NT
  9. */
  10. /*
  11. * Initialization of the C package, should not be needed.
  12. */
  13. static void PyThread__init_thread(void)
  14. {
  15. }
  16. /*
  17. * Thread support.
  18. */
  19. long PyThread_start_new_thread(void (*func)(void *), void *arg)
  20. {
  21. long rv;
  22. int success = -1;
  23. dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
  24. if (!initialized)
  25. PyThread_init_thread();
  26. rv = _beginthread(func, 0, arg); /* use default stack size */
  27. if (rv != -1) {
  28. success = 0;
  29. dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident()));
  30. }
  31. return success;
  32. }
  33. /*
  34. * Return the thread Id instead of an handle. The Id is said to uniquely identify the
  35. * thread in the system
  36. */
  37. long PyThread_get_thread_ident(void)
  38. {
  39. if (!initialized)
  40. PyThread_init_thread();
  41. return GetCurrentThreadId();
  42. }
  43. static void do_PyThread_exit_thread(int no_cleanup)
  44. {
  45. dprintf(("%ld: do_PyThread_exit_thread called\n", PyThread_get_thread_ident()));
  46. if (!initialized)
  47. if (no_cleanup)
  48. exit(0); /* XXX - was _exit()!! */
  49. else
  50. exit(0);
  51. _endthread();
  52. }
  53. void PyThread_exit_thread(void)
  54. {
  55. do_PyThread_exit_thread(0);
  56. }
  57. void PyThread__exit_thread(void)
  58. {
  59. do_PyThread_exit_thread(1);
  60. }
  61. #ifndef NO_EXIT_PROG
  62. static void do_PyThread_exit_prog(int status, int no_cleanup)
  63. {
  64. dprintf(("PyThread_exit_prog(%d) called\n", status));
  65. if (!initialized)
  66. if (no_cleanup)
  67. _exit(status);
  68. else
  69. exit(status);
  70. }
  71. void PyThread_exit_prog(int status)
  72. {
  73. do_PyThread_exit_prog(status, 0);
  74. }
  75. void PyThread__exit_prog(int status)
  76. {
  77. do_PyThread_exit_prog(status, 1);
  78. }
  79. #endif /* NO_EXIT_PROG */
  80. /*
  81. * Lock support. It has to be implemented using Mutexes, as
  82. * CE doesnt support semaphores. Therefore we use some hacks to
  83. * simulate the non reentrant requirements of Python locks
  84. */
  85. PyThread_type_lock PyThread_allocate_lock(void)
  86. {
  87. HANDLE aLock;
  88. dprintf(("PyThread_allocate_lock called\n"));
  89. if (!initialized)
  90. PyThread_init_thread();
  91. aLock = CreateEvent(NULL, /* Security attributes */
  92. 0, /* Manual-Reset */
  93. 1, /* Is initially signalled */
  94. NULL); /* Name of event */
  95. dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
  96. return (PyThread_type_lock) aLock;
  97. }
  98. void PyThread_free_lock(PyThread_type_lock aLock)
  99. {
  100. dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
  101. CloseHandle(aLock);
  102. }
  103. /*
  104. * Return 1 on success if the lock was acquired
  105. *
  106. * and 0 if the lock was not acquired. This means a 0 is returned
  107. * if the lock has already been acquired by this thread!
  108. */
  109. int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  110. {
  111. int success = 1;
  112. DWORD waitResult;
  113. dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
  114. #ifndef DEBUG
  115. waitResult = WaitForSingleObject(aLock, (waitflag ? INFINITE : 0));
  116. #else
  117. /* To aid in debugging, we regularly wake up. This allows us to
  118. break into the debugger */
  119. while (TRUE) {
  120. waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0);
  121. if (waitflag==0 || (waitflag && waitResult == WAIT_OBJECT_0))
  122. break;
  123. }
  124. #endif
  125. if (waitResult != WAIT_OBJECT_0) {
  126. success = 0; /* We failed */
  127. }
  128. dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
  129. return success;
  130. }
  131. void PyThread_release_lock(PyThread_type_lock aLock)
  132. {
  133. dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
  134. if (!SetEvent(aLock))
  135. dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
  136. }