Thread.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. {
  9. pal_printf("Run in Child Thread: %s\n", (char *) args);
  10. while (count1 < 10) {
  11. while (!(count1 % 2))
  12. DkThreadYieldExecution();
  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();
  24. count1 = 101;
  25. __asm__ volatile("nop" ::: "memory");
  26. return 0;
  27. }
  28. int main (int argc, const char ** argv, const char ** envp)
  29. {
  30. DkSegmentRegister(PAL_SEGMENT_FS, &private1);
  31. const char * ptr1;
  32. __asm__ volatile("mov %%fs:0, %0" : "=r"(ptr1) :: "memory");
  33. pal_printf("Private Message (FS Segment) 1: %s\n", ptr1);
  34. PAL_HANDLE thread1 = DkThreadCreate(callback1, "Hello World");
  35. if (thread1) {
  36. pal_printf("Child Thread Created\n");
  37. while (count1 < 10) {
  38. while (!!(count1 % 2))
  39. DkThreadYieldExecution();
  40. count1++;
  41. __asm__ volatile("nop" ::: "memory");
  42. }
  43. while (count1 < 100)
  44. DkThreadYieldExecution();
  45. for (int i = 0 ; i < 300 ; i++)
  46. DkThreadYieldExecution();
  47. if (count1 == 100)
  48. pal_printf("Child Thread Exited\n");
  49. }
  50. return 0;
  51. }