TestSynchThread.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <winpr/crt.h>
  2. #include <winpr/synch.h>
  3. #include <winpr/thread.h>
  4. static DWORD WINAPI test_thread(LPVOID arg)
  5. {
  6. Sleep(1000);
  7. ExitThread(0);
  8. return 0;
  9. }
  10. int TestSynchThread(int argc, char* argv[])
  11. {
  12. DWORD rc;
  13. HANDLE thread;
  14. thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
  15. if (!thread)
  16. {
  17. printf("CreateThread failure\n");
  18. return -1;
  19. }
  20. /* TryJoin should now fail. */
  21. rc = WaitForSingleObject(thread, 0);
  22. if (WAIT_TIMEOUT != rc)
  23. {
  24. printf("Timed WaitForSingleObject on running thread failed with %" PRIu32 "\n", rc);
  25. return -3;
  26. }
  27. /* Join the thread */
  28. rc = WaitForSingleObject(thread, INFINITE);
  29. if (WAIT_OBJECT_0 != rc)
  30. {
  31. printf("WaitForSingleObject on thread failed with %" PRIu32 "\n", rc);
  32. return -2;
  33. }
  34. /* TimedJoin should now succeed. */
  35. rc = WaitForSingleObject(thread, 0);
  36. if (WAIT_OBJECT_0 != rc)
  37. {
  38. printf("Timed WaitForSingleObject on dead thread failed with %" PRIu32 "\n", rc);
  39. return -5;
  40. }
  41. if (!CloseHandle(thread))
  42. {
  43. printf("CloseHandle failed!");
  44. return -1;
  45. }
  46. thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
  47. if (!thread)
  48. {
  49. printf("CreateThread failure\n");
  50. return -1;
  51. }
  52. /* TryJoin should now fail. */
  53. rc = WaitForSingleObject(thread, 50);
  54. if (WAIT_TIMEOUT != rc)
  55. {
  56. printf("Timed WaitForSingleObject on running thread failed with %" PRIu32 "\n", rc);
  57. return -3;
  58. }
  59. /* Join the thread */
  60. rc = WaitForSingleObject(thread, INFINITE);
  61. if (WAIT_OBJECT_0 != rc)
  62. {
  63. printf("WaitForSingleObject on thread failed with %" PRIu32 "\n", rc);
  64. return -2;
  65. }
  66. /* TimedJoin should now succeed. */
  67. rc = WaitForSingleObject(thread, 0);
  68. if (WAIT_OBJECT_0 != rc)
  69. {
  70. printf("Timed WaitForSingleObject on dead thread failed with %" PRIu32 "\n", rc);
  71. return -5;
  72. }
  73. if (!CloseHandle(thread))
  74. {
  75. printf("CloseHandle failed!");
  76. return -1;
  77. }
  78. /* Thread detach test */
  79. thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
  80. if (!thread)
  81. {
  82. printf("CreateThread failure\n");
  83. return -1;
  84. }
  85. if (!CloseHandle(thread))
  86. {
  87. printf("CloseHandle failed!");
  88. return -1;
  89. }
  90. return 0;
  91. }