Thread.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* This Hello World demostrate a simple multithread program */
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. void* private1 = "Hello World 1";
  5. void* private2 = "Hello World 2";
  6. static volatile int count1 = 0;
  7. int callback1(void* args) {
  8. pal_printf("Run in Child Thread: %s\n", (char*)args);
  9. while (count1 < 10) {
  10. while (!(count1 % 2)) {
  11. DkThreadYieldExecution();
  12. }
  13. count1++;
  14. __asm__ volatile("nop" ::: "memory");
  15. }
  16. pal_printf("Threads Run in Parallel OK\n");
  17. DkSegmentRegister(PAL_SEGMENT_FS, &private2);
  18. const char* ptr2;
  19. __asm__ volatile("mov %%fs:0, %0" : "=r"(ptr2)::"memory");
  20. pal_printf("Private Message (FS Segment) 2: %s\n", ptr2);
  21. count1 = 100;
  22. __asm__ volatile("nop" ::: "memory");
  23. DkThreadExit(/*clear_child_tid=*/NULL);
  24. count1 = 101;
  25. __asm__ volatile("nop" ::: "memory");
  26. return 0;
  27. }
  28. int main(int argc, const char** argv, const char** envp) {
  29. DkSegmentRegister(PAL_SEGMENT_FS, &private1);
  30. const char* ptr1;
  31. __asm__ volatile("mov %%fs:0, %0" : "=r"(ptr1)::"memory");
  32. pal_printf("Private Message (FS Segment) 1: %s\n", ptr1);
  33. PAL_HANDLE thread1 = DkThreadCreate(callback1, "Hello World");
  34. if (thread1) {
  35. pal_printf("Child Thread Created\n");
  36. while (count1 < 10) {
  37. while (!!(count1 % 2)) {
  38. DkThreadYieldExecution();
  39. }
  40. count1++;
  41. __asm__ volatile("nop" ::: "memory");
  42. }
  43. while (count1 < 100) {
  44. DkThreadYieldExecution();
  45. }
  46. for (int i = 0; i < 500; i++) {
  47. DkThreadYieldExecution();
  48. }
  49. __asm__ volatile("nop" ::: "memory");
  50. if (count1 == 100)
  51. pal_printf("Child Thread Exited\n");
  52. }
  53. return 0;
  54. }