Thread2.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdatomic.h>
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. volatile bool dummy_true = true;
  5. static atomic_bool thread2_started = false;
  6. static atomic_bool thread3_started = false;
  7. static atomic_bool thread3_exit_ok = true;
  8. static atomic_bool thread4_started = false;
  9. int thread2_run(void* args) {
  10. pal_printf("Thread 2 started.\n");
  11. thread2_started = true;
  12. pal_printf("Exiting thread 2 by return.\n");
  13. return 0;
  14. }
  15. int thread3_run(void* args) {
  16. pal_printf("Thread 3 started.\n");
  17. thread3_started = true;
  18. pal_printf("Exiting thread 3 by DkThreadExit.\n");
  19. // Ensure that the compiler can't know that this should never return.
  20. if (dummy_true) {
  21. DkThreadExit(/*clear_child_tid=*/NULL);
  22. }
  23. thread3_exit_ok = false;
  24. pal_printf("Exiting thread 3 failed.\n");
  25. return 0;
  26. }
  27. int thread4_run(void* args) {
  28. pal_printf("Thread 4 started.\n");
  29. thread4_started = true;
  30. pal_printf("Exiting thread 4 by return.\n");
  31. return 0;
  32. }
  33. // If there's a thread limit, like on SGX, it should be set to exactly 2. There
  34. // should be only the main thread and only one other thread at a time.
  35. int main() {
  36. pal_printf("Thread 1 (main) started.\n");
  37. PAL_HANDLE thread2 = DkThreadCreate(thread2_run, NULL);
  38. if (!thread2) {
  39. pal_printf("DkThreadCreate failed for thread 2.\n");
  40. return 1;
  41. }
  42. // 1 s should be enough even on a very busy system to start a thread and
  43. // then exit it again including all cleanup.
  44. DkThreadDelayExecution(1000000);
  45. if (thread2_started) {
  46. pal_printf("Thread 2 ok.\n");
  47. }
  48. PAL_HANDLE thread3 = DkThreadCreate(thread3_run, NULL);
  49. if (!thread3) {
  50. pal_printf("DkThreadCreate failed for thread 3.\n");
  51. return 1;
  52. }
  53. DkThreadDelayExecution(1000000);
  54. if (thread3_started && thread3_exit_ok) {
  55. pal_printf("Thread 3 ok.\n");
  56. }
  57. PAL_HANDLE thread4 = DkThreadCreate(thread4_run, NULL);
  58. if (!thread4) {
  59. pal_printf("DkThreadCreate failed for thread 4.\n");
  60. return 1;
  61. }
  62. DkThreadDelayExecution(1000000);
  63. if (thread4_started) {
  64. pal_printf("Thread 4 ok.\n");
  65. }
  66. return 0;
  67. }